1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
use handlebars::*;
use serde_json::json;
fn dump<'reg, 'rc>(
h: &Helper<'reg, 'rc>,
_: &'reg Handlebars,
_: &Context,
_: &mut RenderContext,
out: &mut dyn Output,
) -> Result<(), RenderError> {
assert_eq!(2, h.params().len());
let result = h
.params()
.iter()
.map(|p| p.value().render())
.collect::<Vec<String>>()
.join(", ");
out.write(&result)?;
Ok(())
}
#[test]
fn test_helper_with_space_param() {
let mut r = Handlebars::new();
r.register_helper("echo", Box::new(dump));
let s = r
.render_template(
"Output: {{echo \"Mozilla Firefox\" \"Google Chrome\"}}",
&json!({}),
)
.unwrap();
assert_eq!(s, "Output: Mozilla Firefox, Google Chrome".to_owned());
}
#[test]
fn test_empty_lines_472() {
let mut r = Handlebars::new();
r.register_template_string(
"t1",
r#"{{#each routes}}
import { default as {{this.handler}} } from '{{this.file_path}}'
{{/each}}
addEventListener('fetch', (event) => {
event.respondWith(handleEvent(event))
})"#,
)
.unwrap();
r.register_template_string(
"t2",
r#"addEventListener('fetch', (event) => {
event.respondWith(handleEvent(event))
})
{{#each routes}}
import { default as {{this.handler}} } from '{{this.file_path}}'
{{/each}}
"#,
)
.unwrap();
let data = json!({"routes": [{"handler": "__hello_handler", "file_path": "./hello.js"},
{"handler": "__world_index_handler", "file_path": "./world/index.js"},
{"handler": "__index_handler", "file_path": "./index.js"}]});
let exp1 = r#"import { default as __hello_handler } from './hello.js'
import { default as __world_index_handler } from './world/index.js'
import { default as __index_handler } from './index.js'
addEventListener('fetch', (event) => {
event.respondWith(handleEvent(event))
})"#;
let exp2 = r#"addEventListener('fetch', (event) => {
event.respondWith(handleEvent(event))
})
import { default as __hello_handler } from './hello.js'
import { default as __world_index_handler } from './world/index.js'
import { default as __index_handler } from './index.js'
"#;
assert_eq!(r.render("t1", &data).unwrap(), exp1);
assert_eq!(r.render("t2", &data).unwrap(), exp2);
}
|