pub fn testdata<P, F>(path: P, callback: F) where
P: AsRef<Path>,
F: FnMut(Vec<String>) -> Vec<String>,
Expand description
Test all .input
files in the given directory (recursively) using the
callback and compare the result with the expected output provided by a
.valid
file alongside the input.
Test procedure
All .input
files in the provided directory will be read as text, split
into lines, and passed to the provided callback.
The callback returns a list of output lines that is then compared to the
lines loaded from a .valid
file with the same name as the input.
The test will fail if the callback output does not match the lines in the
.valid
file. In this case, the function will output the differences
(see below).
After running the callback for all inputs, if there was any failed test the function will panic.
Input lines
For convenience, both the .input
and .valid
files are read into lines
by using the text::lines() function, which
provides some whitespace filtering and normalization.
The use of lines is more convenient for most test cases and the filtering avoids errors by differences in whitespace.
Failure output
After testing all .input
files, the function will output a summary of
the tests. For failed tests, diff::lines will
be used to provide the difference between the actual lines (source
) and
the expected lines from the .valid
file (result
).
Generating valid files
As a convenience feature, if a .valid
file is not found alongside the
input, the test will fail but will also create a .valid.new
file with
the actual output.
This feature can be used to easily generate .valid
files by creating
the .input
file, running the tests, and then removing the .new
from
the created file after manually inspecting it to make sure it is the
expected behavior.