Skip to content

Commit

Permalink
test: add a (failing) test using identifiers starting with numbers
Browse files Browse the repository at this point in the history
These test cases capture the issue called out in sunng87#450.

The handling of identifiers which start with numbers diverges from the
official JavaScript implementation of Handlebars. It's especially bad
for cases like `{{eq 1a}}`, which validly parses as `{{eq 1 a}}`!
  • Loading branch information
mkantor committed Dec 5, 2021
1 parent a3e276c commit 630c780
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,3 +1117,37 @@ fn test_zero_args_heler() {
"Output name: first_name not resolved"
);
}

#[test]
fn test_identifiers_starting_with_numbers() {
let mut r = Registry::new();

assert!(r
.register_template_string("r1", "{{#if 0a}}true{{/if}}")
.is_ok());
let r1 = r.render("r1", &json!({"0a": true})).unwrap();
assert_eq!(r1, "true");

assert!(r.register_template_string("r2", "{{eq 1a 1}}").is_ok());
let r2 = r.render("r2", &json!({"1a": 2, "a": 1})).unwrap();
assert_eq!(r2, "false");

assert!(r
.register_template_string("r3", "0: {{0}} {{#if (eq 0 true)}}resolved from context{{/if}}\n1a: {{1a}} {{#if (eq 1a true)}}resolved from context{{/if}}\n2_2: {{2_2}} {{#if (eq 2_2 true)}}resolved from context{{/if}}") // YUP it is just eq that barfs! is if handled specially? maybe this test should go nearer to specific helpers that fail?
.is_ok());
let r3 = r
.render("r3", &json!({"0": true, "1a": true, "2_2": true}))
.unwrap();
assert_eq!(
r3,
"0: true \n1a: true resolved from context\n2_2: true resolved from context"
);

// these should all be errors:
assert!(r.register_template_string("r4", "{{eq 1}}").is_ok());
assert!(r.register_template_string("r5", "{{eq a1}}").is_ok());
assert!(r.register_template_string("r6", "{{eq 1a}}").is_ok());
assert!(r.render("r4", &()).is_err());
assert!(r.render("r5", &()).is_err());
assert!(r.render("r6", &()).is_err());
}

0 comments on commit 630c780

Please sign in to comment.