Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 10, 2021
0 parents commit 6b3acf9
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
**/*.rs.bk
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "crates/data/assets/toml-test"]
path = crates/data/assets/toml-test
url = https://github.com/BurntSushi/toml-test.git
7 changes: 7 additions & 0 deletions crates/data/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "toml-test-data"
version = "1.0.0"
edition = "2018"

[dependencies]
include_dir = "0.6"
1 change: 1 addition & 0 deletions crates/data/assets/toml-test
Submodule toml-test added at 0a2c92
62 changes: 62 additions & 0 deletions crates/data/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const VALID_DIR: include_dir::Dir = include_dir::include_dir!("assets/toml-test/tests/valid");
const INVALID_DIR: include_dir::Dir = include_dir::include_dir!("assets/toml-test/tests/invalid");

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Valid<'a> {
pub name: &'a str,
pub fixture: &'a [u8],
pub expected: &'a [u8],
}

pub fn valid() -> impl Iterator<Item = Valid<'static>> {
valid_files(VALID_DIR.files()).chain(VALID_DIR.dirs().iter().flat_map(|d| {
assert!(d.dirs().is_empty());
valid_files(d.files())
}))
}

fn valid_files(
files: &'static [include_dir::File<'static>],
) -> impl Iterator<Item = Valid<'static>> {
files
.iter()
.filter(|f| f.path().extension().unwrap_or_default() == "toml")
.map(move |f| {
let t = f;
let j = files
.iter()
.find(|f| {
f.path().parent() == t.path().parent()
&& f.path().file_stem() == t.path().file_stem()
&& f.path().extension().unwrap() == "json"
})
.unwrap();
let name = t.path().to_str().unwrap();
let fixture = t.contents();
let expected = j.contents();
Valid {
name,
fixture,
expected,
}
})
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Invalid<'a> {
pub name: &'a str,
pub fixture: &'a [u8],
}

pub fn invalid() -> impl Iterator<Item = Invalid<'static>> {
assert!(INVALID_DIR.files().is_empty());
INVALID_DIR.dirs().iter().flat_map(|d| {
assert!(d.dirs().is_empty());
d.files().iter().map(|f| {
let t = f;
let name = f.path().to_str().unwrap();
let fixture = t.contents();
Invalid { name, fixture }
})
})
}
9 changes: 9 additions & 0 deletions crates/data/tests/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[test]
fn valid_doesnt_panic() {
toml_test_data::valid().last().unwrap();
}

#[test]
fn invalid_doesnt_panic() {
toml_test_data::invalid().last().unwrap();
}

0 comments on commit 6b3acf9

Please sign in to comment.