3 filter_tests, parse_opts, run_test, DynTestFn, DynTestName, MetricMap, RunIgnored,
4 ShouldPanic, StaticTestName, TestDesc, TestDescAndFn, TestOpts, TrFailed, TrFailedMsg,
9 use std::sync::mpsc::channel;
11 fn one_ignored_one_unignored_test() -> Vec<TestDescAndFn> {
15 name: StaticTestName("1"),
17 should_panic: ShouldPanic::No,
20 testfn: DynTestFn(Box::new(move || {})),
24 name: StaticTestName("2"),
26 should_panic: ShouldPanic::No,
29 testfn: DynTestFn(Box::new(move || {})),
35 pub fn do_not_run_ignored_tests() {
39 let desc = TestDescAndFn {
41 name: StaticTestName("whatever"),
43 should_panic: ShouldPanic::No,
46 testfn: DynTestFn(Box::new(f)),
48 let (tx, rx) = channel();
49 run_test(&TestOpts::new(), false, desc, tx, Concurrent::No);
50 let (_, res, _) = rx.recv().unwrap();
55 pub fn ignored_tests_result_in_ignored() {
57 let desc = TestDescAndFn {
59 name: StaticTestName("whatever"),
61 should_panic: ShouldPanic::No,
64 testfn: DynTestFn(Box::new(f)),
66 let (tx, rx) = channel();
67 run_test(&TestOpts::new(), false, desc, tx, Concurrent::No);
68 let (_, res, _) = rx.recv().unwrap();
69 assert!(res == TrIgnored);
73 fn test_should_panic() {
77 let desc = TestDescAndFn {
79 name: StaticTestName("whatever"),
81 should_panic: ShouldPanic::Yes,
84 testfn: DynTestFn(Box::new(f)),
86 let (tx, rx) = channel();
87 run_test(&TestOpts::new(), false, desc, tx, Concurrent::No);
88 let (_, res, _) = rx.recv().unwrap();
93 fn test_should_panic_good_message() {
95 panic!("an error message");
97 let desc = TestDescAndFn {
99 name: StaticTestName("whatever"),
101 should_panic: ShouldPanic::YesWithMessage("error message"),
104 testfn: DynTestFn(Box::new(f)),
106 let (tx, rx) = channel();
107 run_test(&TestOpts::new(), false, desc, tx, Concurrent::No);
108 let (_, res, _) = rx.recv().unwrap();
109 assert!(res == TrOk);
113 fn test_should_panic_bad_message() {
115 panic!("an error message");
117 let expected = "foobar";
118 let failed_msg = "panic did not include expected string";
119 let desc = TestDescAndFn {
121 name: StaticTestName("whatever"),
123 should_panic: ShouldPanic::YesWithMessage(expected),
126 testfn: DynTestFn(Box::new(f)),
128 let (tx, rx) = channel();
129 run_test(&TestOpts::new(), false, desc, tx, Concurrent::No);
130 let (_, res, _) = rx.recv().unwrap();
131 assert!(res == TrFailedMsg(format!("{} '{}'", failed_msg, expected)));
135 fn test_should_panic_but_succeeds() {
137 let desc = TestDescAndFn {
139 name: StaticTestName("whatever"),
141 should_panic: ShouldPanic::Yes,
144 testfn: DynTestFn(Box::new(f)),
146 let (tx, rx) = channel();
147 run_test(&TestOpts::new(), false, desc, tx, Concurrent::No);
148 let (_, res, _) = rx.recv().unwrap();
149 assert!(res == TrFailed);
153 fn parse_ignored_flag() {
155 "progname".to_string(),
156 "filter".to_string(),
157 "--ignored".to_string(),
159 let opts = parse_opts(&args).unwrap().unwrap();
160 assert_eq!(opts.run_ignored, RunIgnored::Only);
164 fn parse_include_ignored_flag() {
166 "progname".to_string(),
167 "filter".to_string(),
168 "-Zunstable-options".to_string(),
169 "--include-ignored".to_string(),
171 let opts = parse_opts(&args).unwrap().unwrap();
172 assert_eq!(opts.run_ignored, RunIgnored::Yes);
176 pub fn filter_for_ignored_option() {
177 // When we run ignored tests the test filter should filter out all the
178 // unignored tests and flip the ignore flag on the rest to false
180 let mut opts = TestOpts::new();
181 opts.run_tests = true;
182 opts.run_ignored = RunIgnored::Only;
184 let tests = one_ignored_one_unignored_test();
185 let filtered = filter_tests(&opts, tests);
187 assert_eq!(filtered.len(), 1);
188 assert_eq!(filtered[0].desc.name.to_string(), "1");
189 assert!(!filtered[0].desc.ignore);
193 pub fn run_include_ignored_option() {
194 // When we "--include-ignored" tests, the ignore flag should be set to false on
195 // all tests and no test filtered out
197 let mut opts = TestOpts::new();
198 opts.run_tests = true;
199 opts.run_ignored = RunIgnored::Yes;
201 let tests = one_ignored_one_unignored_test();
202 let filtered = filter_tests(&opts, tests);
204 assert_eq!(filtered.len(), 2);
205 assert!(!filtered[0].desc.ignore);
206 assert!(!filtered[1].desc.ignore);
210 pub fn exclude_should_panic_option() {
211 let mut opts = TestOpts::new();
212 opts.run_tests = true;
213 opts.exclude_should_panic = true;
215 let mut tests = one_ignored_one_unignored_test();
216 tests.push(TestDescAndFn {
218 name: StaticTestName("3"),
220 should_panic: ShouldPanic::Yes,
223 testfn: DynTestFn(Box::new(move || {})),
226 let filtered = filter_tests(&opts, tests);
228 assert_eq!(filtered.len(), 2);
229 assert!(filtered.iter().all(|test| test.desc.should_panic == ShouldPanic::No));
233 pub fn exact_filter_match() {
234 fn tests() -> Vec<TestDescAndFn> {
235 vec!["base", "base::test", "base::test1", "base::test2"]
237 .map(|name| TestDescAndFn {
239 name: StaticTestName(name),
241 should_panic: ShouldPanic::No,
244 testfn: DynTestFn(Box::new(move || {})),
249 let substr = filter_tests(
251 filter: Some("base".into()),
256 assert_eq!(substr.len(), 4);
258 let substr = filter_tests(
260 filter: Some("bas".into()),
265 assert_eq!(substr.len(), 4);
267 let substr = filter_tests(
269 filter: Some("::test".into()),
274 assert_eq!(substr.len(), 3);
276 let substr = filter_tests(
278 filter: Some("base::test".into()),
283 assert_eq!(substr.len(), 3);
285 let exact = filter_tests(
287 filter: Some("base".into()),
293 assert_eq!(exact.len(), 1);
295 let exact = filter_tests(
297 filter: Some("bas".into()),
303 assert_eq!(exact.len(), 0);
305 let exact = filter_tests(
307 filter: Some("::test".into()),
313 assert_eq!(exact.len(), 0);
315 let exact = filter_tests(
317 filter: Some("base::test".into()),
323 assert_eq!(exact.len(), 1);
327 pub fn sort_tests() {
328 let mut opts = TestOpts::new();
329 opts.run_tests = true;
332 "sha1::test".to_string(),
333 "isize::test_to_str".to_string(),
334 "isize::test_pow".to_string(),
335 "test::do_not_run_ignored_tests".to_string(),
336 "test::ignored_tests_result_in_ignored".to_string(),
337 "test::first_free_arg_should_be_a_filter".to_string(),
338 "test::parse_ignored_flag".to_string(),
339 "test::parse_include_ignored_flag".to_string(),
340 "test::filter_for_ignored_option".to_string(),
341 "test::run_include_ignored_option".to_string(),
342 "test::sort_tests".to_string(),
346 let mut tests = Vec::new();
348 let test = TestDescAndFn {
350 name: DynTestName((*name).clone()),
352 should_panic: ShouldPanic::No,
355 testfn: DynTestFn(Box::new(testfn)),
361 let filtered = filter_tests(&opts, tests);
364 "isize::test_pow".to_string(),
365 "isize::test_to_str".to_string(),
366 "sha1::test".to_string(),
367 "test::do_not_run_ignored_tests".to_string(),
368 "test::filter_for_ignored_option".to_string(),
369 "test::first_free_arg_should_be_a_filter".to_string(),
370 "test::ignored_tests_result_in_ignored".to_string(),
371 "test::parse_ignored_flag".to_string(),
372 "test::parse_include_ignored_flag".to_string(),
373 "test::run_include_ignored_option".to_string(),
374 "test::sort_tests".to_string(),
377 for (a, b) in expected.iter().zip(filtered) {
378 assert!(*a == b.desc.name.to_string());
383 pub fn test_metricmap_compare() {
384 let mut m1 = MetricMap::new();
385 let mut m2 = MetricMap::new();
386 m1.insert_metric("in-both-noise", 1000.0, 200.0);
387 m2.insert_metric("in-both-noise", 1100.0, 200.0);
389 m1.insert_metric("in-first-noise", 1000.0, 2.0);
390 m2.insert_metric("in-second-noise", 1000.0, 2.0);
392 m1.insert_metric("in-both-want-downwards-but-regressed", 1000.0, 10.0);
393 m2.insert_metric("in-both-want-downwards-but-regressed", 2000.0, 10.0);
395 m1.insert_metric("in-both-want-downwards-and-improved", 2000.0, 10.0);
396 m2.insert_metric("in-both-want-downwards-and-improved", 1000.0, 10.0);
398 m1.insert_metric("in-both-want-upwards-but-regressed", 2000.0, -10.0);
399 m2.insert_metric("in-both-want-upwards-but-regressed", 1000.0, -10.0);
401 m1.insert_metric("in-both-want-upwards-and-improved", 1000.0, -10.0);
402 m2.insert_metric("in-both-want-upwards-and-improved", 2000.0, -10.0);
406 pub fn test_bench_once_no_iter() {
407 fn f(_: &mut Bencher) {}
412 pub fn test_bench_once_iter() {
413 fn f(b: &mut Bencher) {
420 pub fn test_bench_no_iter() {
421 fn f(_: &mut Bencher) {}
423 let (tx, rx) = channel();
425 let desc = TestDesc {
426 name: StaticTestName("f"),
428 should_panic: ShouldPanic::No,
432 crate::bench::benchmark(desc, tx, true, f);
437 pub fn test_bench_iter() {
438 fn f(b: &mut Bencher) {
442 let (tx, rx) = channel();
444 let desc = TestDesc {
445 name: StaticTestName("f"),
447 should_panic: ShouldPanic::No,
451 crate::bench::benchmark(desc, tx, true, f);