Fix some bugs

This commit is contained in:
soup 2024-05-19 01:52:05 -04:00
parent 8c0f5d6f46
commit 482f06ee25
No known key found for this signature in database
3 changed files with 69 additions and 11 deletions

View file

@ -27,6 +27,8 @@ pub fn lookup<'a>(
path: &ValuePath, path: &ValuePath,
value: &'a dyn Value, value: &'a dyn Value,
) -> Option<&'a dyn Value> { ) -> Option<&'a dyn Value> {
dbg!(value);
dbg!(path);
let mut value = value; let mut value = value;
for name in &path.0 { for name in &path.0 {
match value.lookup(name) { match value.lookup(name) {
@ -60,6 +62,6 @@ pub fn for_(out: &mut String, f: &For, value: &dyn Value) {
let mut idx = 0; let mut idx = 0;
while let Some(value) = value.for_(idx) { while let Some(value) = value.for_(idx) {
execute_(out, &f.body, value); execute_(out, &f.body, value);
idx += 0; idx += 1;
} }
} }

View file

@ -156,6 +156,8 @@ impl<'a> ParseState<'a> {
} }
inner.parse_one(); inner.parse_one();
} }
inner.text_might_end();
inner.add_current_text_to_ast();
self.current_byte_offset = inner.current_byte_offset; self.current_byte_offset = inner.current_byte_offset;
if !self.remaining().starts_with("{/for}") { if !self.remaining().starts_with("{/for}") {
return; return;
@ -269,4 +271,35 @@ mod test {
})], })],
); );
} }
#[test]
fn parse_for_2() {
go(
"{for .}{.}, {/for}",
vec![AstKind::For(For {
expr: ValuePath(vec![]),
body: vec![
AstKind::Print(Print(ValuePath(vec![]))),
AstKind::Text(", ".to_string()),
],
})],
);
}
#[test]
fn parse_for_nested() {
go(
"{for .items}{for .names}{.},{/for}{/for}",
vec![AstKind::For(For {
expr: ValuePath(vec!["items".to_string()]),
body: vec![AstKind::For(For {
expr: ValuePath(vec!["names".to_string()]),
body: vec![
AstKind::Print(Print(ValuePath(vec![]))),
AstKind::Text(",".to_string()),
],
})],
})],
)
}
} }

View file

@ -7,7 +7,7 @@ use internal::{
parse::{parse, Ast}, parse::{parse, Ast},
}; };
pub trait Value { pub trait Value: core::fmt::Debug {
fn print(&self, out: &mut String); fn print(&self, out: &mut String);
fn lookup(&self, name: &str) -> Option<&dyn Value>; fn lookup(&self, name: &str) -> Option<&dyn Value>;
fn has(&self) -> Option<&dyn Value>; fn has(&self) -> Option<&dyn Value>;
@ -212,24 +212,47 @@ impl Newt {
mod test { mod test {
use crate::{Newt, Value}; use crate::{Newt, Value};
fn go(tmpl: &str, value: &dyn Value, expected: &str) {
assert_eq!(Newt::build(tmpl).execute(value), expected)
}
#[test] #[test]
fn simple_print() { fn simple_print() {
let tmpl = r#"Hello, {.}"#; go("Hello, {.}!", &"World", "Hello, World!")
let out = Newt::build(tmpl).execute(&"World!");
assert_eq!(out, "Hello, World!");
} }
#[test] #[test]
fn print_lookup() { fn print_lookup() {
let tmpl = "Hello, {.name}!"; go(
let out = Newt::build(tmpl).execute(&[("name", &"Ted" as &dyn Value)]); "Hello, {.name}!",
assert_eq!(out, "Hello, Ted!"); &[("name", &"Ted" as &dyn Value)],
"Hello, Ted!",
);
} }
#[test] #[test]
fn print_for() { fn print_for() {
let tmpl = "{for .}{.}, {/for}"; go(
let out = Newt::build(tmpl).execute(&[&"Bob" as &dyn Value, &"Larry"]); "{for .}{.}, {/for}",
assert_eq!(out, "Bob, Larry"); &[&"Bob" as &dyn Value, &"Larry"],
"Bob, Larry, ",
);
}
#[test]
fn print_for_nested() {
go(
"{for .items}{for .names}{.}, {/for}{/for}",
&[(
"items",
&[
&[(
"names",
&[&"Bob" as &dyn Value, &"Larry"] as &dyn Value,
)] as &dyn Value,
] as &dyn Value,
)],
"Bob, Larry, ",
)
} }
} }