From 0478743d2da0cc2c84b77bb940ec9853f96ebd7a Mon Sep 17 00:00:00 2001 From: Martin Pool Date: Sun, 17 Nov 2024 16:43:10 -0800 Subject: [PATCH] Show type parameters like `From<&str>` in mutant names Fixes #445 --- NEWS.md | 6 +++ src/visit.rs | 48 +++++++++++++++++-- ...st__list_mutants_in_all_trees_as_json.snap | 6 +-- ...st__list_mutants_in_all_trees_as_text.snap | 6 +-- .../main__well_tested_tree_check_only.snap | 4 +- ...n__well_tested_tree_finds_no_problems.snap | 4 +- ...ed_tree_finds_no_problems__caught.txt.snap | 4 +- ...__util__list_mutants_json_well_tested.snap | 4 +- .../list__util__list_mutants_well_tested.snap | 4 +- ...tants_well_tested_exclude_name_filter.snap | 4 +- 10 files changed, 67 insertions(+), 23 deletions(-) diff --git a/NEWS.md b/NEWS.md index e3482a83..2692ba55 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. @@ -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. diff --git a/src/visit.rs b/src/visit.rs index 4a9b1c14..cdc937fc 100644 --- a/src/visit.rs +++ b/src/visit.rs @@ -462,8 +462,7 @@ impl<'ast> Visit<'ast> for DiscoveryVisitor<'_> { // Can't think of how to generate a viable different default. return; } - format!("", trait = trait_path.segments.last().unwrap().ident) - // TODO: trait = trait_path.to_pretty_string()) and update tests to match + format!("", trait = trait_path.to_pretty_string()) } else { type_name }; @@ -818,6 +817,7 @@ fn find_path_attribute(attrs: &[Attribute]) -> std::result::Result for Apath { + fn as_ref(&self) -> &str { + &self.0 + } + } + + impl From 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 for Apath>::as_ref -> &str with "" + src/main.rs: replace for Apath>::as_ref -> &str with "xyzzy" + src/main.rs: replace for String>::from -> String with String::new() + src/main.rs: replace for String>::from -> String with "xyzzy".into() + src/main.rs: replace for Apath>::from -> Apath with Default::default() + "#} + ); + } } diff --git a/tests/snapshots/list__list_mutants_in_all_trees_as_json.snap b/tests/snapshots/list__list_mutants_in_all_trees_as_json.snap index e7dd54c8..c4dc2034 100644 --- a/tests/snapshots/list__list_mutants_in_all_trees_as_json.snap +++ b/tests/snapshots/list__list_mutants_in_all_trees_as_json.snap @@ -6628,7 +6628,7 @@ expression: buf { "file": "src/methods.rs", "function": { - "function_name": "::fmt", + "function_name": "::fmt", "return_type": "-> fmt::Result", "span": { "end": { @@ -6658,7 +6658,7 @@ expression: buf { "file": "src/methods.rs", "function": { - "function_name": "::fmt", + "function_name": "::fmt", "return_type": "-> fmt::Result", "span": { "end": { @@ -9842,5 +9842,3 @@ expression: buf } ] ``` - - diff --git a/tests/snapshots/list__list_mutants_in_all_trees_as_text.snap b/tests/snapshots/list__list_mutants_in_all_trees_as_text.snap index 4159add5..152cb695 100644 --- a/tests/snapshots/list__list_mutants_in_all_trees_as_text.snap +++ b/tests/snapshots/list__list_mutants_in_all_trees_as_text.snap @@ -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 ::fmt -> fmt::Result with Ok(Default::default()) -src/methods.rs:29:9: replace ::fmt -> fmt::Result with Ok(Default::default()) +src/methods.rs:23:9: replace ::fmt -> fmt::Result with Ok(Default::default()) +src/methods.rs:29:9: replace ::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 @@ -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 ``` - - diff --git a/tests/snapshots/main__well_tested_tree_check_only.snap b/tests/snapshots/main__well_tested_tree_check_only.snap index 0458a4b3..2add55f1 100644 --- a/tests/snapshots/main__well_tested_tree_check_only.snap +++ b/tests/snapshots/main__well_tested_tree_check_only.snap @@ -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 ::fmt -> fmt::Result with Ok(Default::default()) -ok src/methods.rs:29:9: replace ::fmt -> fmt::Result with Ok(Default::default()) +ok src/methods.rs:23:9: replace ::fmt -> fmt::Result with Ok(Default::default()) +ok src/methods.rs:29:9: replace ::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 diff --git a/tests/snapshots/main__well_tested_tree_finds_no_problems.snap b/tests/snapshots/main__well_tested_tree_finds_no_problems.snap index 9cd1f83e..90197a4b 100644 --- a/tests/snapshots/main__well_tested_tree_finds_no_problems.snap +++ b/tests/snapshots/main__well_tested_tree_finds_no_problems.snap @@ -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 ::fmt -> fmt::Result with Ok(Default::default()) -caught src/methods.rs:29:9: replace ::fmt -> fmt::Result with Ok(Default::default()) +caught src/methods.rs:23:9: replace ::fmt -> fmt::Result with Ok(Default::default()) +caught src/methods.rs:29:9: replace ::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 diff --git a/tests/snapshots/main__well_tested_tree_finds_no_problems__caught.txt.snap b/tests/snapshots/main__well_tested_tree_finds_no_problems__caught.txt.snap index 7041fdb3..a657c677 100644 --- a/tests/snapshots/main__well_tested_tree_finds_no_problems__caught.txt.snap +++ b/tests/snapshots/main__well_tested_tree_finds_no_problems__caught.txt.snap @@ -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 ::fmt -> fmt::Result with Ok(Default::default()) -src/methods.rs:29:9: replace ::fmt -> fmt::Result with Ok(Default::default()) +src/methods.rs:23:9: replace ::fmt -> fmt::Result with Ok(Default::default()) +src/methods.rs:29:9: replace ::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 diff --git a/tests/util/snapshots/list__util__list_mutants_json_well_tested.snap b/tests/util/snapshots/list__util__list_mutants_json_well_tested.snap index 4483110a..bfd4a861 100644 --- a/tests/util/snapshots/list__util__list_mutants_json_well_tested.snap +++ b/tests/util/snapshots/list__util__list_mutants_json_well_tested.snap @@ -606,7 +606,7 @@ expression: "String::from_utf8_lossy(&output.stdout)" { "file": "src/methods.rs", "function": { - "function_name": "::fmt", + "function_name": "::fmt", "return_type": "-> fmt::Result", "span": { "end": { @@ -636,7 +636,7 @@ expression: "String::from_utf8_lossy(&output.stdout)" { "file": "src/methods.rs", "function": { - "function_name": "::fmt", + "function_name": "::fmt", "return_type": "-> fmt::Result", "span": { "end": { diff --git a/tests/util/snapshots/list__util__list_mutants_well_tested.snap b/tests/util/snapshots/list__util__list_mutants_well_tested.snap index 95ce41ec..88c1118d 100644 --- a/tests/util/snapshots/list__util__list_mutants_well_tested.snap +++ b/tests/util/snapshots/list__util__list_mutants_well_tested.snap @@ -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 ::fmt -> fmt::Result with Ok(Default::default()) -src/methods.rs:29:9: replace ::fmt -> fmt::Result with Ok(Default::default()) +src/methods.rs:23:9: replace ::fmt -> fmt::Result with Ok(Default::default()) +src/methods.rs:29:9: replace ::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 diff --git a/tests/util/snapshots/list__util__list_mutants_well_tested_exclude_name_filter.snap b/tests/util/snapshots/list__util__list_mutants_well_tested_exclude_name_filter.snap index f1cd3c8f..9157d47f 100644 --- a/tests/util/snapshots/list__util__list_mutants_well_tested_exclude_name_filter.snap +++ b/tests/util/snapshots/list__util__list_mutants_well_tested_exclude_name_filter.snap @@ -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 ::fmt -> fmt::Result with Ok(Default::default()) -src/methods.rs:29:9: replace ::fmt -> fmt::Result with Ok(Default::default()) +src/methods.rs:23:9: replace ::fmt -> fmt::Result with Ok(Default::default()) +src/methods.rs:29:9: replace ::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