Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of identifiers which start with numbers #479

Merged
merged 2 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ literal = { string_literal |

null_literal = @{ "null" ~ !symbol_char }
boolean_literal = @{ ("true"|"false") ~ !symbol_char }
number_literal = @{ "-"? ~ ASCII_DIGIT+ ~ "."? ~ ASCII_DIGIT* ~ ("E" ~ "-"? ~ ASCII_DIGIT+)? }
number_literal = @{ "-"? ~ ASCII_DIGIT+ ~ "."? ~ ASCII_DIGIT* ~ ("E" ~ "-"? ~ ASCII_DIGIT+)? ~ !symbol_char }
json_char_double_quote = {
!("\"" | "\\") ~ ANY
| "\\" ~ ("\"" | "\\" | "/" | "b" | "f" | "n" | "r" | "t")
Expand Down
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());
}