Skip to content

Commit

Permalink
Merge pull request #30 from sunshowers/fix-test
Browse files Browse the repository at this point in the history
Include the kind of test while matching filters
  • Loading branch information
LukasKalbertodt committed Jan 14, 2024
2 parents 2ed35db + f1db53f commit 3ac6132
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@

#![forbid(unsafe_code)]

use std::{process, sync::mpsc, fmt, time::Instant};
use std::{process, sync::mpsc, fmt, time::Instant, borrow::Cow};

mod args;
mod printer;
Expand Down Expand Up @@ -240,6 +240,16 @@ struct TestInfo {
is_bench: bool,
}

impl TestInfo {
fn test_name_with_kind(&self) -> Cow<'_, str> {
if self.kind.is_empty() {
Cow::Borrowed(&self.name)
} else {
Cow::Owned(format!("[{}] {}", self.kind, self.name))
}
}
}

/// Output of a benchmark.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Measurement {
Expand Down Expand Up @@ -364,22 +374,37 @@ impl Arguments {
}

fn is_filtered_out(&self, test: &Trial) -> bool {
let test_name = &test.info.name;
let test_name = test.name();
// Match against the full test name, including the kind. This upholds the invariant that if
// --list prints out:
//
// <some string>: test
//
// then "--exact <some string>" runs exactly that test.
let test_name_with_kind = test.info.test_name_with_kind();

// If a filter was specified, apply this
if let Some(filter) = &self.filter {
match self.exact {
true if test_name != filter => return true,
false if !test_name.contains(filter) => return true,
// For exact matches, we want to match against either the test name (to maintain
// backwards compatibility with older versions of libtest-mimic), or the test kind
// (technically more correct with respect to matching against the output of --list.)
true if test_name != filter && &test_name_with_kind != filter => return true,
false if !test_name_with_kind.contains(filter) => return true,
_ => {}
};
}

// If any skip pattern were specified, test for all patterns.
for skip_filter in &self.skip {
match self.exact {
true if test_name == skip_filter => return true,
false if test_name.contains(skip_filter) => return true,
// For exact matches, we want to match against either the test name (to maintain
// backwards compatibility with older versions of libtest-mimic), or the test kind
// (technically more correct with respect to matching against the output of --list.)
true if test_name == skip_filter || &test_name_with_kind == skip_filter => {
return true
}
false if test_name_with_kind.contains(skip_filter) => return true,
_ => {}
}
}
Expand Down
70 changes: 70 additions & 0 deletions tests/mixed_bag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,16 @@ fn list_ignored() {
#[test]
fn list_with_filter() {
let (c, out) = common::do_run(args(["--list", "a"]), tests());
// Matches all tests that contain "a" in either the name or the kind.
assert_log!(out, "
cat: test
[apple] fox: test
[apple] bunny: test
[banana] fly: test
[banana] bear: test
cyan: bench
[banana] orange: bench
[banana] pink: bench
");
assert_eq!(c, Conclusion {
num_filtered_out: 0,
Expand All @@ -242,6 +247,71 @@ fn list_with_filter() {
});
}

#[test]
fn list_with_filter_exact() {
// --exact matches the test name either with or without the kind.
let (c, out) = common::do_run(args(["--list", "--exact", "[apple] fox"]), tests());
assert_log!(out, "
[apple] fox: test
");
assert_eq!(c, Conclusion {
num_filtered_out: 0,
num_passed: 0,
num_failed: 0,
num_ignored: 0,
num_measured: 0,
});
let (c, out) = common::do_run(args(["--list", "--exact", "fly"]), tests());
assert_log!(out, "
[banana] fly: test
");
assert_eq!(c, Conclusion {
num_filtered_out: 0,
num_passed: 0,
num_failed: 0,
num_ignored: 0,
num_measured: 0,
});

// --skip --exact can be used to exclude tests.
let (c, out) = common::do_run(
args([
"--list",
"--exact",
"--skip",
"[apple] fox",
"--skip",
"fly",
]),
tests(),
);
assert_log!(out, "
cat: test
dog: test
[apple] bunny: test
frog: test
owl: test
[banana] bear: test
red: bench
blue: bench
[kiwi] yellow: bench
[kiwi] green: bench
purple: bench
cyan: bench
[banana] orange: bench
[banana] pink: bench
");
assert_eq!(c, Conclusion {
num_filtered_out: 0,
num_passed: 0,
num_failed: 0,
num_ignored: 0,
num_measured: 0,
});

// --skip --exact matches test names without the kind as well.
}

#[test]
fn filter_c() {
check(args(["c"]), tests, 2,
Expand Down

0 comments on commit 3ac6132

Please sign in to comment.