1 unstable release
0.1.0 | Mar 30, 2023 |
---|
#76 in #source-file
8KB
104 lines
An utility for testing a function that takes in a string and returns a string.
How it works
The trycall
macro takes in a path to a directory. The directory should contain
two files for each test case:
- source - The source file with extension
.txt
- expected - The expected output file with extension
.out
The trycall
macro will then run the function on each source file and compare
the output to the expected file.
The below example will run the add_one
function on each source file in the
tests/add_one
directory (relative to the crate's root) and compare the output to
the respective expected file (e.g. tests/add_one/word.txt
will be compared to
tests/add_one/word.out
).
- If the output does not match the expected file, the test will fail.
- If the expected file does not exist, it will be treated as an empty string.
- If the function panics, the test will fail.
Example
pub fn add_one(s: &str) -> String {
s.parse::<i32>()
.map(|n| (n + 1).to_string())
.unwrap()
}
#[cfg(test)]
mod tests {
#[test]
fn add_one() {
trycall::trycall!("tests/add_one").with(super::add_one);
}
}
Updating the expected output
If you want to update the expected output, you can pass in the UPDATE_EXPECT
environment
variable. For example:
UPDATE_EXPECT=1 cargo test
Filtering
You can filter the test cases by passing in the trycall=
prefix followed by
a string which will be checked against the source file name. For example,
if you want to run the test on the negative_number
test case, you can run any
of the following commands:
cargo test -- trycall=negative_number
cargo test -- trycall=negative
cargo test -- trycall=number
You can also pass in the test case source file path directly to the trycall
macro:
#[test]
fn add_one() {
trycall::trycall!("tests/add_one/negative_number.txt").with(super::add_one);
}
Dependencies
~145KB