File tree 3 files changed +38
-7
lines changed
3 files changed +38
-7
lines changed Original file line number Diff line number Diff line change 41
41
42
42
pub const PUBKEY : & str = "7RoSF9fUmdphVCpabEoefH81WwrW7orsWonXWqTXkKV8" ;
43
43
44
- pub type Mocks = HashMap < RpcRequest , Value > ;
44
+ #[ derive( Default ) ]
45
+ pub struct Mocks ( Vec < ( RpcRequest , Value ) > ) ;
46
+
47
+ impl Mocks {
48
+ pub fn new ( ) -> Self {
49
+ Self ( Vec :: new ( ) )
50
+ }
51
+
52
+ pub fn from < const N : usize > ( mocks : [ ( RpcRequest , Value ) ; N ] ) -> Self {
53
+ Self ( mocks. to_vec ( ) )
54
+ }
55
+
56
+ pub fn insert ( & mut self , request : RpcRequest , response : Value ) {
57
+ self . 0 . push ( ( request, response) ) ;
58
+ }
59
+
60
+ pub fn remove ( & mut self , request : & RpcRequest ) -> Option < Value > {
61
+ self . 0
62
+ . iter ( )
63
+ . position ( |( r, _) | r == request)
64
+ . map ( |index| self . 0 . remove ( index) . 1 )
65
+ }
66
+ }
67
+
68
+ impl FromIterator < ( RpcRequest , Value ) > for Mocks {
69
+ fn from_iter < T : IntoIterator < Item = ( RpcRequest , Value ) > > ( iter : T ) -> Self {
70
+ let mut mocks = Mocks :: new ( ) ;
71
+ for ( request, response) in iter {
72
+ mocks. insert ( request, response) ;
73
+ }
74
+ mocks
75
+ }
76
+ }
77
+
45
78
pub struct MockSender {
46
79
mocks : RwLock < Mocks > ,
47
80
url : String ,
Original file line number Diff line number Diff line change @@ -4707,7 +4707,7 @@ pub(crate) fn parse_keyed_accounts(
4707
4707
4708
4708
#[ doc( hidden) ]
4709
4709
pub fn create_rpc_client_mocks ( ) -> crate :: mock_sender:: Mocks {
4710
- let mut mocks = std :: collections :: HashMap :: new ( ) ;
4710
+ let mut mocks = crate :: mock_sender :: Mocks :: default ( ) ;
4711
4711
4712
4712
let get_account_request = RpcRequest :: GetAccountInfo ;
4713
4713
let get_account_response = serde_json:: to_value ( Response {
Original file line number Diff line number Diff line change @@ -3677,7 +3677,7 @@ impl RpcClient {
3677
3677
/// Mocks for documentation examples
3678
3678
#[ doc( hidden) ]
3679
3679
pub fn create_rpc_client_mocks ( ) -> crate :: mock_sender:: Mocks {
3680
- let mut mocks = std :: collections :: HashMap :: new ( ) ;
3680
+ let mut mocks = crate :: mock_sender :: Mocks :: default ( ) ;
3681
3681
3682
3682
let get_account_request = RpcRequest :: GetAccountInfo ;
3683
3683
let get_account_response = serde_json:: to_value ( Response {
@@ -3980,7 +3980,7 @@ mod tests {
3980
3980
3981
3981
// Test: with context
3982
3982
{
3983
- let mocks: Mocks = [ (
3983
+ let mocks: Mocks = Mocks :: from ( [ (
3984
3984
RpcRequest :: GetProgramAccounts ,
3985
3985
serde_json:: to_value ( OptionalContext :: Context ( Response {
3986
3986
context : RpcResponseContext {
@@ -3990,9 +3990,7 @@ mod tests {
3990
3990
value : vec ! [ keyed_account] ,
3991
3991
} ) )
3992
3992
. unwrap ( ) ,
3993
- ) ]
3994
- . into_iter ( )
3995
- . collect ( ) ;
3993
+ ) ] ) ;
3996
3994
let rpc_client = RpcClient :: new_mock_with_mocks ( "mock_client" . to_string ( ) , mocks) ;
3997
3995
let result = rpc_client
3998
3996
. get_program_accounts_with_config (
You can’t perform that action at this time.
0 commit comments