| 1 | extern crate handlebars; |
| 2 | |
| 3 | #[macro_use] |
| 4 | extern crate serde_json; |
| 5 | |
| 6 | use handlebars::{handlebars_helper, no_escape, Handlebars}; |
| 7 | |
| 8 | #[test] |
| 9 | fn test_escape_216() { |
| 10 | let hbs = Handlebars::new(); |
| 11 | |
| 12 | let data = json!({ |
| 13 | "FOO": "foo", |
| 14 | "BAR": "bar" |
| 15 | }); |
| 16 | |
| 17 | assert_eq!( |
| 18 | hbs.render_template(r"\\\\ {{FOO}} {{BAR}} {{FOO}}{{BAR}} {{FOO}}#{{BAR}} {{FOO}}//{{BAR}} {{FOO}}\\{{FOO}} {{FOO}}\\\\{{FOO}}\\\{{FOO}} \\\{{FOO}} \{{FOO}} \{{FOO}}", &data).unwrap(), |
| 19 | r"\\\\ foo bar foobar foo#bar foo//bar foo\foo foo\\\foo\\foo \\foo {{FOO}} {{FOO}}" |
| 20 | ); |
| 21 | } |
| 22 | |
| 23 | #[test] |
| 24 | fn test_string_no_escape_422() { |
| 25 | let mut hbs = Handlebars::new(); |
| 26 | |
| 27 | handlebars_helper!(replace: |input: str, from: str, to: str| { |
| 28 | input.replace(from, to) |
| 29 | }); |
| 30 | handlebars_helper!(echo: |input: str| { |
| 31 | input |
| 32 | }); |
| 33 | hbs.register_helper("replace", Box::new(replace)); |
| 34 | hbs.register_helper("echo", Box::new(echo)); |
| 35 | |
| 36 | assert_eq!( |
| 37 | r#"some\ path"#, |
| 38 | hbs.render_template(r#"{{replace "some/path" "/" "\\ " }}"#, &()) |
| 39 | .unwrap() |
| 40 | ); |
| 41 | assert_eq!( |
| 42 | r#"some\ path"#, |
| 43 | hbs.render_template(r#"{{replace 'some/path' '/' '\\ ' }}"#, &()) |
| 44 | .unwrap() |
| 45 | ); |
| 46 | |
| 47 | assert_eq!( |
| 48 | r#"some\path"#, |
| 49 | hbs.render_template(r#"{{replace "some/path" "/" "\\" }}"#, &()) |
| 50 | .unwrap() |
| 51 | ); |
| 52 | assert_eq!( |
| 53 | r#"some\path"#, |
| 54 | hbs.render_template(r#"{{replace 'some/path' '/' '\\' }}"#, &()) |
| 55 | .unwrap() |
| 56 | ); |
| 57 | |
| 58 | assert_eq!( |
| 59 | r#"double-quoted \ 'with' "nesting""#, |
| 60 | hbs.render_template(r#"{{echo "double-quoted \\ 'with' \"nesting\""}}"#, &()) |
| 61 | .unwrap() |
| 62 | ); |
| 63 | assert_eq!( |
| 64 | r#"single-quoted \ 'with' "nesting""#, |
| 65 | hbs.render_template(r#"{{echo 'single-quoted \\ \'with\' "nesting"'}}"#, &()) |
| 66 | .unwrap() |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn test_string_whitespace_467() { |
| 72 | const TEMPLATE_UNQUOTED: &str = r#"{{#each synonyms}} |
| 73 | {{this.name}} => '{{this.sym}}', |
| 74 | {{/each}} |
| 75 | "#; |
| 76 | |
| 77 | let mut hbs = Handlebars::new(); |
| 78 | hbs.register_escape_fn(no_escape); |
| 79 | hbs.register_template_string("perl", TEMPLATE_UNQUOTED) |
| 80 | .unwrap(); |
| 81 | |
| 82 | let r = hbs |
| 83 | .render("perl", &json!({"synonyms": [{"name": "lt", "sym": "<"}]})) |
| 84 | .unwrap(); |
| 85 | assert_eq!(" lt => '<',\n", r); |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn test_triple_bracket_expression_471() { |
| 90 | let mut hbs = Handlebars::new(); |
| 91 | |
| 92 | handlebars_helper!(replace: |input: str| { |
| 93 | input.replace("\n", "<br/>") |
| 94 | }); |
| 95 | hbs.register_helper("replace", Box::new(replace)); |
| 96 | |
| 97 | assert_eq!( |
| 98 | "some<br/>path", |
| 99 | hbs.render_template("{{replace h}}", &json!({"h": "some\npath"})) |
| 100 | .unwrap() |
| 101 | ); |
| 102 | |
| 103 | assert_eq!( |
| 104 | "some<br/>path", |
| 105 | hbs.render_template("{{{replace h}}}", &json!({"h": "some\npath"})) |
| 106 | .unwrap() |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn test_trimmed_nonescaped_variable() { |
| 112 | let mut handlebars = Handlebars::new(); |
| 113 | handlebars |
| 114 | .register_partial("system", "system: {{~{system}~}}") |
| 115 | .unwrap(); |
| 116 | |
| 117 | let output = handlebars |
| 118 | .render_template("{{>system system=\"hello\" prompt=\" world\"}}", &()) |
| 119 | .unwrap(); |
| 120 | |
| 121 | assert_eq!(output, "system:hello"); |
| 122 | } |