| 1 | use handlebars::Handlebars; |
| 2 | use serde_json::json; |
| 3 | |
| 4 | #[test] |
| 5 | fn test_partial_with_blocks() { |
| 6 | let hbs = Handlebars::new(); |
| 7 | |
| 8 | let data = json!({ |
| 9 | "a": [ |
| 10 | {"b": 1}, |
| 11 | {"b": 2}, |
| 12 | ], |
| 13 | }); |
| 14 | |
| 15 | let template = "{{#*inline \"test\"}}{{b}};{{/inline}}{{#each a as |z|}}{{> test z}}{{/each}}"; |
| 16 | assert_eq!(hbs.render_template(template, &data).unwrap(), "1;2;"); |
| 17 | } |
| 18 | |
| 19 | #[test] |
| 20 | fn test_root_with_blocks() { |
| 21 | let hbs = Handlebars::new(); |
| 22 | |
| 23 | let data = json!({ |
| 24 | "a": [ |
| 25 | {"b": 1}, |
| 26 | {"b": 2}, |
| 27 | ], |
| 28 | "b": 3, |
| 29 | }); |
| 30 | |
| 31 | let template = |
| 32 | "{{#*inline \"test\"}}{{b}}:{{@root.b}};{{/inline}}{{#each a}}{{> test}}{{/each}}"; |
| 33 | assert_eq!(hbs.render_template(template, &data).unwrap(), "1:3;2:3;"); |
| 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn test_singular_and_pair_block_params() { |
| 38 | let hbs = Handlebars::new(); |
| 39 | |
| 40 | let data = json!([ |
| 41 | {"value": 11}, |
| 42 | {"value": 22}, |
| 43 | ]); |
| 44 | |
| 45 | let template = |
| 46 | "{{#each this as |b index|}}{{b.value}}{{#each this as |value key|}}:{{key}},{{/each}}{{/each}}"; |
| 47 | assert_eq!( |
| 48 | hbs.render_template(template, &data).unwrap(), |
| 49 | "11:value,22:value," |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn test_nested_each() { |
| 55 | let hbs = Handlebars::new(); |
| 56 | |
| 57 | let data = json!({ |
| 58 | "classes": [ |
| 59 | { |
| 60 | "methods": [ |
| 61 | {"id": 1}, |
| 62 | {"id": 2} |
| 63 | ] |
| 64 | }, |
| 65 | { |
| 66 | "methods": [ |
| 67 | {"id": 3}, |
| 68 | {"id": 4} |
| 69 | ] |
| 70 | }, |
| 71 | ], |
| 72 | }); |
| 73 | |
| 74 | let template = "{{#each classes as |class|}}{{#each class.methods as |method|}}{{method.id}};{{/each}}{{/each}}"; |
| 75 | assert_eq!(hbs.render_template(template, &data).unwrap(), "1;2;3;4;"); |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn test_referencing_block_param_from_upper_scope() { |
| 80 | let hbs = Handlebars::new(); |
| 81 | |
| 82 | let data = json!({ |
| 83 | "classes": [ |
| 84 | { |
| 85 | "methods": [ |
| 86 | {"id": 1}, |
| 87 | {"id": 2} |
| 88 | ], |
| 89 | "private": false |
| 90 | }, |
| 91 | { |
| 92 | "methods": [ |
| 93 | {"id": 3}, |
| 94 | {"id": 4} |
| 95 | ], |
| 96 | "private": true |
| 97 | }, |
| 98 | ], |
| 99 | }); |
| 100 | |
| 101 | let template = "{{#each classes as |class|}}{{#each class.methods as |method|}}{{class.private}}|{{method.id}};{{/each}}{{/each}}"; |
| 102 | assert_eq!( |
| 103 | hbs.render_template(template, &data).unwrap(), |
| 104 | "false|1;false|2;true|3;true|4;" |
| 105 | ); |
| 106 | } |