Skip to content

Commit

Permalink
Show type parameters like From<&str> in mutant names
Browse files Browse the repository at this point in the history
Fixes #445
  • Loading branch information
sourcefrog committed Jan 4, 2025
1 parent 8d8cfa0 commit 0478743
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 23 deletions.
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# cargo-mutants changelog

## Unreleased

- Changed: Show more type parameters in mutant names, like `impl From<&str> for Foo` rather than `impl From for Foo`.

## 25.0.0

- Better estimation of time remaining, based on the time taken to test mutants so far, excluding the time for the baseline.
Expand All @@ -22,6 +26,8 @@

- New: `--skip-calls=NAME,NAME` on the command line or `skip_calls = [NAMES..]` in `.cargo/mutants.toml` allows configuring other functions whose calls should never be mutated.

- Changed: The mutant name for trait impls now includes the path of the trait as it occurs in the source file: for example `impl fmt::Display for Foo`.

## 24.11.0

- New: `--test-workspace` and `--test-package` arguments and config options support projects whose tests live in a different package.
Expand Down
48 changes: 45 additions & 3 deletions src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,7 @@ impl<'ast> Visit<'ast> for DiscoveryVisitor<'_> {
// Can't think of how to generate a viable different default.
return;
}
format!("<impl {trait} for {type_name}>", trait = trait_path.segments.last().unwrap().ident)
// TODO: trait = trait_path.to_pretty_string()) and update tests to match
format!("<impl {trait} for {type_name}>", trait = trait_path.to_pretty_string())
} else {
type_name
};
Expand Down Expand Up @@ -818,6 +817,7 @@ fn find_path_attribute(attrs: &[Attribute]) -> std::result::Result<Option<Utf8Pa
mod test {
use indoc::indoc;
use itertools::Itertools;
use pretty_assertions::assert_eq;
use test_log::test;

use crate::test_util::copy_of_testdata;
Expand Down Expand Up @@ -1026,7 +1026,7 @@ mod test {
let mutants = mutate_source_str(
indoc! {"
fn main() {
let mut v = V::new();
let mut v = v::new();
v.dont_touch_this(2 + 3);
}
"},
Expand All @@ -1042,4 +1042,46 @@ mod test {
0
);
}

#[test]
fn mutant_name_includes_type_parameters() {
// From https://github.com/sourcefrog/cargo-mutants/issues/334
let options = Options::from_arg_strs(["mutants"]);
let mutants = mutate_source_str(
indoc! {r#"
impl AsRef<str> for Apath {
fn as_ref(&self) -> &str {
&self.0
}
}
impl From<Apath> for String {
fn from(a: Apath) -> String {
a.0
}
}
impl<'a> From<&'a str> for Apath {
fn from(s: &'a str) -> Apath {
assert!(Apath::is_valid(s), "invalid apath: {s:?}");
Apath(s.to_string())
}
}
"#},
&options,
)
.unwrap();
dbg!(&mutants);
let mutant_names = mutants.iter().map(|m| m.name(false) + "\n").join("");
assert_eq!(
mutant_names,
indoc! {r#"
src/main.rs: replace <impl AsRef<str> for Apath>::as_ref -> &str with ""
src/main.rs: replace <impl AsRef<str> for Apath>::as_ref -> &str with "xyzzy"
src/main.rs: replace <impl From<Apath> for String>::from -> String with String::new()
src/main.rs: replace <impl From<Apath> for String>::from -> String with "xyzzy".into()
src/main.rs: replace <impl From<&'a str> for Apath>::from -> Apath with Default::default()
"#}
);
}
}
6 changes: 2 additions & 4 deletions tests/snapshots/list__list_mutants_in_all_trees_as_json.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6628,7 +6628,7 @@ expression: buf
{
"file": "src/methods.rs",
"function": {
"function_name": "<impl Display for Foo>::fmt",
"function_name": "<impl fmt::Display for Foo>::fmt",
"return_type": "-> fmt::Result",
"span": {
"end": {
Expand Down Expand Up @@ -6658,7 +6658,7 @@ expression: buf
{
"file": "src/methods.rs",
"function": {
"function_name": "<impl Debug for &Foo>::fmt",
"function_name": "<impl fmt::Debug for &Foo>::fmt",
"return_type": "-> fmt::Result",
"span": {
"end": {
Expand Down Expand Up @@ -9842,5 +9842,3 @@ expression: buf
}
]
```


6 changes: 2 additions & 4 deletions tests/snapshots/list__list_mutants_in_all_trees_as_text.snap
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ src/inside_mod.rs:4:13: replace outer::inner::name -> &'static str with "xyzzy"
src/methods.rs:17:9: replace Foo::double with ()
src/methods.rs:17:16: replace *= with += in Foo::double
src/methods.rs:17:16: replace *= with /= in Foo::double
src/methods.rs:23:9: replace <impl Display for Foo>::fmt -> fmt::Result with Ok(Default::default())
src/methods.rs:29:9: replace <impl Debug for &Foo>::fmt -> fmt::Result with Ok(Default::default())
src/methods.rs:23:9: replace <impl fmt::Display for Foo>::fmt -> fmt::Result with Ok(Default::default())
src/methods.rs:29:9: replace <impl fmt::Debug for &Foo>::fmt -> fmt::Result with Ok(Default::default())
src/nested_function.rs:2:5: replace has_nested -> u32 with 0
src/nested_function.rs:2:5: replace has_nested -> u32 with 1
src/nested_function.rs:3:9: replace has_nested::inner -> u32 with 0
Expand Down Expand Up @@ -545,5 +545,3 @@ main2/src/main.rs:10:5: replace triple_3 -> i32 with 0
main2/src/main.rs:10:5: replace triple_3 -> i32 with 1
main2/src/main.rs:10:5: replace triple_3 -> i32 with -1
```
4 changes: 2 additions & 2 deletions tests/snapshots/main__well_tested_tree_check_only.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ ok src/inside_mod.rs:4:13: replace outer::inner::name -> &'static str with
ok src/methods.rs:17:9: replace Foo::double with ()
ok src/methods.rs:17:16: replace *= with += in Foo::double
ok src/methods.rs:17:16: replace *= with /= in Foo::double
ok src/methods.rs:23:9: replace <impl Display for Foo>::fmt -> fmt::Result with Ok(Default::default())
ok src/methods.rs:29:9: replace <impl Debug for &Foo>::fmt -> fmt::Result with Ok(Default::default())
ok src/methods.rs:23:9: replace <impl fmt::Display for Foo>::fmt -> fmt::Result with Ok(Default::default())
ok src/methods.rs:29:9: replace <impl fmt::Debug for &Foo>::fmt -> fmt::Result with Ok(Default::default())
ok src/nested_function.rs:2:5: replace has_nested -> u32 with 0
ok src/nested_function.rs:2:5: replace has_nested -> u32 with 1
ok src/nested_function.rs:3:9: replace has_nested::inner -> u32 with 0
Expand Down
4 changes: 2 additions & 2 deletions tests/snapshots/main__well_tested_tree_finds_no_problems.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ caught src/inside_mod.rs:4:13: replace outer::inner::name -> &'static str with
caught src/methods.rs:17:9: replace Foo::double with ()
caught src/methods.rs:17:16: replace *= with += in Foo::double
caught src/methods.rs:17:16: replace *= with /= in Foo::double
caught src/methods.rs:23:9: replace <impl Display for Foo>::fmt -> fmt::Result with Ok(Default::default())
caught src/methods.rs:29:9: replace <impl Debug for &Foo>::fmt -> fmt::Result with Ok(Default::default())
caught src/methods.rs:23:9: replace <impl fmt::Display for Foo>::fmt -> fmt::Result with Ok(Default::default())
caught src/methods.rs:29:9: replace <impl fmt::Debug for &Foo>::fmt -> fmt::Result with Ok(Default::default())
caught src/nested_function.rs:2:5: replace has_nested -> u32 with 0
caught src/nested_function.rs:2:5: replace has_nested -> u32 with 1
caught src/nested_function.rs:3:9: replace has_nested::inner -> u32 with 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ src/inside_mod.rs:4:13: replace outer::inner::name -> &'static str with "xyzzy"
src/methods.rs:17:9: replace Foo::double with ()
src/methods.rs:17:16: replace *= with += in Foo::double
src/methods.rs:17:16: replace *= with /= in Foo::double
src/methods.rs:23:9: replace <impl Display for Foo>::fmt -> fmt::Result with Ok(Default::default())
src/methods.rs:29:9: replace <impl Debug for &Foo>::fmt -> fmt::Result with Ok(Default::default())
src/methods.rs:23:9: replace <impl fmt::Display for Foo>::fmt -> fmt::Result with Ok(Default::default())
src/methods.rs:29:9: replace <impl fmt::Debug for &Foo>::fmt -> fmt::Result with Ok(Default::default())
src/nested_function.rs:2:5: replace has_nested -> u32 with 0
src/nested_function.rs:2:5: replace has_nested -> u32 with 1
src/nested_function.rs:3:9: replace has_nested::inner -> u32 with 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ expression: "String::from_utf8_lossy(&output.stdout)"
{
"file": "src/methods.rs",
"function": {
"function_name": "<impl Display for Foo>::fmt",
"function_name": "<impl fmt::Display for Foo>::fmt",
"return_type": "-> fmt::Result",
"span": {
"end": {
Expand Down Expand Up @@ -636,7 +636,7 @@ expression: "String::from_utf8_lossy(&output.stdout)"
{
"file": "src/methods.rs",
"function": {
"function_name": "<impl Debug for &Foo>::fmt",
"function_name": "<impl fmt::Debug for &Foo>::fmt",
"return_type": "-> fmt::Result",
"span": {
"end": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ src/inside_mod.rs:4:13: replace outer::inner::name -> &'static str with "xyzzy"
src/methods.rs:17:9: replace Foo::double with ()
src/methods.rs:17:16: replace *= with += in Foo::double
src/methods.rs:17:16: replace *= with /= in Foo::double
src/methods.rs:23:9: replace <impl Display for Foo>::fmt -> fmt::Result with Ok(Default::default())
src/methods.rs:29:9: replace <impl Debug for &Foo>::fmt -> fmt::Result with Ok(Default::default())
src/methods.rs:23:9: replace <impl fmt::Display for Foo>::fmt -> fmt::Result with Ok(Default::default())
src/methods.rs:29:9: replace <impl fmt::Debug for &Foo>::fmt -> fmt::Result with Ok(Default::default())
src/nested_function.rs:2:5: replace has_nested -> u32 with 0
src/nested_function.rs:2:5: replace has_nested -> u32 with 1
src/nested_function.rs:3:9: replace has_nested::inner -> u32 with 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ src/inside_mod.rs:4:13: replace outer::inner::name -> &'static str with "xyzzy"
src/methods.rs:17:9: replace Foo::double with ()
src/methods.rs:17:16: replace *= with += in Foo::double
src/methods.rs:17:16: replace *= with /= in Foo::double
src/methods.rs:23:9: replace <impl Display for Foo>::fmt -> fmt::Result with Ok(Default::default())
src/methods.rs:29:9: replace <impl Debug for &Foo>::fmt -> fmt::Result with Ok(Default::default())
src/methods.rs:23:9: replace <impl fmt::Display for Foo>::fmt -> fmt::Result with Ok(Default::default())
src/methods.rs:29:9: replace <impl fmt::Debug for &Foo>::fmt -> fmt::Result with Ok(Default::default())
src/nested_function.rs:2:5: replace has_nested -> u32 with 0
src/nested_function.rs:2:5: replace has_nested -> u32 with 1
src/nested_function.rs:3:9: replace has_nested::inner -> u32 with 0
Expand Down

0 comments on commit 0478743

Please sign in to comment.