Skip to content

Commit

Permalink
Test error values
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcefrog committed May 2, 2023
1 parent 4836718 commit 83624d0
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 19 deletions.
4 changes: 1 addition & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
{
"rust-analyzer.linkedProjects": ["./testdata/tree/error_handling/Cargo.toml"]
}
{}
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ exclude = [
"testdata/tree/cfg_attr_mutants_skip",
"testdata/tree/cfg_attr_test_skip",
"testdata/tree/dependency",
"testdata/tree/error_handling",
"testdata/tree/error_value",
"testdata/tree/everything_skipped",
"testdata/tree/factorial",
"testdata/tree/fails_without_feature",
Expand Down
10 changes: 0 additions & 10 deletions testdata/tree/error_handling/Cargo.toml

This file was deleted.

9 changes: 9 additions & 0 deletions testdata/tree/error_value/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "cargo-mutants-testdata-error-value"
version = "0.0.0"
edition = "2021"
authors = ["Martin Pool"]
publish = false

[lib]
doctest = false
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `error_handling` tree
# `error_value` tree

This tree contains a function that can return an error.
The tests ignore the error case, so would fail to detect
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,26 @@ fn small_well_tested_mutants_with_cargo_arg_release() {
.unwrap();
}

#[test]
fn error_value_catches_untested_ok_case() {
// By default this tree should fail because it's configured to
// generate an error value, and the tests forgot to check that
// the code under test does return Ok.
let tmp_src_dir = copy_of_testdata("error_value");
run()
.arg("mutants")
.args(["-v", "-V", "--no-times", "--no-shuffle"])
.arg("-d")
.arg(tmp_src_dir.path())
.assert()
.code(2)
.stderr("")
.stdout(predicate::function(|stdout| {
insta::assert_snapshot!(stdout);
true
}));
}

#[test]
/// The `--output` directory creates the named directory if necessary, and then
/// creates `mutants.out` within it. `mutants.out` is not created in the
Expand Down
10 changes: 10 additions & 0 deletions tests/cli/snapshots/cli__error_value_catches_untested_ok_case.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: tests/cli/main.rs
expression: stdout
---
Found 2 mutants to test
Unmutated baseline ... ok
src/lib.rs:3: replace even_is_ok -> Result<u32, &'static str> with Ok(Default::default()) ... caught
src/lib.rs:3: replace even_is_ok -> Result<u32, &'static str> with Err("injected") ... NOT CAUGHT
2 mutants tested: 1 missed, 1 caught

Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,20 @@ expression: buf
]
```

## testdata/tree/error_handling
## testdata/tree/error_value

```json
[
{
"package": "cargo-mutants-testdata-small-well-tested",
"package": "cargo-mutants-testdata-error-value",
"file": "src/lib.rs",
"line": 3,
"function": "even_is_ok",
"return_type": "-> Result<u32, &'static str>",
"replacement": "Ok(Default::default())"
},
{
"package": "cargo-mutants-testdata-small-well-tested",
"package": "cargo-mutants-testdata-error-value",
"file": "src/lib.rs",
"line": 3,
"function": "even_is_ok",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ src/lib.rs:17: replace double -> usize with Default::default()
src/lib.rs:1: replace factorial -> u32 with Default::default()
```

## testdata/tree/error_handling
## testdata/tree/error_value

```
src/lib.rs:3: replace even_is_ok -> Result<u32, &'static str> with Ok(Default::default())
Expand Down

0 comments on commit 83624d0

Please sign in to comment.