Struct lang_tester::LangTester

source ·
pub struct LangTester { /* private fields */ }

Implementations§

source§

impl LangTester

source

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.

source

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).

source

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.

source

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.

source

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.

source

pub fn test_file_filter<F>(&mut self, test_path_filter: F) -> &mut Self
where F: 'static + Fn(&Path) -> bool + RefUnwindSafe,

👎Deprecated since 0.7.5: Convert 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.

source

pub fn test_path_filter<F>(&mut self, test_path_filter: F) -> &mut Self
where F: 'static + Fn(&Path) -> bool + RefUnwindSafe,

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.

source

pub fn test_extract<F>(&mut self, test_extract: F) -> &mut Self
where F: 'static + Fn(&Path) -> String + RefUnwindSafe + Send + Sync,

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")
    })
    ...
source

pub fn fm_options<F>(&mut self, fm_options: F) -> &mut Self
where F: 'static + for<'a> Fn(&'a Path, TestStream, FMBuilder<'a>) -> FMBuilder<'a> + RefUnwindSafe + Send + Sync,

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)
   })
source

pub fn test_cmds<F>(&mut self, test_cmds: F) -> &mut Self
where F: 'static + Fn(&Path) -> Vec<(&str, Command)> + RefUnwindSafe + Send + Sync,

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
    ...
source

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.

source

pub fn run(&mut self)

Run all the lang tests.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.