Crate specker [] [src]

Checks if any number of files match some specification. Designed for testing file generation.

Let's say we have this specification:

This example is not tested
# file: output/index.html
..
<body>
..
# file: output/style.css
..
body {
..
}
..

Specker can check if there is a file named output/index.html containing <body> in some line, as well as file output/style.css containing body { and } lines. Symbol .. matches any number of lines.

If there is a match error, specker can print a nice message like:

This example is not tested
1 | <bddy>
  | ^^^^^^
  | Expected "<body>", found "<bddy>"

It also has iterators to run many such specification tests in bulk.

Example code that iterates the "spec" dir, collects all "txt" specifications and checks them:

This example is not tested
extern crate specker;

use std::fs;
use std::env;
use std::path::PathBuf;
use std::collections::HashMap;

#[test]
fn check_specifications() {
    let src_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());

    for maybe_spec in specker::walk_spec_dir(&spec_dir, "txt", specker::Options {
        skip_lines: "..",
        marker: "##",
        var_start: "${",
        var_end: "}",
    }) {
        let spec_path = maybe_spec.unwrap_or_else(|e| {
            // print nicely formatted error
            panic!("\n{}", specker::display_error(&e));
        });

        // go over spec items and check if file contents match
        for (item, input_file_name) in spec_path.spec.iter()
            .filter_map(
                |item| item.get_param("file")
                    .map(|param_value| (item, param_value))
            )
            {
                let path = spec_dir.join(input_file_name);
                let mut file = fs::File::open(&path)
                    .expect(&format!("failed to open file {:?}", &path));

                if let Err(e) = item.match_contents(&mut file, &HashMap::new()) {
                    // print nicely formatted error
                    println!("{}", specker::display_error_for_file(&path, &e));
                    // print one-liner error
                    panic!("{}", e);
                }
            }
    }
}

Structs

At
Item

Specification item, that describes how a file should be matched against.

ItemIter

Specification item iterator.

ItemValuesByKeyIter

Iterator over the specification items that contain a specific key.

Options

Specification parser options.

Param

Specification item parameter.

Spec

Parsed specification.

SpecPath

Parsed specification at a path.

SpecWalkIter

Iterator over parsed specification files.

Enums

Error

Specification iteration or parsing error.

LexError

Spec lexer error.

Match

Specification token.

ParseError

Spec parser error.

TemplateMatchError

Error returned for failed template match.

TemplateWriteError

Error returned for failed template write.

Functions

display_error

Display nice error that combines line and column info with file contents.

display_error_for_file

Display nice error that combines line and column info with file contents but error itself does not have file path info.

display_error_for_read

Display nice error that combines line and column info with file source contents.

walk_spec_dir

Walks spec directory and returns the iterator over all parsed SpecPath objects.

Type Definitions

Result

Specification iteration or parsing result.