From 40688bacd25f49e251c82fbcdfb64480ecb25713 Mon Sep 17 00:00:00 2001 From: Amit Upadhyay Date: Thu, 14 Nov 2024 12:08:09 +0530 Subject: [PATCH] parametrising test helper --- v0.5/fastn-section/src/debug.rs | 1 + v0.5/fastn-section/src/error.rs | 1 + v0.5/fastn-section/src/utils.rs | 2 +- v0.5/fastn-unresolved/src/lib.rs | 2 +- .../src/parser/component_invocation.rs | 22 +++++++ v0.5/fastn-unresolved/src/parser/import.rs | 23 +------- v0.5/fastn-unresolved/src/parser/mod.rs | 58 ++++++++++++++++--- 7 files changed, 77 insertions(+), 32 deletions(-) create mode 100644 v0.5/fastn-unresolved/src/parser/component_invocation.rs diff --git a/v0.5/fastn-section/src/debug.rs b/v0.5/fastn-section/src/debug.rs index f5f5cc62e..b6b052fa6 100644 --- a/v0.5/fastn-section/src/debug.rs +++ b/v0.5/fastn-section/src/debug.rs @@ -242,6 +242,7 @@ fn error(e: &fastn_section::Error, _s: &fastn_section::Span, _source: &str) -> s fastn_section::Error::ImportMustHaveCaption => "import_must_have_caption", fastn_section::Error::BodyNotAllowed => "body_not_allowed", fastn_section::Error::ExtraArgumentFound => "extra_argument_found", + fastn_section::Error::ComponentIsNotAFunction => "component_is_not_a_function", }; serde_json::json!({ "error": v}) diff --git a/v0.5/fastn-section/src/error.rs b/v0.5/fastn-section/src/error.rs index a2d1881fe..84228ce53 100644 --- a/v0.5/fastn-section/src/error.rs +++ b/v0.5/fastn-section/src/error.rs @@ -22,6 +22,7 @@ pub enum Error { ImportMustHaveCaption, BodyNotAllowed, ExtraArgumentFound, + ComponentIsNotAFunction, // SectionNotFound(&'a str), // MoreThanOneCaption, // ParseError, diff --git a/v0.5/fastn-section/src/utils.rs b/v0.5/fastn-section/src/utils.rs index 021778e86..ee946c63a 100644 --- a/v0.5/fastn-section/src/utils.rs +++ b/v0.5/fastn-section/src/utils.rs @@ -108,7 +108,7 @@ impl fastn_section::Section { todo!() } - pub fn kind_name<'input>(&self, _source: &'input str) -> &'input str { + pub fn kind_name<'input>(&self, _source: &'input str) -> Option<&'input str> { todo!() } diff --git a/v0.5/fastn-unresolved/src/lib.rs b/v0.5/fastn-unresolved/src/lib.rs index 7ae5cb5a5..aff5bd95e 100644 --- a/v0.5/fastn-unresolved/src/lib.rs +++ b/v0.5/fastn-unresolved/src/lib.rs @@ -72,7 +72,7 @@ pub struct Import { #[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)] pub struct ComponentInvocation { pub name: Identifier, - pub caption: Vec, + pub caption: Option, pub arguments: Vec, pub body: Vec, pub children: Vec, diff --git a/v0.5/fastn-unresolved/src/parser/component_invocation.rs b/v0.5/fastn-unresolved/src/parser/component_invocation.rs new file mode 100644 index 000000000..0c5f19bbb --- /dev/null +++ b/v0.5/fastn-unresolved/src/parser/component_invocation.rs @@ -0,0 +1,22 @@ +pub(super) fn component_invocation( + source: &str, + section: fastn_section::Section, + document: &mut fastn_unresolved::Document, +) { + if let Some(ref m) = section.function_marker { + document + .errors + .push(m.wrap(fastn_section::Error::ComponentIsNotAFunction)); + // we will go ahead with this component invocation parsing + } + + document + .content + .push(fastn_unresolved::ComponentInvocation { + name: fastn_unresolved::Identifier(section.name(source).to_string()), + caption: section.caption, + arguments: vec![], // todo + body: vec![], // todo + children: vec![], // todo + }) +} diff --git a/v0.5/fastn-unresolved/src/parser/import.rs b/v0.5/fastn-unresolved/src/parser/import.rs index a135abd7b..3e7e66757 100644 --- a/v0.5/fastn-unresolved/src/parser/import.rs +++ b/v0.5/fastn-unresolved/src/parser/import.rs @@ -108,28 +108,7 @@ fn aliasable(s: &str) -> fastn_unresolved::AliasableIdentifier { #[cfg(test)] mod tests { - #[track_caller] - fn t1(source: &str, expected: serde_json::Value) { - println!("--------- testing -----------\n{source}\n--------- source ------------"); - use fastn_section::JDebug; - - 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!(document.imports.get(0).unwrap().debug(source), expected); - } - - macro_rules! t { - ($source:expr, $debug:tt) => { - t1($source, serde_json::json!($debug)); - }; - } + fastn_unresolved::tt!(super::import, |mut d| Box::new(d.imports.pop().unwrap())); #[test] fn test_import() { diff --git a/v0.5/fastn-unresolved/src/parser/mod.rs b/v0.5/fastn-unresolved/src/parser/mod.rs index 47d9d2ad0..7062931ae 100644 --- a/v0.5/fastn-unresolved/src/parser/mod.rs +++ b/v0.5/fastn-unresolved/src/parser/mod.rs @@ -1,3 +1,4 @@ +mod component_invocation; mod import; pub fn parse(_document_id: &str, source: &str) -> fastn_unresolved::Document { @@ -6,7 +7,7 @@ pub fn parse(_document_id: &str, source: &str) -> fastn_unresolved::Document { // guess the section and call the appropriate unresolved method. for section in sections.into_iter() { let name = section.name(source).to_ascii_lowercase(); - let kind = section.kind_name(source).to_ascii_lowercase(); + let kind = section.kind_name(source).map(str::to_ascii_lowercase); // at this level we are very liberal, we just need a hint to which parser to use. // the parsers themselves do the error checks and validation. // @@ -17,19 +18,60 @@ pub fn parse(_document_id: &str, source: &str) -> fastn_unresolved::Document { // so if we do not get exact match, we try to do recovery (maybe we can use // https://github.com/lotabout/fuzzy-matcher). match ( - kind.as_str(), + kind.as_deref(), name.as_str(), section.function_marker.is_some(), ) { - ("import", _, _) | (_, "import", _) => import::import(source, section, &mut document), - ("record", _, _) => todo!(), - ("type", _, _) => todo!(), - ("module", _, _) => todo!(), - ("component", _, _) => todo!(), - (_, _, true) => todo!(), + (Some("import"), _, _) | (_, "import", _) => { + import::import(source, section, &mut document) + } + (Some("record"), _, _) => todo!(), + (Some("type"), _, _) => todo!(), + (Some("module"), _, _) => todo!(), + (Some("component"), _, _) => todo!(), + (None, _, _) => { + component_invocation::component_invocation(source, section, &mut document) + } (_, _, _) => todo!(), } } document } + +#[cfg(test)] +#[track_caller] +/// t1 takes a function parses a single section. and another function to extract the debug value +fn t1(source: &str, expected: serde_json::Value, parser: PARSER, debug: DEBUG) +where + PARSER: Fn(&str, fastn_section::Section, &mut fastn_unresolved::Document), + DEBUG: FnOnce(fastn_unresolved::Document) -> Box, +{ + println!("--------- testing -----------\n{source}\n--------- source ------------"); + + 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() + }; + + // assert everything else is empty + parser(source, section, &mut document); + + assert_eq!(debug(document).debug(source), expected); +} + +#[cfg(test)] +#[macro_export] +macro_rules! tt { + ($p:expr, $d:expr) => { + #[allow(unused_macros)] + macro_rules! t { + ($source:expr, $expected:tt) => { + fastn_unresolved::parser::t1($source, serde_json::json!($expected), $p, $d); + }; + } + }; +}