Struct lang_tester::LangTester
source · pub struct LangTester { /* private fields */ }
Implementations§
source§impl LangTester
impl LangTester
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new LangTester
with default options. Note that, at a minimum, you need to call
test_dir
, test_extract
, and
test_cmds
.
sourcepub fn test_dir(&mut self, test_dir: &str) -> &mut Self
pub fn test_dir(&mut self, test_dir: &str) -> &mut Self
Specify the directory where test files are contained. Note that this directory will be searched recursively (i.e. subdirectories and their contents will also be considered as potential test files).
sourcepub fn test_threads(&mut self, test_threads: usize) -> &mut Self
pub fn test_threads(&mut self, test_threads: usize) -> &mut Self
Specify the number of simultaneous running test cases. Defaults to using all available CPUs.
sourcepub fn rerun_at_most(&mut self, rerun_at_most: u64) -> &mut Self
pub fn rerun_at_most(&mut self, rerun_at_most: u64) -> &mut Self
Specify the maximum number of times to rerun a test if it fails and a rerun-if-* matches. Defaults to 3.
sourcepub fn comment_prefix<S: AsRef<str>>(&mut self, comment_prefix: S) -> &mut Self
pub fn comment_prefix<S: AsRef<str>>(&mut self, comment_prefix: S) -> &mut Self
If set, defines what lines will be treated as comments if, ignoring the current level of
indentation, they begin with comment_prefix
.
This option defaults to None
.
sourcepub fn test_file_filter<F>(&mut self, test_path_filter: F) -> &mut Self
👎Deprecated since 0.7.5: Convert test_file_filter(|p| ...)
to test_path_fiter(|p| p.is_file() & ...)
pub fn test_file_filter<F>(&mut self, test_path_filter: F) -> &mut Self
test_file_filter(|p| ...)
to test_path_fiter(|p| p.is_file() & ...)
If test_file_filter
is specified, only files for which it returns true
will be
considered tests. A common use of this is to filter files based on filename extensions
e.g.:
LangTester::new()
...
.test_file_filter(|p| p.extension().unwrap().to_str().unwrap() == "rs")
...
Note that lang_tester
recursively searches directories for files.
sourcepub fn test_path_filter<F>(&mut self, test_path_filter: F) -> &mut Self
pub fn test_path_filter<F>(&mut self, test_path_filter: F) -> &mut Self
If test_path_filter
is specified, only paths for which it returns true
will be
considered tests. A common use of this is to filter tests based on filename extensions
e.g.:
LangTester::new()
...
.test_path_filter(|p| p.extension().and_then(|x| x.to_str()) == Some("rs"))
...
Note that lang_tester
recursively searches directories.
sourcepub fn test_extract<F>(&mut self, test_extract: F) -> &mut Self
pub fn test_extract<F>(&mut self, test_extract: F) -> &mut Self
Specify a function which can extract the test data for lang_tester
from a test file path,
returning it as a String
. Note that the test data does not have to be extracted from the
Path
passed to the function – it can come from any source.
How the test data is extracted from the test file is entirely up to the user, though a common convention is to store the test data in a comment at the beginning of the test file. For example, for Rust code one could use a function along the lines of the following:
LangTester::new()
...
.test_extract(|p| {
std::fs::read_to_string(p)
.unwrap()
.lines()
// Skip non-commented lines at the start of the file.
.skip_while(|l| !l.starts_with("//"))
// Extract consecutive commented lines.
.take_while(|l| l.starts_with("//"))
.map(|l| &l[2..])
.collect::<Vec<_>>()
.join("\n")
})
...
sourcepub fn fm_options<F>(&mut self, fm_options: F) -> &mut Self
pub fn fm_options<F>(&mut self, fm_options: F) -> &mut Self
Specify a function which sets options for the fm
library.
fm
is used for the fuzzy matching in lang_tester
. This function can be used to override
fm
’s defaults for a given test file (passed as Path
) and a given testing stream (stderr
or stdout, passed as TestStream
) when executing a command for that file: it is passed a
FMBuilder
and must return a FMBuilder
.
For example, to make use of fm
’s “name matcher” option such that all instances of $1
must match the same value (without precisely specifying what that value is) one could use
the following:
LangTester::new()
...
.fm_options(|_, _, fmb| {
let ptn_re = Regex::new(r"\$.+?\b").unwrap();
let text_re = Regex::new(r".+?\b").unwrap();
fmb.name_matcher(ptn_re, text_re)
})
sourcepub fn test_cmds<F>(&mut self, test_cmds: F) -> &mut Self
pub fn test_cmds<F>(&mut self, test_cmds: F) -> &mut Self
Specify a function which takes a Path
to a test file and returns a vector containing 1 or
more (name
, <Command
>)
pairs. The commands will be executed in order on the test file: for each executed command,
test commands starting with <name>
will be checked. For example, if your pipeline
requires separate compilation and linking, you might specify something along the lines of
the following:
let tempdir = ...; // A `Path` to a temporary directory.
LangTester::new()
...
.test_cmds(|p| {
let mut exe = PathBuf::new();
exe.push(&tempdir);
exe.push(p.file_stem().unwrap());
let mut compiler = Command::new("rustc");
compiler.args(&["-o", exe.to_str().unwrap(), p.to_str().unwrap()]);
let runtime = Command::new(exe);
vec![("Compiler", compiler), ("Run-time", runtime)]
})
...
and then have test data such as:
Compiler:
status: success
stderr:
stdout:
Run-time:
status: failure
stderr:
...
Error at line 10
...
sourcepub fn use_cmdline_args(&mut self, use_cmdline_args: bool) -> &mut Self
pub fn use_cmdline_args(&mut self, use_cmdline_args: bool) -> &mut Self
If set to true
, this reads arguments from std::env::args()
and interprets them in the
same way as normal cargo test files. For example if you have tests “ab” and “cd” but only
want to run the latter:
$ <test bin> c
As this suggests, a simple substring search is used to decide which tests to run.
You can get help on lang_tester
’s options:
$ <test bin> --help
This option defaults to true
.