@@ -75,9 +75,9 @@ const TEST_WARN_TIMEOUT_S: u64 = 60;
75
75
// to be used by rustc to compile tests in libtest
76
76
pub mod test {
77
77
pub use { Bencher , TestName , TestResult , TestDesc , TestDescAndFn , TestOpts , TrFailed ,
78
- TrIgnored , TrOk , Metric , MetricMap , StaticTestFn , StaticTestName , DynTestName ,
79
- DynTestFn , run_test, test_main, test_main_static, filter_tests, parse_opts ,
80
- StaticBenchFn , ShouldPanic } ;
78
+ TrFailedMsg , TrIgnored , TrOk , Metric , MetricMap , StaticTestFn , StaticTestName ,
79
+ DynTestName , DynTestFn , run_test, test_main, test_main_static, filter_tests,
80
+ parse_opts , StaticBenchFn , ShouldPanic } ;
81
81
}
82
82
83
83
pub mod stats;
@@ -473,6 +473,7 @@ pub struct BenchSamples {
473
473
pub enum TestResult {
474
474
TrOk ,
475
475
TrFailed ,
476
+ TrFailedMsg ( String ) ,
476
477
TrIgnored ,
477
478
TrMetrics ( MetricMap ) ,
478
479
TrBench ( BenchSamples ) ,
@@ -611,7 +612,7 @@ impl<T: Write> ConsoleTestState<T> {
611
612
pub fn write_result ( & mut self , result : & TestResult ) -> io:: Result < ( ) > {
612
613
match * result {
613
614
TrOk => self . write_ok ( ) ,
614
- TrFailed => self . write_failed ( ) ,
615
+ TrFailed | TrFailedMsg ( _ ) => self . write_failed ( ) ,
615
616
TrIgnored => self . write_ignored ( ) ,
616
617
TrMetrics ( ref mm) => {
617
618
self . write_metric ( ) ?;
@@ -638,6 +639,7 @@ impl<T: Write> ConsoleTestState<T> {
638
639
match * result {
639
640
TrOk => "ok" . to_owned( ) ,
640
641
TrFailed => "failed" . to_owned( ) ,
642
+ TrFailedMsg ( ref msg) => format!( "failed: {}" , msg) ,
641
643
TrIgnored => "ignored" . to_owned( ) ,
642
644
TrMetrics ( ref mm) => mm. fmt_metrics( ) ,
643
645
TrBench ( ref bs) => fmt_bench_samples( bs) ,
@@ -773,6 +775,14 @@ pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Resu
773
775
st. failed += 1 ;
774
776
st. failures . push ( ( test, stdout) ) ;
775
777
}
778
+ TrFailedMsg ( msg) => {
779
+ st. failed += 1 ;
780
+ let mut stdout = stdout;
781
+ stdout. extend_from_slice (
782
+ format ! ( "note: {}" , msg) . as_bytes ( )
783
+ ) ;
784
+ st. failures . push ( ( test, stdout) ) ;
785
+ }
776
786
}
777
787
Ok ( ( ) )
778
788
}
@@ -1270,12 +1280,16 @@ fn calc_result(desc: &TestDesc, task_result: Result<(), Box<Any + Send>>) -> Tes
1270
1280
match ( & desc. should_panic , task_result) {
1271
1281
( & ShouldPanic :: No , Ok ( ( ) ) ) |
1272
1282
( & ShouldPanic :: Yes , Err ( _) ) => TrOk ,
1273
- ( & ShouldPanic :: YesWithMessage ( msg) , Err ( ref err) )
1283
+ ( & ShouldPanic :: YesWithMessage ( msg) , Err ( ref err) ) =>
1274
1284
if err. downcast_ref :: < String > ( )
1275
- . map ( |e| & * * e)
1276
- . or_else ( || err. downcast_ref :: < & ' static str > ( ) . map ( |e| * e) )
1277
- . map ( |e| e. contains ( msg) )
1278
- . unwrap_or ( false ) => TrOk ,
1285
+ . map ( |e| & * * e)
1286
+ . or_else ( || err. downcast_ref :: < & ' static str > ( ) . map ( |e| * e) )
1287
+ . map ( |e| e. contains ( msg) )
1288
+ . unwrap_or ( false ) {
1289
+ TrOk
1290
+ } else {
1291
+ TrFailedMsg ( format ! ( "Panic did not include expected string '{}'" , msg) )
1292
+ } ,
1279
1293
_ => TrFailed ,
1280
1294
}
1281
1295
}
@@ -1482,8 +1496,9 @@ pub mod bench {
1482
1496
1483
1497
#[ cfg( test) ]
1484
1498
mod tests {
1485
- use test:: { TrFailed , TrIgnored , TrOk , filter_tests, parse_opts, TestDesc , TestDescAndFn ,
1486
- TestOpts , run_test, MetricMap , StaticTestName , DynTestName , DynTestFn , ShouldPanic } ;
1499
+ use test:: { TrFailed , TrFailedMsg , TrIgnored , TrOk , filter_tests, parse_opts, TestDesc ,
1500
+ TestDescAndFn , TestOpts , run_test, MetricMap , StaticTestName , DynTestName ,
1501
+ DynTestFn , ShouldPanic } ;
1487
1502
use std:: sync:: mpsc:: channel;
1488
1503
1489
1504
#[ test]
@@ -1565,18 +1580,20 @@ mod tests {
1565
1580
fn f ( ) {
1566
1581
panic ! ( "an error message" ) ;
1567
1582
}
1583
+ let expected = "foobar" ;
1584
+ let failed_msg = "Panic did not include expected string" ;
1568
1585
let desc = TestDescAndFn {
1569
1586
desc : TestDesc {
1570
1587
name : StaticTestName ( "whatever" ) ,
1571
1588
ignore : false ,
1572
- should_panic : ShouldPanic :: YesWithMessage ( "foobar" ) ,
1589
+ should_panic : ShouldPanic :: YesWithMessage ( expected ) ,
1573
1590
} ,
1574
1591
testfn : DynTestFn ( Box :: new ( move |( ) | f ( ) ) ) ,
1575
1592
} ;
1576
1593
let ( tx, rx) = channel ( ) ;
1577
1594
run_test ( & TestOpts :: new ( ) , false , desc, tx) ;
1578
1595
let ( _, res, _) = rx. recv ( ) . unwrap ( ) ;
1579
- assert ! ( res == TrFailed ) ;
1596
+ assert ! ( res == TrFailedMsg ( format! ( "{} '{}'" , failed_msg , expected ) ) ) ;
1580
1597
}
1581
1598
1582
1599
#[ test]
0 commit comments