@@ -35,10 +35,11 @@ macro_rules! function {
35
35
macro_rules! assert_helper {
36
36
// The handling of this pattern-arm of assert_helper
37
37
// is wrapped in a block {} to avoid name collisions
38
- ( condition = $condition: expr, $message: literal, $details: expr, $assert_type: path, $display_type: literal, must_hit = $must_hit: literal) => { {
38
+ ( condition = $condition: expr, $message: literal, $( details = $details : expr) ? , $assert_type: path, $display_type: literal, must_hit = $must_hit: literal) => { {
39
39
// Force evaluation of expressions.
40
40
let condition = $condition;
41
- let details = $details;
41
+ let details = & $crate:: serde_json:: json!( { } ) ;
42
+ $( let details = $details; ) ?
42
43
43
44
$crate:: function!( FUN_NAME ) ;
44
45
@@ -109,16 +110,24 @@ macro_rules! assert_helper {
109
110
/// ```
110
111
#[ macro_export]
111
112
macro_rules! assert_always {
112
- ( $condition: expr, $message: literal, $details: expr) => {
113
+ ( $condition: expr, $message: literal$ ( , $details: expr) ? ) => {
113
114
$crate:: assert_helper!(
114
115
condition = $condition,
115
116
$message,
116
- $details,
117
+ $( details = $details ) ? ,
117
118
$crate:: assert:: AssertType :: Always ,
118
119
"Always" ,
119
120
must_hit = true
120
121
)
121
122
} ;
123
+ ( $( $rest: tt) * ) => {
124
+ :: std:: compile_error!(
125
+ r#"Invalid syntax when calling macro `assert_always`.
126
+ Example usage:
127
+ `assert_always!(condition_expr, "assertion message (static literal)", &details_json_value_expr)`
128
+ "#
129
+ ) ;
130
+ } ;
122
131
}
123
132
124
133
/// Assert that ``condition`` is true every time this function is called. The corresponding test property will pass even if the assertion is never encountered.
@@ -137,16 +146,24 @@ macro_rules! assert_always {
137
146
/// ```
138
147
#[ macro_export]
139
148
macro_rules! assert_always_or_unreachable {
140
- ( $condition: expr, $message: literal, $details: expr) => {
149
+ ( $condition: expr, $message: literal$ ( , $details: expr) ? ) => {
141
150
$crate:: assert_helper!(
142
151
condition = $condition,
143
152
$message,
144
- $details,
153
+ $( details = $details ) ? ,
145
154
$crate:: assert:: AssertType :: Always ,
146
155
"AlwaysOrUnreachable" ,
147
156
must_hit = false
148
157
)
149
158
} ;
159
+ ( $( $rest: tt) * ) => {
160
+ :: std:: compile_error!(
161
+ r#"Invalid syntax when calling macro `assert_always_or_unreachable`.
162
+ Example usage:
163
+ `assert_always_or_unreachable!(condition_expr, "assertion message (static literal)", &details_json_value_expr)`
164
+ "#
165
+ ) ;
166
+ } ;
150
167
}
151
168
152
169
/// Assert that ``condition`` is true at least one time that this function was called.
@@ -166,16 +183,24 @@ macro_rules! assert_always_or_unreachable {
166
183
/// ```
167
184
#[ macro_export]
168
185
macro_rules! assert_sometimes {
169
- ( $condition: expr, $message: literal, $details: expr) => {
186
+ ( $condition: expr, $message: literal$ ( , $details: expr) ? ) => {
170
187
$crate:: assert_helper!(
171
188
condition = $condition,
172
189
$message,
173
- $details,
190
+ $( details = $details ) ? ,
174
191
$crate:: assert:: AssertType :: Sometimes ,
175
192
"Sometimes" ,
176
193
must_hit = true
177
194
)
178
195
} ;
196
+ ( $( $rest: tt) * ) => {
197
+ :: std:: compile_error!(
198
+ r#"Invalid syntax when calling macro `assert_sometimes`.
199
+ Example usage:
200
+ `assert_sometimes!(condition_expr, "assertion message (static literal)", &details_json_value_expr)`
201
+ "#
202
+ ) ;
203
+ } ;
179
204
}
180
205
181
206
/// Assert that a line of code is reached at least once.
@@ -197,16 +222,24 @@ macro_rules! assert_sometimes {
197
222
/// ```
198
223
#[ macro_export]
199
224
macro_rules! assert_reachable {
200
- ( $message: literal, $details: expr) => {
225
+ ( $message: literal$ ( , $details: expr) ? ) => {
201
226
$crate:: assert_helper!(
202
227
condition = true ,
203
228
$message,
204
- $details,
229
+ $( details = $details ) ? ,
205
230
$crate:: assert:: AssertType :: Reachability ,
206
231
"Reachable" ,
207
232
must_hit = true
208
233
)
209
234
} ;
235
+ ( $( $rest: tt) * ) => {
236
+ :: std:: compile_error!(
237
+ r#"Invalid syntax when calling macro `assert_reachable`.
238
+ Example usage:
239
+ `assert_reachable!("assertion message (static literal)", &details_json_value_expr)`
240
+ "#
241
+ ) ;
242
+ } ;
210
243
}
211
244
212
245
/// Assert that a line of code is never reached.
@@ -229,16 +262,24 @@ macro_rules! assert_reachable {
229
262
/// ```
230
263
#[ macro_export]
231
264
macro_rules! assert_unreachable {
232
- ( $message: literal, $details: expr) => {
265
+ ( $message: literal$ ( , $details: expr) ? ) => {
233
266
$crate:: assert_helper!(
234
267
condition = false ,
235
268
$message,
236
- $details,
269
+ $( details = $details ) ? ,
237
270
$crate:: assert:: AssertType :: Reachability ,
238
271
"Unreachable" ,
239
272
must_hit = false
240
273
)
241
274
} ;
275
+ ( $( $rest: tt) * ) => {
276
+ :: std:: compile_error!(
277
+ r#"Invalid syntax when calling macro `assert_unreachable`.
278
+ Example usage:
279
+ `assert_unreachable!("assertion message (static literal)", &details_json_value_expr)`
280
+ "#
281
+ ) ;
282
+ } ;
242
283
}
243
284
244
285
#[ cfg( feature = "full" ) ]
@@ -283,10 +324,12 @@ macro_rules! guidance_helper {
283
324
#[ doc( hidden) ]
284
325
#[ macro_export]
285
326
macro_rules! numeric_guidance_helper {
286
- ( $assert: path, $op: tt, $maximize: literal, $left: expr, $right: expr, $message: literal, $details: expr) => { {
327
+ ( $assert: path, $op: tt, $maximize: literal, $left: expr, $right: expr, $message: literal$ ( , $details: expr) ? ) => { {
287
328
let left = $left;
288
329
let right = $right;
289
- let mut details = $details. clone( ) ;
330
+ let details = & $crate:: serde_json:: json!( { } ) ;
331
+ $( let details = $details; ) ?
332
+ let mut details = details. clone( ) ;
290
333
details[ "left" ] = left. into( ) ;
291
334
details[ "right" ] = right. into( ) ;
292
335
$assert!( left $op right, $message, & details) ;
@@ -324,22 +367,24 @@ macro_rules! numeric_guidance_helper {
324
367
#[ doc( hidden) ]
325
368
#[ macro_export]
326
369
macro_rules! numeric_guidance_helper {
327
- ( $assert: ident, $op: tt, $maximize: literal, $left: expr, $right: expr, $message: literal, $details: expr) => {
328
- assert!( $left $op $right, $message, $details) ;
370
+ ( $assert: ident, $op: tt, $maximize: literal, $left: expr, $right: expr, $message: literal$ ( , $details: expr) ? ) => {
371
+ assert!( $left $op $right, $message$ ( , $details) ? ) ;
329
372
} ;
330
373
}
331
374
332
375
#[ cfg( feature = "full" ) ]
333
376
#[ doc( hidden) ]
334
377
#[ macro_export]
335
378
macro_rules! boolean_guidance_helper {
336
- ( $assert: path, $all: literal, { $( $name: ident: $cond: expr) ,* } , $message: literal, $details: expr) => { {
337
- let mut details = $details. clone( ) ;
379
+ ( $assert: path, $all: literal, { $( $name: ident: $cond: expr) ,* } , $message: literal$( , $details: expr) ?) => { {
380
+ let details = & $crate:: serde_json:: json!( { } ) ;
381
+ $( let details = $details; ) ?
382
+ let mut details = details. clone( ) ;
338
383
let ( cond, guidance_data) = {
339
384
$( let $name = $cond; ) *
340
385
$( details[ :: std:: stringify!( $name) ] = $name. into( ) ; ) *
341
386
(
342
- if $all { $ ( $name) && * } else { $ ( $name) || * } ,
387
+ if $all { true $ ( && $name) * } else { false $ ( || $name) * } ,
343
388
$crate:: serde_json:: json!( { $( :: std:: stringify!( $name) : $name) ,* } )
344
389
)
345
390
} ;
@@ -352,74 +397,138 @@ macro_rules! boolean_guidance_helper {
352
397
#[ doc( hidden) ]
353
398
#[ macro_export]
354
399
macro_rules! boolean_guidance_helper {
355
- ( $assert: path, $all: literal, { $( $name: ident: $cond: expr) ,* } , $message: literal, $details: expr) => { {
356
- let cond = if $all { $ ( $name) && * } else { $ ( $name) || * } ;
357
- $assert!( cond, $message, & details) ;
400
+ ( $assert: path, $all: literal, { $( $name: ident: $cond: expr) ,* } , $message: literal$ ( , $details: expr) ? ) => { {
401
+ let cond = if $all { true $ ( && $name) * } else { false $ ( || $name) * } ,
402
+ $assert!( cond, $message$ ( , & $ details) ? ) ;
358
403
} } ;
359
404
}
360
405
361
406
/// `assert_always_greater_than(x, y, ...)` is mostly equivalent to `assert_always!(x > y, ...)`, except Antithesis has more visibility to the value of `x` and `y`, and the assertion details would be merged with `{"left": x, "right": y}`.
362
407
#[ macro_export]
363
408
macro_rules! assert_always_greater_than {
364
- ( $left: expr, $right: expr, $message: literal, $details: expr) => {
365
- $crate:: numeric_guidance_helper!( $crate:: assert_always, >, false , $left, $right, $message, $details)
409
+ ( $left: expr, $right: expr, $message: literal$( , $details: expr) ?) => {
410
+ $crate:: numeric_guidance_helper!( $crate:: assert_always, >, false , $left, $right, $message$( , $details) ?)
411
+ } ;
412
+ ( $( $rest: tt) * ) => {
413
+ :: std:: compile_error!(
414
+ r#"Invalid syntax when calling macro `assert_always_greater_than`.
415
+ Example usage:
416
+ `assert_always_greater_than!(left_expr, right_expr, "assertion message (static literal)", &details_json_value_expr)`
417
+ "#
418
+ ) ;
366
419
} ;
367
420
}
368
421
369
422
/// `assert_always_greater_than_or_equal_to(x, y, ...)` is mostly equivalent to `assert_always!(x >= y, ...)`, except Antithesis has more visibility to the value of `x` and `y`, and the assertion details would be merged with `{"left": x, "right": y}`.
370
423
#[ macro_export]
371
424
macro_rules! assert_always_greater_than_or_equal_to {
372
- ( $left: expr, $right: expr, $message: literal, $details: expr) => {
425
+ ( $left: expr, $right: expr, $message: literal$ ( , $details: expr) ? ) => {
373
426
$crate:: numeric_guidance_helper!( $crate:: assert_always, >=, false , $left, $right, $message, $details)
374
427
} ;
428
+ ( $( $rest: tt) * ) => {
429
+ :: std:: compile_error!(
430
+ r#"Invalid syntax when calling macro `assert_always_greater_than_or_equal_to`.
431
+ Example usage:
432
+ `assert_always_greater_than_or_equal_to!(left_expr, right_expr, "assertion message (static literal)", &details_json_value_expr)`
433
+ "#
434
+ ) ;
435
+ } ;
375
436
}
376
437
377
438
/// `assert_always_less_than(x, y, ...)` is mostly equivalent to `assert_always!(x < y, ...)`, except Antithesis has more visibility to the value of `x` and `y`, and the assertion details would be merged with `{"left": x, "right": y}`.
378
439
#[ macro_export]
379
440
macro_rules! assert_always_less_than {
380
- ( $left: expr, $right: expr, $message: literal, $details: expr) => {
441
+ ( $left: expr, $right: expr, $message: literal$ ( , $details: expr) ? ) => {
381
442
$crate:: numeric_guidance_helper!( $crate:: assert_always, <, true , $left, $right, $message, $details)
382
443
} ;
444
+ ( $( $rest: tt) * ) => {
445
+ :: std:: compile_error!(
446
+ r#"Invalid syntax when calling macro `assert_always_less_than`.
447
+ Example usage:
448
+ `assert_always_less_than!(left_expr, right_expr, "assertion message (static literal)", &details_json_value_expr)`
449
+ "#
450
+ ) ;
451
+ } ;
383
452
}
384
453
385
454
/// `assert_always_less_than_or_equal_to(x, y, ...)` is mostly equivalent to `assert_always!(x <= y, ...)`, except Antithesis has more visibility to the value of `x` and `y`, and the assertion details would be merged with `{"left": x, "right": y}`.
386
455
#[ macro_export]
387
456
macro_rules! assert_always_less_than_or_equal_to {
388
- ( $left: expr, $right: expr, $message: literal, $details: expr) => {
457
+ ( $left: expr, $right: expr, $message: literal$ ( , $details: expr) ? ) => {
389
458
$crate:: numeric_guidance_helper!( $crate:: assert_always, <=, true , $left, $right, $message, $details)
390
459
} ;
460
+ ( $( $rest: tt) * ) => {
461
+ :: std:: compile_error!(
462
+ r#"Invalid syntax when calling macro `assert_always_less_than_or_equal_to`.
463
+ Example usage:
464
+ `assert_always_less_than_or_equal_to!(left_expr, right_expr, "assertion message (static literal)", &details_json_value_expr)`
465
+ "#
466
+ ) ;
467
+ } ;
391
468
}
392
469
393
470
/// `assert_sometimes_greater_than(x, y, ...)` is mostly equivalent to `assert_sometimes!(x > y, ...)`, except Antithesis has more visibility to the value of `x` and `y`, and the assertion details would be merged with `{"left": x, "right": y}`.
394
471
#[ macro_export]
395
472
macro_rules! assert_sometimes_greater_than {
396
- ( $left: expr, $right: expr, $message: literal, $details: expr) => {
473
+ ( $left: expr, $right: expr, $message: literal$ ( , $details: expr) ? ) => {
397
474
$crate:: numeric_guidance_helper!( $crate:: assert_sometimes, >, true , $left, $right, $message, $details)
398
475
} ;
476
+ ( $( $rest: tt) * ) => {
477
+ :: std:: compile_error!(
478
+ r#"Invalid syntax when calling macro `assert_sometimes_greater_than`.
479
+ Example usage:
480
+ `assert_sometimes_greater_than!(left_expr, right_expr, "assertion message (static literal)", &details_json_value_expr)`
481
+ "#
482
+ ) ;
483
+ } ;
399
484
}
400
485
401
486
/// `assert_sometimes_greater_than_or_equal_to(x, y, ...)` is mostly equivalent to `assert_sometimes!(x >= y, ...)`, except Antithesis has more visibility to the value of `x` and `y`, and the assertion details would be merged with `{"left": x, "right": y}`.
402
487
#[ macro_export]
403
488
macro_rules! assert_sometimes_greater_than_or_equal_to {
404
- ( $left: expr, $right: expr, $message: literal, $details: expr) => {
489
+ ( $left: expr, $right: expr, $message: literal$ ( , $details: expr) ? ) => {
405
490
$crate:: numeric_guidance_helper!( $crate:: assert_sometimes, >=, true , $left, $right, $message, $details)
406
491
} ;
492
+ ( $( $rest: tt) * ) => {
493
+ :: std:: compile_error!(
494
+ r#"Invalid syntax when calling macro `assert_sometimes_greater_than_or_equal_to`.
495
+ Example usage:
496
+ `assert_sometimes_greater_than_or_equal_to!(left_expr, right_expr, "assertion message (static literal)", &details_json_value_expr)`
497
+ "#
498
+ ) ;
499
+ } ;
407
500
}
408
501
409
502
/// `assert_sometimes_less_than(x, y, ...)` is mostly equivalent to `assert_sometimes!(x < y, ...)`, except Antithesis has more visibility to the value of `x` and `y`, and the assertion details would be merged with `{"left": x, "right": y}`.
410
503
#[ macro_export]
411
504
macro_rules! assert_sometimes_less_than {
412
- ( $left: expr, $right: expr, $message: literal, $details: expr) => {
505
+ ( $left: expr, $right: expr, $message: literal$ ( , $details: expr) ? ) => {
413
506
$crate:: numeric_guidance_helper!( $crate:: assert_sometimes, <, false , $left, $right, $message, $details)
414
507
} ;
508
+ ( $( $rest: tt) * ) => {
509
+ :: std:: compile_error!(
510
+ r#"Invalid syntax when calling macro `assert_sometimes_less_than`.
511
+ Example usage:
512
+ `assert_sometimes_less_than!(left_expr, right_expr, "assertion message (static literal)", &details_json_value_expr)`
513
+ "#
514
+ ) ;
515
+ } ;
415
516
}
416
517
417
518
/// `assert_sometimes_less_than_or_equal_to(x, y, ...)` is mostly equivalent to `assert_sometimes!(x <= y, ...)`, except Antithesis has more visibility to the value of `x` and `y`, and the assertion details would be merged with `{"left": x, "right": y}`.
418
519
#[ macro_export]
419
520
macro_rules! assert_sometimes_less_than_or_equal_to {
420
- ( $left: expr, $right: expr, $message: literal, $details: expr) => {
521
+ ( $left: expr, $right: expr, $message: literal$ ( , $details: expr) ? ) => {
421
522
$crate:: numeric_guidance_helper!( $crate:: assert_sometimes, <=, false , $left, $right, $message, $details)
422
523
} ;
524
+ ( $( $rest: tt) * ) => {
525
+ :: std:: compile_error!(
526
+ r#"Invalid syntax when calling macro `assert_sometimes_less_than_or_equal_to`.
527
+ Example usage:
528
+ `assert_sometimes_less_than_or_equal_to!(left_expr, right_expr, "assertion message (static literal)", &details_json_value_expr)`
529
+ "#
530
+ ) ;
531
+ } ;
423
532
}
424
533
425
534
/// `assert_always_some({a: x, b: y, ...})` is similar to `assert_always(x || y || ...)`, except:
@@ -428,9 +537,17 @@ macro_rules! assert_sometimes_less_than_or_equal_to {
428
537
/// - The assertion details would be merged with `{"a": x, "b": y, ...}`.
429
538
#[ macro_export]
430
539
macro_rules! assert_always_some {
431
- ( { $( $name: ident: $cond: expr) ,* } , $message: literal, $details: expr) => {
432
- $crate:: boolean_guidance_helper!( $crate:: assert_always, false , { $( $name: $cond) ,* } , $message, $details) ;
433
- }
540
+ ( { $( $( $name: ident: $cond: expr) ,+ $( , ) ?) ?} , $message: literal$( , $details: expr) ?) => {
541
+ $crate:: boolean_guidance_helper!( $crate:: assert_always, false , { $( $( $name: $cond) ,+) ?} , $message$( , $details) ?) ;
542
+ } ;
543
+ ( $( $rest: tt) * ) => {
544
+ :: std:: compile_error!(
545
+ r#"Invalid syntax when calling macro `assert_always_some`.
546
+ Example usage:
547
+ `assert_always_some!({field1: cond1, field2: cond2, ...}, "assertion message (static literal)", &details_json_value_expr)`
548
+ "#
549
+ ) ;
550
+ } ;
434
551
}
435
552
436
553
/// `assert_sometimes_all({a: x, b: y, ...})` is similar to `assert_sometimes(x && y && ...)`, except:
@@ -439,7 +556,15 @@ macro_rules! assert_always_some {
439
556
/// - The assertion details would be merged with `{"a": x, "b": y, ...}`.
440
557
#[ macro_export]
441
558
macro_rules! assert_sometimes_all {
442
- ( { $( $name: ident: $cond: expr) ,* } , $message: literal, $details: expr) => {
443
- $crate:: boolean_guidance_helper!( $crate:: assert_sometimes, true , { $( $name: $cond) ,* } , $message, $details) ;
444
- }
559
+ ( { $( $( $name: ident: $cond: expr) ,+ $( , ) ?) ?} , $message: literal$( , $details: expr) ?) => {
560
+ $crate:: boolean_guidance_helper!( $crate:: assert_sometimes, true , { $( $( $name: $cond) ,+) ?} , $message$( , $details) ?) ;
561
+ } ;
562
+ ( $( $rest: tt) * ) => {
563
+ :: std:: compile_error!(
564
+ r#"Invalid syntax when calling macro `assert_sometimes_all`.
565
+ Example usage:
566
+ `assert_sometimes_all!({field1: cond1, field2: cond2, ...}, "assertion message (static literal)", &details_json_value_expr)`
567
+ "#
568
+ ) ;
569
+ } ;
445
570
}
0 commit comments