Skip to content

Commit

Permalink
parametrising test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Nov 14, 2024
1 parent dad5fc6 commit 40688ba
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 32 deletions.
1 change: 1 addition & 0 deletions v0.5/fastn-section/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
1 change: 1 addition & 0 deletions v0.5/fastn-section/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub enum Error {
ImportMustHaveCaption,
BodyNotAllowed,
ExtraArgumentFound,
ComponentIsNotAFunction,
// SectionNotFound(&'a str),
// MoreThanOneCaption,
// ParseError,
Expand Down
2 changes: 1 addition & 1 deletion v0.5/fastn-section/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
}

Expand Down
2 changes: 1 addition & 1 deletion v0.5/fastn-unresolved/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub struct Import {
#[derive(Debug, Clone, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct ComponentInvocation {
pub name: Identifier,
pub caption: Vec<fastn_section::Tes>,
pub caption: Option<fastn_section::HeaderValue>,
pub arguments: Vec<Argument>,
pub body: Vec<fastn_section::Tes>,
pub children: Vec<ComponentInvocation>,
Expand Down
22 changes: 22 additions & 0 deletions v0.5/fastn-unresolved/src/parser/component_invocation.rs
Original file line number Diff line number Diff line change
@@ -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
})
}
23 changes: 1 addition & 22 deletions v0.5/fastn-unresolved/src/parser/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
58 changes: 50 additions & 8 deletions v0.5/fastn-unresolved/src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod component_invocation;
mod import;

pub fn parse(_document_id: &str, source: &str) -> fastn_unresolved::Document {
Expand All @@ -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.
//
Expand All @@ -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<PARSER, DEBUG>(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<dyn fastn_section::JDebug>,
{
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);
};
}
};
}

0 comments on commit 40688ba

Please sign in to comment.