Skip to content

Commit e291a01

Browse files
committed
Docs for error-value
1 parent 61713aa commit e291a01

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

book/src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
- [Listing and previewing mutations](list.md)
1616
- [Workspaces and packages](workspaces.md)
1717
- [Passing options to Cargo](cargo-args.md)
18+
- [Generating mutants](mutants.md)
19+
- [Error values](error-values.md)
1820
- [Improving performance](performance.md)
1921
- [Parallelism](parallelism.md)
2022
- [Integrations](integrations.md)

book/src/error-values.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Generating error values
2+
3+
cargo-mutants can be configured to generate mutants that return an error value from functions that return a Result.
4+
5+
This will flag cases where no test fails if the function returns an error: that might happen if there are _only_ tests for the error cases and not for the Ok case.
6+
7+
Since crates can choose to use any type for their error values,
8+
cargo-mutants must be told how to construct an appropriate error.
9+
10+
The `--error` command line option and the `error_value` configuration option specify an error value to use.
11+
12+
These options can be repeated or combined, which might be useful
13+
if there are multiple error types in the crate. On any one mutation site, probably only one of the error values will be viable, and cargo-mutants will discover that and use it.
14+
15+
The error value can be any Rust expression that evaluates to a value of the error type. It should not include the `Err` wrapper, because cargo-mutants will add that.
16+
17+
For example, if your crate uses `anyhow::Error` as its error type, you might use `--error '::anyhow::anyhow!("error")'`.
18+
19+
If you have your own error type, you might use `--error 'crate::MyError::Generic'`.
20+
21+
Since the correct error type is a property of the source tree, the configuration should typically go into `.cargo/mutants.toml` rather than being specified on the command line:
22+
23+
```toml
24+
error_values = ["::anyhow::anyhow!(\"mutated\")"]
25+
```
26+
27+
To see only the mutants generated by this configuration, you
28+
can use a command like this:
29+
30+
```sh
31+
cargo r mutants -F anyhow -vV -j4
32+
```

book/src/mutants.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Generating mutants
2+
3+
cargo mutants generates mutants by inspecting the existing
4+
source code and applying a set of rules to generate new code
5+
that is likely to compile but have different behavior.
6+
7+
In the current release, the only mutation pattern is to
8+
replace function bodies with a value of the same type.
9+
This checks that the tests:
10+
11+
1. Observe any side effects of the original function.
12+
2. Distinguish return values.
13+
14+
More mutation rules will be added in future releases.
15+
16+
| Return type | Mutation pattern |
17+
| ----------- | ---------------- |
18+
| `()` | `()` (return unit, with no side effects) |
19+
| `bool` | `true`, `false` |
20+
| `String` | `String::new()`, `"xyzzy".into()` |
21+
| `Result` | `Ok(Default::default())`, [and an error if configured](error-values.md) |
22+
| (any other) | `Default::default()` (and hope the type implements `Default`) |
23+
24+
Some of these values may not be valid for all types: for example, returning
25+
`Default::default()` will work for many types, but not all. In this case the
26+
mutant is said to be "unviable": by default these are counted but not printed,
27+
although they can be shown with `--unviable`.

0 commit comments

Comments
 (0)