-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
267 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
members = [ | ||
"crates/data", | ||
"crates/cli", | ||
"crates/harness", | ||
] | ||
|
||
[package] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "toml-test-harness" | ||
version = "0.1.0" | ||
description = "Cargo test harness for verifying TOML parsers" | ||
repository = "https://github.com/epage/toml-test-rs" | ||
readme = "README.md" | ||
categories = ["testing", "development-tools", "text-processing", "encoding"] | ||
keywords = ["development", "toml"] | ||
license = "MIT OR Apache-2.0" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
toml-test-data = { version = "1.0", path = "../data" } | ||
toml-test = { version = "0.1", path = "../../" } | ||
ignore = "0.4" | ||
libtest-mimic = "0.3.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# toml-test-harness | ||
|
||
> **Cargo test harness for verifying TOML parsers** | ||
[![Documentation](https://img.shields.io/badge/docs-master-blue.svg)][Documentation] | ||
![License](https://img.shields.io/crates/l/toml-test.svg) | ||
[![Crates Status](https://img.shields.io/crates/v/toml-test.svg)](https://crates.io/crates/toml-test) | ||
|
||
Dual-licensed under [MIT](LICENSE-MIT) or [Apache 2.0](LICENSE-APACHE) | ||
|
||
## About | ||
|
||
[toml-test](https://github.com/BurntSushi/toml-test) is a language-agnostic | ||
toml parser spec verification. This crate exposes the test cases as Rust tests. | ||
|
||
[Documentation]: https://docs.rs/toml-test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
use std::io::Write; | ||
|
||
pub use toml_test::encoded::Encoded; | ||
pub use toml_test::verify::Decoder; | ||
pub use toml_test::verify::Encoder; | ||
pub use toml_test::Error; | ||
|
||
pub struct DecoderHarness<D> { | ||
decoder: D, | ||
matches: Option<Matches>, | ||
} | ||
|
||
impl<D: toml_test::verify::Decoder + Send + Sync + 'static> DecoderHarness<D> { | ||
pub fn new(decoder: D) -> Self { | ||
Self { | ||
decoder, | ||
matches: None, | ||
} | ||
} | ||
|
||
pub fn ignore<'p>( | ||
&mut self, | ||
patterns: impl IntoIterator<Item = &'p str>, | ||
) -> Result<&mut Self, toml_test::Error> { | ||
self.matches = Some(Matches::new(patterns.into_iter())?); | ||
Ok(self) | ||
} | ||
|
||
pub fn test(self) -> ! { | ||
let args = libtest_mimic::Arguments::from_args(); | ||
let mut tests = Vec::new(); | ||
tests.extend(toml_test_data::valid().map(|c| { | ||
libtest_mimic::Test { | ||
name: c.name.display().to_string(), | ||
kind: "".into(), | ||
is_ignored: self | ||
.matches | ||
.as_ref() | ||
.map(|m| !m.matched(c.name)) | ||
.unwrap_or_default(), | ||
is_bench: false, | ||
data: Case::from(c), | ||
} | ||
})); | ||
tests.extend(toml_test_data::invalid().map(|c| { | ||
libtest_mimic::Test { | ||
name: c.name.display().to_string(), | ||
kind: "".into(), | ||
is_ignored: self | ||
.matches | ||
.as_ref() | ||
.map(|m| !m.matched(c.name)) | ||
.unwrap_or_default(), | ||
is_bench: false, | ||
data: Case::from(c), | ||
} | ||
})); | ||
|
||
let nocapture = args.nocapture; | ||
libtest_mimic::run_tests(&args, tests, move |test| match test.data { | ||
Case::Valid(case) => { | ||
match self.decoder.verify_valid_case(case.fixture, case.expected) { | ||
Ok(()) => libtest_mimic::Outcome::Passed, | ||
Err(err) => libtest_mimic::Outcome::Failed { | ||
msg: Some(err.to_string()), | ||
}, | ||
} | ||
} | ||
Case::Invalid(case) => match self.decoder.verify_invalid_case(case.fixture) { | ||
Ok(err) => { | ||
if nocapture { | ||
let _ = writeln!(std::io::stdout(), "{}", err); | ||
} | ||
libtest_mimic::Outcome::Passed | ||
} | ||
Err(err) => libtest_mimic::Outcome::Failed { | ||
msg: Some(err.to_string()), | ||
}, | ||
}, | ||
}) | ||
.exit() | ||
} | ||
} | ||
|
||
enum Case { | ||
Valid(toml_test_data::Valid<'static>), | ||
Invalid(toml_test_data::Invalid<'static>), | ||
} | ||
|
||
impl From<toml_test_data::Valid<'static>> for Case { | ||
fn from(other: toml_test_data::Valid<'static>) -> Self { | ||
Self::Valid(other) | ||
} | ||
} | ||
|
||
impl From<toml_test_data::Invalid<'static>> for Case { | ||
fn from(other: toml_test_data::Invalid<'static>) -> Self { | ||
Self::Invalid(other) | ||
} | ||
} | ||
|
||
struct Matches { | ||
ignores: ignore::gitignore::Gitignore, | ||
} | ||
|
||
impl Matches { | ||
fn new<'p>(patterns: impl Iterator<Item = &'p str>) -> Result<Self, toml_test::Error> { | ||
let mut ignores = ignore::gitignore::GitignoreBuilder::new("."); | ||
for line in patterns { | ||
ignores | ||
.add_line(None, line) | ||
.map_err(|e| toml_test::Error::new(e))?; | ||
} | ||
let ignores = ignores.build().map_err(|e| toml_test::Error::new(e))?; | ||
Ok(Self { ignores }) | ||
} | ||
|
||
fn matched(&self, path: &std::path::Path) -> bool { | ||
match self.ignores.matched_path_or_any_parents(path, false) { | ||
ignore::Match::None | ignore::Match::Whitelist(_) => true, | ||
ignore::Match::Ignore(_) => false, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.