Skip to content

Commit 8829954

Browse files
committed
feat: add source location to validation error messages
Include `file!()` and `line!()` in all validation macro error messages to help identify exactly where validation failures occur. Changes: - Add source location suffix `at file:line` to all `be_true!` macro variants - Add source location to `less!`, `greater!`, `less_equal!`, `greater_equal!`, `equal!` macros - Update tests to use `starts_with()` assertions for location-independent matching
1 parent dcae2f2 commit 8829954

2 files changed

Lines changed: 107 additions & 108 deletions

File tree

src/macros.rs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn make_err(fmt: Arguments) -> anyerror::AnyError {
1919
/// Ok(())
2020
/// }
2121
/// assert!(expect_true(1,2).is_ok());
22-
/// assert_eq!("expect to be true: le(a(3), b(2))", expect_true(3,2).unwrap_err().to_string());
22+
/// assert!(expect_true(3,2).unwrap_err().to_string().starts_with("expect to be true: le(a(3), b(2)) at "));
2323
/// ```
2424
///
2525
/// Another example with 3 arguments:
@@ -32,7 +32,7 @@ pub fn make_err(fmt: Arguments) -> anyerror::AnyError {
3232
/// Ok(())
3333
/// }
3434
/// assert!(expect_true3(1,10).is_ok());
35-
/// assert_eq!("expect to be true: mid(a(6), 5(5), b(10))", expect_true3(6,10).unwrap_err().to_string());
35+
/// assert!(expect_true3(6,10).unwrap_err().to_string().starts_with("expect to be true: mid(a(6), 5(5), b(10)) at "));
3636
/// ```
3737
#[macro_export]
3838
macro_rules! be_true {
@@ -43,8 +43,9 @@ macro_rules! be_true {
4343
// Ok
4444
} else {
4545
Err($crate::macros::make_err(format_args!(
46-
"expect to be true: {}()",
47-
stringify!($($call).+)
46+
"expect to be true: {}() at {}:{}",
47+
stringify!($($call).+),
48+
file!(), line!(),
4849
)))?;
4950
}
5051
}};
@@ -57,10 +58,11 @@ macro_rules! be_true {
5758
// Ok
5859
} else {
5960
Err($crate::macros::make_err(format_args!(
60-
"expect to be true: {}({}({:?}))",
61+
"expect to be true: {}({}({:?})) at {}:{}",
6162
stringify!($($call).+),
6263
stringify!($a),
6364
__a,
65+
file!(), line!(),
6466
)))?;
6567
}
6668
}};
@@ -74,12 +76,13 @@ macro_rules! be_true {
7476
// Ok
7577
} else {
7678
Err($crate::macros::make_err(format_args!(
77-
"expect to be true: {}({}({:?}), {}({:?}))",
79+
"expect to be true: {}({}({:?}), {}({:?})) at {}:{}",
7880
stringify!($($call).+),
7981
stringify!($a),
8082
__a,
8183
stringify!($b),
8284
__b,
85+
file!(), line!(),
8386
)))?;
8487
}
8588
}};
@@ -94,14 +97,15 @@ macro_rules! be_true {
9497
// Ok
9598
} else {
9699
Err($crate::macros::make_err(format_args!(
97-
"expect to be true: {}({}({:?}), {}({:?}), {}({:?}))",
100+
"expect to be true: {}({}({:?}), {}({:?}), {}({:?})) at {}:{}",
98101
stringify!($($call).+),
99102
stringify!($a),
100103
__a,
101104
stringify!($b),
102105
__b,
103106
stringify!($c),
104107
__c,
108+
file!(), line!(),
105109
)))?;
106110
}
107111
}};
@@ -117,7 +121,7 @@ macro_rules! be_true {
117121
// Ok
118122
} else {
119123
Err($crate::macros::make_err(format_args!(
120-
"expect to be true: {}({}({:?}), {}({:?}), {}({:?}), {}({:?}))",
124+
"expect to be true: {}({}({:?}), {}({:?}), {}({:?}), {}({:?})) at {}:{}",
121125
stringify!($($call).+),
122126
stringify!($a),
123127
__a,
@@ -127,6 +131,7 @@ macro_rules! be_true {
127131
__c,
128132
stringify!($d),
129133
__d,
134+
file!(), line!(),
130135
)))?;
131136
}
132137
}};
@@ -143,7 +148,7 @@ macro_rules! be_true {
143148
// Ok
144149
} else {
145150
Err($crate::macros::make_err(format_args!(
146-
"expect to be true: {}({}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}))",
151+
"expect to be true: {}({}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?})) at {}:{}",
147152
stringify!($($call).+),
148153
stringify!($a),
149154
__a,
@@ -155,6 +160,7 @@ macro_rules! be_true {
155160
__d,
156161
stringify!($e),
157162
__e,
163+
file!(), line!(),
158164
)))?;
159165
}
160166
}};
@@ -172,7 +178,7 @@ macro_rules! be_true {
172178
// Ok
173179
} else {
174180
Err($crate::macros::make_err(format_args!(
175-
"expect to be true: {}({}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}))",
181+
"expect to be true: {}({}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?})) at {}:{}",
176182
stringify!($($call).+),
177183
stringify!($a),
178184
__a,
@@ -186,6 +192,7 @@ macro_rules! be_true {
186192
__e,
187193
stringify!($f),
188194
__f,
195+
file!(), line!(),
189196
)))?;
190197
}
191198
}};
@@ -204,7 +211,7 @@ macro_rules! be_true {
204211
// Ok
205212
} else {
206213
Err($crate::macros::make_err(format_args!(
207-
"expect to be true: {}({}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}))",
214+
"expect to be true: {}({}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?})) at {}:{}",
208215
stringify!($($call).+),
209216
stringify!($a),
210217
__a,
@@ -220,6 +227,7 @@ macro_rules! be_true {
220227
__f,
221228
stringify!($g),
222229
__g,
230+
file!(), line!(),
223231
)))?;
224232
}
225233
}};
@@ -239,7 +247,7 @@ macro_rules! be_true {
239247
// Ok
240248
} else {
241249
Err($crate::macros::make_err(format_args!(
242-
"expect to be true: {}({}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}))",
250+
"expect to be true: {}({}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?}), {}({:?})) at {}:{}",
243251
stringify!($($call).+),
244252
stringify!($a),
245253
__a,
@@ -257,6 +265,7 @@ macro_rules! be_true {
257265
__g,
258266
stringify!($h),
259267
__h,
268+
file!(), line!(),
260269
)))?;
261270
}
262271
}};
@@ -273,7 +282,7 @@ macro_rules! be_true {
273282
/// Ok(())
274283
/// }
275284
/// assert!(expect_less(1,2).is_ok());
276-
/// assert_eq!("expect: a(2) < b(2)", expect_less(2,2).unwrap_err().to_string());
285+
/// assert!(expect_less(2,2).unwrap_err().to_string().starts_with("expect: a(2) < b(2) at "));
277286
/// ```
278287
#[macro_export]
279288
macro_rules! less {
@@ -284,12 +293,12 @@ macro_rules! less {
284293
// Ok
285294
} else {
286295
Err($crate::macros::make_err(format_args!(
287-
"expect: {}({:?}) {} {}({:?})",
296+
"expect: {}({:?}) < {}({:?}) at {}:{}",
288297
stringify!($a),
289298
a,
290-
"<",
291299
stringify!($b),
292300
b,
301+
file!(), line!(),
293302
)))?;
294303
}
295304
}};
@@ -306,7 +315,7 @@ macro_rules! less {
306315
/// Ok(())
307316
/// }
308317
/// assert!(expect_greater(2,1).is_ok());
309-
/// assert_eq!("expect: a(2) > b(2)", expect_greater(2,2).unwrap_err().to_string());
318+
/// assert!(expect_greater(2,2).unwrap_err().to_string().starts_with("expect: a(2) > b(2) at "));
310319
/// ```
311320
#[macro_export]
312321
macro_rules! greater {
@@ -317,12 +326,12 @@ macro_rules! greater {
317326
// Ok
318327
} else {
319328
Err($crate::macros::make_err(format_args!(
320-
"expect: {}({:?}) {} {}({:?})",
329+
"expect: {}({:?}) > {}({:?}) at {}:{}",
321330
stringify!($a),
322331
a,
323-
">",
324332
stringify!($b),
325333
b,
334+
file!(), line!(),
326335
)))?;
327336
}
328337
}};
@@ -339,7 +348,7 @@ macro_rules! greater {
339348
/// Ok(())
340349
/// }
341350
/// assert!(expect_less_equal(2,2).is_ok());
342-
/// assert_eq!("expect: a(3) <= b(2)", expect_less_equal(3,2).unwrap_err().to_string());
351+
/// assert!(expect_less_equal(3,2).unwrap_err().to_string().starts_with("expect: a(3) <= b(2) at "));
343352
/// ```
344353
#[macro_export]
345354
macro_rules! less_equal {
@@ -350,12 +359,12 @@ macro_rules! less_equal {
350359
// Ok
351360
} else {
352361
Err($crate::macros::make_err(format_args!(
353-
"expect: {}({:?}) {} {}({:?})",
362+
"expect: {}({:?}) <= {}({:?}) at {}:{}",
354363
stringify!($a),
355364
a,
356-
"<=",
357365
stringify!($b),
358366
b,
367+
file!(), line!(),
359368
)))?;
360369
}
361370
}};
@@ -372,7 +381,7 @@ macro_rules! less_equal {
372381
/// Ok(())
373382
/// }
374383
/// assert!(expect_greater_equal(2,2).is_ok());
375-
/// assert_eq!("expect: a(2) >= b(3)", expect_greater_equal(2,3).unwrap_err().to_string());
384+
/// assert!(expect_greater_equal(2,3).unwrap_err().to_string().starts_with("expect: a(2) >= b(3) at "));
376385
/// ```
377386
#[macro_export]
378387
macro_rules! greater_equal {
@@ -383,12 +392,12 @@ macro_rules! greater_equal {
383392
// Ok
384393
} else {
385394
Err($crate::macros::make_err(format_args!(
386-
"expect: {}({:?}) {} {}({:?})",
395+
"expect: {}({:?}) >= {}({:?}) at {}:{}",
387396
stringify!($a),
388397
a,
389-
">=",
390398
stringify!($b),
391399
b,
400+
file!(), line!(),
392401
)))?;
393402
}
394403
}};
@@ -405,7 +414,7 @@ macro_rules! greater_equal {
405414
/// Ok(())
406415
/// }
407416
/// assert!(expect_equal(2,2).is_ok());
408-
/// assert_eq!("expect: a(3) == b(2)", expect_equal(3,2).unwrap_err().to_string());
417+
/// assert!(expect_equal(3,2).unwrap_err().to_string().starts_with("expect: a(3) == b(2) at "));
409418
/// ```
410419
#[macro_export]
411420
macro_rules! equal {
@@ -416,12 +425,12 @@ macro_rules! equal {
416425
// Ok
417426
} else {
418427
Err($crate::macros::make_err(format_args!(
419-
"expect: {}({:?}) {} {}({:?})",
428+
"expect: {}({:?}) == {}({:?}) at {}:{}",
420429
stringify!($a),
421430
a,
422-
"==",
423431
stringify!($b),
424432
b,
433+
file!(), line!(),
425434
)))?;
426435
}
427436
}};

0 commit comments

Comments
 (0)