@@ -71,6 +71,29 @@ pub struct ExpectedMessage {
71
71
contains : Vec < String > ,
72
72
}
73
73
74
+ #[ derive( Debug ) ]
75
+ pub enum MsgMatchError {
76
+ ParseError ,
77
+ MissingJsonrpc ,
78
+ BadJsonrpc ,
79
+ MissingId ,
80
+ BadId ,
81
+ StringNotFound ( String , String ) ,
82
+ }
83
+
84
+ impl < ' a , ' b > ToString for MsgMatchError {
85
+ fn to_string ( & self ) -> String {
86
+ match self {
87
+ MsgMatchError :: ParseError => "JSON parsing failed" . into ( ) ,
88
+ MsgMatchError :: MissingJsonrpc => "Missing jsonrpc field" . into ( ) ,
89
+ MsgMatchError :: BadJsonrpc => "Bad jsonrpc field" . into ( ) ,
90
+ MsgMatchError :: MissingId => "Missing id field" . into ( ) ,
91
+ MsgMatchError :: BadId => "Unexpected id" . into ( ) ,
92
+ MsgMatchError :: StringNotFound ( n, h) => format ! ( "Could not find `{}` in `{}`" , n, h) ,
93
+ }
94
+ }
95
+ }
96
+
74
97
impl ExpectedMessage {
75
98
pub fn new ( id : Option < u64 > ) -> ExpectedMessage {
76
99
ExpectedMessage {
@@ -83,6 +106,36 @@ impl ExpectedMessage {
83
106
self . contains . push ( s. to_owned ( ) ) ;
84
107
self
85
108
}
109
+
110
+ // Err is the expected message that was not found in the given string.
111
+ pub fn try_match ( & self , msg : & str ) -> Result < ( ) , MsgMatchError > {
112
+ use self :: MsgMatchError :: * ;
113
+
114
+ let values: serde_json:: Value = serde_json:: from_str ( msg) . map_err ( |_| ParseError ) ?;
115
+ let jsonrpc = values. get ( "jsonrpc" ) . ok_or ( MissingJsonrpc ) ?;
116
+ if jsonrpc != "2.0" {
117
+ return Err ( BadJsonrpc ) ;
118
+ }
119
+
120
+ if let Some ( id) = self . id {
121
+ let values_id = values. get ( "id" ) . ok_or ( MissingId ) ?;
122
+ if id != values_id. as_u64 ( ) . unwrap ( ) {
123
+ return Err ( BadId )
124
+ } ;
125
+ }
126
+
127
+ self . try_match_raw ( msg)
128
+ }
129
+
130
+ pub fn try_match_raw ( & self , s : & str ) -> Result < ( ) , MsgMatchError > {
131
+ for c in & self . contains {
132
+ s
133
+ . find ( c)
134
+ . ok_or ( MsgMatchError :: StringNotFound ( c. to_owned ( ) , s. to_owned ( ) ) )
135
+ . map ( |_| ( ) ) ?;
136
+ }
137
+ Ok ( ( ) )
138
+ }
86
139
}
87
140
88
141
pub fn read_message < R : Read > ( reader : & mut BufReader < R > ) -> io:: Result < String > {
@@ -112,12 +165,12 @@ pub fn read_message<R: Read>(reader: &mut BufReader<R>) -> io::Result<String> {
112
165
Ok ( result)
113
166
}
114
167
168
+ pub fn read_messages < R : Read > ( reader : & mut BufReader < R > , count : usize ) -> Vec < String > {
169
+ ( 0 ..count) . map ( |_| read_message ( reader) . unwrap ( ) ) . collect ( )
170
+ }
171
+
115
172
pub fn expect_messages < R : Read > ( reader : & mut BufReader < R > , expected : & [ & ExpectedMessage ] ) {
116
- let mut results: Vec < String > = Vec :: new ( ) ;
117
- while results. len ( ) < expected. len ( ) {
118
- let msg = read_message ( reader) . unwrap ( ) ;
119
- results. push ( msg) ;
120
- }
173
+ let results = read_messages ( reader, expected. len ( ) ) ;
121
174
122
175
println ! (
123
176
"expect_messages:\n results: {:#?},\n expected: {:#?}" ,
@@ -126,30 +179,38 @@ pub fn expect_messages<R: Read>(reader: &mut BufReader<R>, expected: &[&Expected
126
179
) ;
127
180
assert_eq ! ( results. len( ) , expected. len( ) ) ;
128
181
for ( found, expected) in results. iter ( ) . zip ( expected. iter ( ) ) {
129
- let values: serde_json:: Value = serde_json:: from_str ( found) . unwrap ( ) ;
130
- assert ! (
131
- values
132
- . get( "jsonrpc" )
133
- . expect( "Missing jsonrpc field" )
134
- . as_str( )
135
- . unwrap( ) == "2.0" ,
136
- "Bad jsonrpc field"
137
- ) ;
138
- if let Some ( id) = expected. id {
139
- assert_eq ! (
140
- values
141
- . get( "id" )
142
- . expect( "Missing id field" )
143
- . as_u64( )
144
- . unwrap( ) ,
145
- id,
146
- "Unexpected id"
147
- ) ;
182
+ if let Err ( err) = expected. try_match ( & found) {
183
+ panic ! ( err. to_string( ) ) ;
148
184
}
149
- for c in & expected. contains {
150
- found
151
- . find ( c)
152
- . expect ( & format ! ( "Could not find `{}` in `{}`" , c, found) ) ;
185
+ }
186
+ }
187
+
188
+ pub fn expect_messages_unordered < R : Read > ( reader : & mut BufReader < R > , expected : & [ & ExpectedMessage ] ) {
189
+ let mut results = read_messages ( reader, expected. len ( ) ) ;
190
+ let mut expected: Vec < & ExpectedMessage > = expected. to_owned ( ) ;
191
+
192
+ println ! (
193
+ "expect_messages_unordered:\n results: {:#?},\n expected: {:#?}" ,
194
+ results,
195
+ expected
196
+ ) ;
197
+ assert_eq ! ( results. len( ) , expected. len( ) ) ;
198
+
199
+ while !results. is_empty ( ) && !expected. is_empty ( ) {
200
+ let first = expected[ 0 ] ;
201
+
202
+ let opt = results. iter ( )
203
+ . map ( |r| first. try_match ( r) )
204
+ . enumerate ( )
205
+ . find ( |( _, res) | res. is_ok ( ) ) ;
206
+
207
+ match opt {
208
+ Some ( ( idx, Ok ( ( ) ) ) ) => {
209
+ expected. remove ( 0 ) ;
210
+ results. remove ( idx) ;
211
+ } ,
212
+ Some ( ( _, Err ( err) ) ) => panic ! ( err. to_string( ) ) ,
213
+ None => panic ! ( format!( "Could not find `{:?}` among `{:?}" , first, results) )
153
214
}
154
215
}
155
216
}
@@ -232,6 +293,10 @@ impl RlsHandle {
232
293
pub fn expect_messages ( & mut self , expected : & [ & ExpectedMessage ] ) {
233
294
expect_messages ( & mut self . stdout , expected) ;
234
295
}
296
+
297
+ pub fn expect_messages_unordered ( & mut self , expected : & [ & ExpectedMessage ] ) {
298
+ expect_messages_unordered ( & mut self . stdout , expected) ;
299
+ }
235
300
}
236
301
237
302
#[ derive( PartialEq , Clone ) ]
0 commit comments