Giter Club home page Giter Club logo

specker-rs's Introduction

Specker

Build Status

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

Let's say we have this specification:

## 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:

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:

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

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

specker-rs's People

Contributors

nercury avatar

Stargazers

Doug Tangren avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.