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