@@ -66,14 +66,17 @@ impl CommandKind {
66
66
} ;
67
67
68
68
if !count {
69
- print_err ( & format ! ( "Incorrect number of arguments to `@ {}`" , self ) , lineno) ;
69
+ print_err ( & format ! ( "Incorrect number of arguments to `{}`" , self ) , lineno) ;
70
70
return false ;
71
71
}
72
72
73
73
if let CommandKind :: Count = self {
74
74
if args[ 1 ] . parse :: < usize > ( ) . is_err ( ) {
75
75
print_err (
76
- & format ! ( "Second argument to @count must be a valid usize (got `{}`)" , args[ 1 ] ) ,
76
+ & format ! (
77
+ "Second argument to `count` must be a valid usize (got `{}`)" ,
78
+ args[ 1 ]
79
+ ) ,
77
80
lineno,
78
81
) ;
79
82
return false ;
@@ -101,7 +104,8 @@ static LINE_PATTERN: OnceLock<Regex> = OnceLock::new();
101
104
fn line_pattern ( ) -> Regex {
102
105
RegexBuilder :: new (
103
106
r#"
104
- \s(?P<invalid>!?)@(?P<negated>!?)
107
+ //@\s+
108
+ (?P<negated>!?)
105
109
(?P<cmd>[A-Za-z]+(?:-[A-Za-z]+)*)
106
110
(?P<args>.*)$
107
111
"# ,
@@ -116,6 +120,10 @@ fn print_err(msg: &str, lineno: usize) {
116
120
eprintln ! ( "Invalid command: {} on line {}" , msg, lineno)
117
121
}
118
122
123
+ // FIXME: This setup is temporary until we figure out how to improve this situation.
124
+ // See <https://github.com/rust-lang/rust/issues/125813#issuecomment-2141953780>.
125
+ include ! ( concat!( env!( "CARGO_MANIFEST_DIR" ) , "/../compiletest/src/command-list.rs" ) ) ;
126
+
119
127
/// Get a list of commands from a file. Does the work of ensuring the commands
120
128
/// are syntactically valid.
121
129
fn get_commands ( template : & str ) -> Result < Vec < Command > , ( ) > {
@@ -132,36 +140,22 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
132
140
} ;
133
141
134
142
let negated = cap. name ( "negated" ) . unwrap ( ) . as_str ( ) == "!" ;
135
- let cmd = cap. name ( "cmd" ) . unwrap ( ) . as_str ( ) ;
136
143
137
- let cmd = match cmd {
144
+ let cmd = match cap . name ( " cmd" ) . unwrap ( ) . as_str ( ) {
138
145
"has" => CommandKind :: Has ,
139
146
"count" => CommandKind :: Count ,
140
147
"is" => CommandKind :: Is ,
141
148
"ismany" => CommandKind :: IsMany ,
142
149
"set" => CommandKind :: Set ,
143
- _ => {
144
- print_err ( & format ! ( "Unrecognized command name `@{}`" , cmd) , lineno) ;
150
+ // FIXME: See the comment above the `include!(...)`.
151
+ cmd if KNOWN_DIRECTIVE_NAMES . contains ( & cmd) => continue ,
152
+ cmd => {
153
+ print_err ( & format ! ( "Unrecognized command name `{cmd}`" ) , lineno) ;
145
154
errors = true ;
146
155
continue ;
147
156
}
148
157
} ;
149
158
150
- if let Some ( m) = cap. name ( "invalid" ) {
151
- if m. as_str ( ) == "!" {
152
- print_err (
153
- & format ! (
154
- "`!@{0}{1}`, (help: try with `@!{1}`)" ,
155
- if negated { "!" } else { "" } ,
156
- cmd,
157
- ) ,
158
- lineno,
159
- ) ;
160
- errors = true ;
161
- continue ;
162
- }
163
- }
164
-
165
159
let args = cap. name ( "args" ) . map_or ( Some ( vec ! [ ] ) , |m| shlex:: split ( m. as_str ( ) ) ) ;
166
160
167
161
let args = match args {
@@ -197,19 +191,19 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
197
191
let result = match command. kind {
198
192
CommandKind :: Has => {
199
193
match command. args . len ( ) {
200
- // @ has <jsonpath> = check path exists
194
+ // ` has <jsonpath>`: Check that `jsonpath` exists.
201
195
1 => {
202
196
let val = cache. value ( ) ;
203
197
let results = select ( val, & command. args [ 0 ] ) . unwrap ( ) ;
204
198
!results. is_empty ( )
205
199
}
206
- // @ has <jsonpath> <value> = check *any* item matched by path equals value
200
+ // ` has <jsonpath> <value>`: Check *any* item matched by `jsonpath` equals ` value`.
207
201
2 => {
208
202
let val = cache. value ( ) . clone ( ) ;
209
203
let results = select ( & val, & command. args [ 0 ] ) . unwrap ( ) ;
210
204
let pat = string_to_value ( & command. args [ 1 ] , cache) ;
211
205
let has = results. contains ( & pat. as_ref ( ) ) ;
212
- // Give better error for when @ has check fails
206
+ // Give better error for when ` has` check fails.
213
207
if !command. negated && !has {
214
208
return Err ( CkError :: FailedCheck (
215
209
format ! (
@@ -227,16 +221,16 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
227
221
_ => unreachable ! ( ) ,
228
222
}
229
223
}
224
+ // `ismany <path> <jsonpath> <value...>`
230
225
CommandKind :: IsMany => {
231
- // @ ismany <path> <jsonpath> <value>...
226
+ assert ! ( !command . negated , "` ismany` may not be negated" ) ;
232
227
let ( query, values) = if let [ query, values @ ..] = & command. args [ ..] {
233
228
( query, values)
234
229
} else {
235
230
unreachable ! ( "Checked in CommandKind::validate" )
236
231
} ;
237
232
let val = cache. value ( ) ;
238
233
let got_values = select ( val, & query) . unwrap ( ) ;
239
- assert ! ( !command. negated, "`@!ismany` is not supported" ) ;
240
234
241
235
// Serde json doesn't implement Ord or Hash for Value, so we must
242
236
// use a Vec here. While in theory that makes setwize equality
@@ -265,8 +259,8 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
265
259
}
266
260
true
267
261
}
262
+ // `count <jsonpath> <count>`: Check that `jsonpath` matches exactly `count` times.
268
263
CommandKind :: Count => {
269
- // @count <jsonpath> <count> = Check that the jsonpath matches exactly [count] times
270
264
assert_eq ! ( command. args. len( ) , 2 ) ;
271
265
let expected: usize = command. args [ 1 ] . parse ( ) . unwrap ( ) ;
272
266
let val = cache. value ( ) ;
@@ -287,8 +281,8 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
287
281
eq
288
282
}
289
283
}
284
+ // `has <jsonpath> <value>`: Check` *exactly one* item matched by `jsonpath`, and it equals `value`.
290
285
CommandKind :: Is => {
291
- // @has <jsonpath> <value> = check *exactly one* item matched by path, and it equals value
292
286
assert_eq ! ( command. args. len( ) , 2 ) ;
293
287
let val = cache. value ( ) . clone ( ) ;
294
288
let results = select ( & val, & command. args [ 0 ] ) . unwrap ( ) ;
@@ -308,16 +302,17 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
308
302
is
309
303
}
310
304
}
305
+ // `set <name> = <jsonpath>`
311
306
CommandKind :: Set => {
312
- // @ set <name> = <jsonpath>
307
+ assert ! ( !command . negated , "` set` may not be negated" ) ;
313
308
assert_eq ! ( command. args. len( ) , 3 ) ;
314
309
assert_eq ! ( command. args[ 1 ] , "=" , "Expected an `=`" ) ;
315
310
let val = cache. value ( ) . clone ( ) ;
316
311
let results = select ( & val, & command. args [ 2 ] ) . unwrap ( ) ;
317
312
assert_eq ! (
318
313
results. len( ) ,
319
314
1 ,
320
- "Expected 1 match for `{}` (because of @ set): matched to {:?}" ,
315
+ "Expected 1 match for `{}` (because of ` set` ): matched to {:?}" ,
321
316
command. args[ 2 ] ,
322
317
results
323
318
) ;
@@ -330,7 +325,7 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
330
325
}
331
326
_ => {
332
327
panic ! (
333
- "Got multiple results in `@ set` for `{}`: {:?}" ,
328
+ "Got multiple results in `set` for `{}`: {:?}" ,
334
329
& command. args[ 2 ] , results,
335
330
) ;
336
331
}
@@ -341,18 +336,14 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
341
336
if result == command. negated {
342
337
if command. negated {
343
338
Err ( CkError :: FailedCheck (
344
- format ! (
345
- "`@!{} {}` matched when it shouldn't" ,
346
- command. kind,
347
- command. args. join( " " )
348
- ) ,
339
+ format ! ( "`!{} {}` matched when it shouldn't" , command. kind, command. args. join( " " ) ) ,
349
340
command,
350
341
) )
351
342
} else {
352
343
// FIXME: In the future, try 'peeling back' each step, and see at what level the match failed
353
344
Err ( CkError :: FailedCheck (
354
345
format ! (
355
- "`@ {} {}` didn't match when it should" ,
346
+ "`{} {}` didn't match when it should" ,
356
347
command. kind,
357
348
command. args. join( " " )
358
349
) ,
0 commit comments