diff --git a/v0.5/Cargo.lock b/v0.5/Cargo.lock index b84982b96..3b716af78 100644 --- a/v0.5/Cargo.lock +++ b/v0.5/Cargo.lock @@ -116,6 +116,7 @@ version = "0.1.0" dependencies = [ "fastn-section", "serde", + "serde_json", ] [[package]] diff --git a/v0.5/fastn-unresolved/Cargo.toml b/v0.5/fastn-unresolved/Cargo.toml index b476a19e6..320545bc0 100644 --- a/v0.5/fastn-unresolved/Cargo.toml +++ b/v0.5/fastn-unresolved/Cargo.toml @@ -11,3 +11,6 @@ homepage.workspace = true [dependencies] fastn-section.workspace = true serde.workspace = true + +[dev-dependencies] +serde_json.workspace = true \ No newline at end of file diff --git a/v0.5/fastn-unresolved/src/parser/import.rs b/v0.5/fastn-unresolved/src/parser/import.rs index 6c3591149..285bdb389 100644 --- a/v0.5/fastn-unresolved/src/parser/import.rs +++ b/v0.5/fastn-unresolved/src/parser/import.rs @@ -85,3 +85,36 @@ fn parse_exposing( ) { todo!() } + +#[cfg(test)] +mod tests { + #[track_caller] + fn t1(source: &str, expected: serde_json::Value) { + let (mut document, sections) = + fastn_unresolved::Document::new(fastn_section::Document::parse(source)); + + let section = { + assert_eq!(sections.len(), 1); + sections.into_iter().next().unwrap() + }; + + super::import(source, section, &mut document); + assert_eq!(serde_json::to_value(document.imports).unwrap(), expected); + } + + macro_rules! t { + ($source:expr, $debug:tt) => { + t1($source, serde_json::json!($debug)); + }; + } + + #[test] + fn test_import() { + t!("-- import: foo", { "import": "foo" }); + t!("-- import: foo.fifthtry.site/bar", { "import": "foo.fifthtry.site/bar" }); + t!("-- import: foo as f", { "import": "foo=>f" }); + t!("-- import: foo as f\nexposing: x", { "import": "foo=>f", "exposing": ["x"] }); + t!("-- import: foo\nexposing: x", { "import": "foo", "exposing": ["x"] }); + t!("-- import: foo as f\nexposing: x as y", { "import": "foo as f", "exposing": ["x=>y"] }); + } +}