@@ -66,14 +66,17 @@ impl CommandKind {
6666 } ;
6767
6868 if !count {
69- print_err ( & format ! ( "Incorrect number of arguments to `@ {}`" , self ) , lineno) ;
69+ print_err ( & format ! ( "Incorrect number of arguments to `{}`" , self ) , lineno) ;
7070 return false ;
7171 }
7272
7373 if let CommandKind :: Count = self {
7474 if args[ 1 ] . parse :: < usize > ( ) . is_err ( ) {
7575 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+ ) ,
7780 lineno,
7881 ) ;
7982 return false ;
@@ -101,7 +104,8 @@ static LINE_PATTERN: OnceLock<Regex> = OnceLock::new();
101104fn line_pattern ( ) -> Regex {
102105 RegexBuilder :: new (
103106 r#"
104- \s(?P<invalid>!?)@(?P<negated>!?)
107+ //@\s+
108+ (?P<negated>!?)
105109 (?P<cmd>[A-Za-z]+(?:-[A-Za-z]+)*)
106110 (?P<args>.*)$
107111 "# ,
@@ -116,6 +120,10 @@ fn print_err(msg: &str, lineno: usize) {
116120 eprintln ! ( "Invalid command: {} on line {}" , msg, lineno)
117121}
118122
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+
119127/// Get a list of commands from a file. Does the work of ensuring the commands
120128/// are syntactically valid.
121129fn get_commands ( template : & str ) -> Result < Vec < Command > , ( ) > {
@@ -132,36 +140,22 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
132140 } ;
133141
134142 let negated = cap. name ( "negated" ) . unwrap ( ) . as_str ( ) == "!" ;
135- let cmd = cap. name ( "cmd" ) . unwrap ( ) . as_str ( ) ;
136143
137- let cmd = match cmd {
144+ let cmd = match cap . name ( " cmd" ) . unwrap ( ) . as_str ( ) {
138145 "has" => CommandKind :: Has ,
139146 "count" => CommandKind :: Count ,
140147 "is" => CommandKind :: Is ,
141148 "ismany" => CommandKind :: IsMany ,
142149 "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) ;
145154 errors = true ;
146155 continue ;
147156 }
148157 } ;
149158
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-
165159 let args = cap. name ( "args" ) . map_or ( Some ( vec ! [ ] ) , |m| shlex:: split ( m. as_str ( ) ) ) ;
166160
167161 let args = match args {
@@ -197,19 +191,19 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
197191 let result = match command. kind {
198192 CommandKind :: Has => {
199193 match command. args . len ( ) {
200- // @ has <jsonpath> = check path exists
194+ // ` has <jsonpath>`: Check that `jsonpath` exists.
201195 1 => {
202196 let val = cache. value ( ) ;
203197 let results = select ( val, & command. args [ 0 ] ) . unwrap ( ) ;
204198 !results. is_empty ( )
205199 }
206- // @ has <jsonpath> <value> = check *any* item matched by path equals value
200+ // ` has <jsonpath> <value>`: Check *any* item matched by `jsonpath` equals ` value`.
207201 2 => {
208202 let val = cache. value ( ) . clone ( ) ;
209203 let results = select ( & val, & command. args [ 0 ] ) . unwrap ( ) ;
210204 let pat = string_to_value ( & command. args [ 1 ] , cache) ;
211205 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.
213207 if !command. negated && !has {
214208 return Err ( CkError :: FailedCheck (
215209 format ! (
@@ -227,16 +221,16 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
227221 _ => unreachable ! ( ) ,
228222 }
229223 }
224+ // `ismany <path> <jsonpath> <value...>`
230225 CommandKind :: IsMany => {
231- // @ ismany <path> <jsonpath> <value>...
226+ assert ! ( !command . negated , "` ismany` may not be negated" ) ;
232227 let ( query, values) = if let [ query, values @ ..] = & command. args [ ..] {
233228 ( query, values)
234229 } else {
235230 unreachable ! ( "Checked in CommandKind::validate" )
236231 } ;
237232 let val = cache. value ( ) ;
238233 let got_values = select ( val, & query) . unwrap ( ) ;
239- assert ! ( !command. negated, "`@!ismany` is not supported" ) ;
240234
241235 // Serde json doesn't implement Ord or Hash for Value, so we must
242236 // 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> {
265259 }
266260 true
267261 }
262+ // `count <jsonpath> <count>`: Check that `jsonpath` matches exactly `count` times.
268263 CommandKind :: Count => {
269- // @count <jsonpath> <count> = Check that the jsonpath matches exactly [count] times
270264 assert_eq ! ( command. args. len( ) , 2 ) ;
271265 let expected: usize = command. args [ 1 ] . parse ( ) . unwrap ( ) ;
272266 let val = cache. value ( ) ;
@@ -287,8 +281,8 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
287281 eq
288282 }
289283 }
284+ // `has <jsonpath> <value>`: Check` *exactly one* item matched by `jsonpath`, and it equals `value`.
290285 CommandKind :: Is => {
291- // @has <jsonpath> <value> = check *exactly one* item matched by path, and it equals value
292286 assert_eq ! ( command. args. len( ) , 2 ) ;
293287 let val = cache. value ( ) . clone ( ) ;
294288 let results = select ( & val, & command. args [ 0 ] ) . unwrap ( ) ;
@@ -308,16 +302,17 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
308302 is
309303 }
310304 }
305+ // `set <name> = <jsonpath>`
311306 CommandKind :: Set => {
312- // @ set <name> = <jsonpath>
307+ assert ! ( !command . negated , "` set` may not be negated" ) ;
313308 assert_eq ! ( command. args. len( ) , 3 ) ;
314309 assert_eq ! ( command. args[ 1 ] , "=" , "Expected an `=`" ) ;
315310 let val = cache. value ( ) . clone ( ) ;
316311 let results = select ( & val, & command. args [ 2 ] ) . unwrap ( ) ;
317312 assert_eq ! (
318313 results. len( ) ,
319314 1 ,
320- "Expected 1 match for `{}` (because of @ set): matched to {:?}" ,
315+ "Expected 1 match for `{}` (because of ` set` ): matched to {:?}" ,
321316 command. args[ 2 ] ,
322317 results
323318 ) ;
@@ -330,7 +325,7 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
330325 }
331326 _ => {
332327 panic ! (
333- "Got multiple results in `@ set` for `{}`: {:?}" ,
328+ "Got multiple results in `set` for `{}`: {:?}" ,
334329 & command. args[ 2 ] , results,
335330 ) ;
336331 }
@@ -341,18 +336,14 @@ fn check_command(command: Command, cache: &mut Cache) -> Result<(), CkError> {
341336 if result == command. negated {
342337 if command. negated {
343338 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( " " ) ) ,
349340 command,
350341 ) )
351342 } else {
352343 // FIXME: In the future, try 'peeling back' each step, and see at what level the match failed
353344 Err ( CkError :: FailedCheck (
354345 format ! (
355- "`@ {} {}` didn't match when it should" ,
346+ "`{} {}` didn't match when it should" ,
356347 command. kind,
357348 command. args. join( " " )
358349 ) ,
0 commit comments