Skip to content

Commit 3c755be

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 3afda7e commit 3c755be

3 files changed

Lines changed: 117 additions & 111 deletions

File tree

src/macros.rs

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::fmt::Arguments;
44

55
pub fn make_err(fmt: Arguments) -> anyerror::AnyError {
6-
anyerror::AnyError::error(format!("{}", fmt))
6+
anyerror::AnyError::error(format!("{fmt}"))
77
}
88

99
/// Assert that function call `call(a,b,...)`(up to 8 arguments) to return true, otherwise it return
@@ -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,13 @@ 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!(),
302+
line!(),
293303
)))?;
294304
}
295305
}};
@@ -306,7 +316,7 @@ macro_rules! less {
306316
/// Ok(())
307317
/// }
308318
/// assert!(expect_greater(2,1).is_ok());
309-
/// assert_eq!("expect: a(2) > b(2)", expect_greater(2,2).unwrap_err().to_string());
319+
/// assert!(expect_greater(2,2).unwrap_err().to_string().starts_with("expect: a(2) > b(2) at "));
310320
/// ```
311321
#[macro_export]
312322
macro_rules! greater {
@@ -317,12 +327,13 @@ macro_rules! greater {
317327
// Ok
318328
} else {
319329
Err($crate::macros::make_err(format_args!(
320-
"expect: {}({:?}) {} {}({:?})",
330+
"expect: {}({:?}) > {}({:?}) at {}:{}",
321331
stringify!($a),
322332
a,
323-
">",
324333
stringify!($b),
325334
b,
335+
file!(),
336+
line!(),
326337
)))?;
327338
}
328339
}};
@@ -339,7 +350,7 @@ macro_rules! greater {
339350
/// Ok(())
340351
/// }
341352
/// 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());
353+
/// assert!(expect_less_equal(3,2).unwrap_err().to_string().starts_with("expect: a(3) <= b(2) at "));
343354
/// ```
344355
#[macro_export]
345356
macro_rules! less_equal {
@@ -350,12 +361,13 @@ macro_rules! less_equal {
350361
// Ok
351362
} else {
352363
Err($crate::macros::make_err(format_args!(
353-
"expect: {}({:?}) {} {}({:?})",
364+
"expect: {}({:?}) <= {}({:?}) at {}:{}",
354365
stringify!($a),
355366
a,
356-
"<=",
357367
stringify!($b),
358368
b,
369+
file!(),
370+
line!(),
359371
)))?;
360372
}
361373
}};
@@ -372,7 +384,7 @@ macro_rules! less_equal {
372384
/// Ok(())
373385
/// }
374386
/// 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());
387+
/// assert!(expect_greater_equal(2,3).unwrap_err().to_string().starts_with("expect: a(2) >= b(3) at "));
376388
/// ```
377389
#[macro_export]
378390
macro_rules! greater_equal {
@@ -383,12 +395,13 @@ macro_rules! greater_equal {
383395
// Ok
384396
} else {
385397
Err($crate::macros::make_err(format_args!(
386-
"expect: {}({:?}) {} {}({:?})",
398+
"expect: {}({:?}) >= {}({:?}) at {}:{}",
387399
stringify!($a),
388400
a,
389-
">=",
390401
stringify!($b),
391402
b,
403+
file!(),
404+
line!(),
392405
)))?;
393406
}
394407
}};
@@ -405,7 +418,7 @@ macro_rules! greater_equal {
405418
/// Ok(())
406419
/// }
407420
/// assert!(expect_equal(2,2).is_ok());
408-
/// assert_eq!("expect: a(3) == b(2)", expect_equal(3,2).unwrap_err().to_string());
421+
/// assert!(expect_equal(3,2).unwrap_err().to_string().starts_with("expect: a(3) == b(2) at "));
409422
/// ```
410423
#[macro_export]
411424
macro_rules! equal {
@@ -416,12 +429,13 @@ macro_rules! equal {
416429
// Ok
417430
} else {
418431
Err($crate::macros::make_err(format_args!(
419-
"expect: {}({:?}) {} {}({:?})",
432+
"expect: {}({:?}) == {}({:?}) at {}:{}",
420433
stringify!($a),
421434
a,
422-
"==",
423435
stringify!($b),
424436
b,
437+
file!(),
438+
line!(),
425439
)))?;
426440
}
427441
}};

0 commit comments

Comments
 (0)