Added a test for backtracking.

DustJS appears to not do any backtracking.
master
Tom Alexander 4 years ago
parent 2b6c3990a9
commit c3fe7b47af
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

@ -30,6 +30,9 @@ After walk "foo":
{"foo":{"f1":"f","f2":"ff"},"bar":{"b1":"b","b2":"bb"}}
```
Scoping
-------
This appears to be using dynamic scoping instead of lexical scoping. For example, in lexical scoping a read of "b1" would fail after that final walk because you're inside the "foo" context which does not have any "b1" in or above it, however, since this is using dynamic scoping its using the invocations to build a scope tree rather than their original position.
Itermediate scopes appear to not be added. For example:
@ -64,3 +67,24 @@ After walk globals.things
```
So if we were on the "Dave" iteration in people and I attempted to read "item" it would not find a value despite "item" being a key in the lexical context above `globals.things`.
Backtracking
------------
Item resolution appears to be greedy. For example if we have:
```js
{
"clothes": {
"shirt": "t-shirt",
"pants": "jeans"
},
"alice": {
"clothes": {
"shirt": "tank top"
}
},
"bob": {},
}
```
If we walked into `alice` and then attempted to read `clothes.pants` it will return nothing because `alice` has a `clothes` block but no `pants` element inside that. However, if we walked into `bob` and attempted to read `clothes.pants` it would return `jeans` because `bob` does not have a `clothes` block so it would walk up to the global `clothes` block.

@ -10,10 +10,10 @@
],
"deep_globals": {
"item": "pencil",
"things": {"color": "purple"}
"things": {"color": "purple", "deeper_item": {"style": "number 2"}}
},
"deep_people": [
{"name": "Dave"},
{"name": "Emily", "item": "pen"}
{"name": "Emily", "item": "pen", "deeper_item": {"style": "ballpoint", "material": "plastic"}}
]
}

@ -7,6 +7,7 @@ Testing walking after entering a parent context {#globals}job: {job}, email: {em
Doing a deep walk to see if intermediate steps are added to the dynamic context.{~n}
{#deep_people}
{#deep_globals.things}
{name} has a {color} {item}{~n}
{name} has a {color} {item} which is {deeper_item.style} and made out of {deeper_item.material}
{/deep_globals.things}
but everyone shares one that is {deeper_item.style} and made out of {deeper_item.material}{~n}
{/deep_people}

@ -178,7 +178,7 @@ impl<'a> DustRenderer<'a> {
}
}
fn walk_path<'a>(
fn new_walk_path<'a>(
context: &'a dyn ContextElement,
path: &Vec<&str>,
breadcrumbs: Vec<&'a dyn ContextElement>,
@ -192,6 +192,21 @@ fn walk_path<'a>(
Ok(output)
}
// TODO: rename walk_path_from_single_level
/// Attempts to walk a path from only 1 level in the context breadcrumbs
fn walk_path<'a>(
context: &'a dyn ContextElement,
path: &Vec<&str>,
) -> Result<&'a dyn ContextElement, RenderError<'a>> {
let mut output = context;
for elem in path.iter() {
output = output.walk(elem)?;
}
Ok(output)
}
#[cfg(test)]
mod tests {
use super::*;

Loading…
Cancel
Save