toml_test_data/
lib.rs

1#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2#![warn(clippy::print_stderr)]
3#![warn(clippy::print_stdout)]
4#![allow(clippy::self_named_module_files)] // `include_dir!`?
5
6#[doc = include_str!("../README.md")]
7#[cfg(doctest)]
8pub struct ReadmeDoctests;
9
10const TESTS_DIR: include_dir::Dir<'_> =
11    include_dir::include_dir!("$CARGO_MANIFEST_DIR/assets/toml-test/tests");
12
13pub fn version(ver: &str) -> impl Iterator<Item = &'static std::path::Path> {
14    TESTS_DIR
15        .get_file(format!("files-toml-{ver}"))
16        .and_then(|f| std::str::from_utf8(f.contents()).ok())
17        .into_iter()
18        .flat_map(|f| f.lines())
19        .map(std::path::Path::new)
20}
21
22pub fn versions() -> std::collections::HashMap<&'static str, Vec<&'static std::path::Path>> {
23    TESTS_DIR
24        .files()
25        .filter_map(|f| {
26            let name = f.path().file_name()?;
27            let version = name.to_str()?.strip_prefix("files-toml-")?;
28            let paths = std::str::from_utf8(f.contents())
29                .ok()?
30                .lines()
31                .map(std::path::Path::new)
32                .collect::<Vec<_>>();
33            Some((version, paths))
34        })
35        .collect()
36}
37
38#[derive(Debug, Copy, Clone, PartialEq, Eq)]
39pub struct Valid<'a> {
40    pub name: &'a std::path::Path,
41    pub fixture: &'a [u8],
42    pub expected: &'a [u8],
43}
44
45pub fn valid() -> impl Iterator<Item = Valid<'static>> {
46    let valid_dir = TESTS_DIR.get_dir("valid").unwrap();
47    valid_files(valid_dir).chain(valid_dir.dirs().flat_map(|d| {
48        assert_eq!(d.dirs().count(), 0);
49        valid_files(d)
50    }))
51}
52
53fn valid_files<'d>(
54    dir: &'d include_dir::Dir<'static>,
55) -> impl Iterator<Item = Valid<'static>> + 'd {
56    dir.files()
57        .filter(|f| f.path().extension().unwrap_or_default() == "toml")
58        .map(move |f| {
59            let t = f;
60            let j = dir
61                .files()
62                .find(|f| {
63                    f.path().parent() == t.path().parent()
64                        && f.path().file_stem() == t.path().file_stem()
65                        && f.path().extension().unwrap() == "json"
66                })
67                .unwrap();
68            let name = t.path();
69            let fixture = t.contents();
70            let expected = j.contents();
71            Valid {
72                name,
73                fixture,
74                expected,
75            }
76        })
77}
78
79#[derive(Debug, Copy, Clone, PartialEq, Eq)]
80pub struct Invalid<'a> {
81    pub name: &'a std::path::Path,
82    pub fixture: &'a [u8],
83}
84
85pub fn invalid() -> impl Iterator<Item = Invalid<'static>> {
86    let invalid_dir = TESTS_DIR.get_dir("invalid").unwrap();
87    assert_eq!(invalid_dir.files().count(), 0);
88    invalid_dir.dirs().flat_map(|d| {
89        assert_eq!(d.dirs().count(), 0);
90        d.files().map(|f| {
91            let t = f;
92            let name = f.path();
93            let fixture = t.contents();
94            Invalid { name, fixture }
95        })
96    })
97}