diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/handlebars/tests/block_context.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/handlebars/tests/block_context.rs')
-rw-r--r-- | vendor/handlebars/tests/block_context.rs | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/vendor/handlebars/tests/block_context.rs b/vendor/handlebars/tests/block_context.rs new file mode 100644 index 000000000..bbaa89a62 --- /dev/null +++ b/vendor/handlebars/tests/block_context.rs @@ -0,0 +1,106 @@ +use handlebars::Handlebars; +use serde_json::json; + +#[test] +fn test_partial_with_blocks() { + let hbs = Handlebars::new(); + + let data = json!({ + "a": [ + {"b": 1}, + {"b": 2}, + ], + }); + + let template = "{{#*inline \"test\"}}{{b}};{{/inline}}{{#each a as |z|}}{{> test z}}{{/each}}"; + assert_eq!(hbs.render_template(template, &data).unwrap(), "1;2;"); +} + +#[test] +fn test_root_with_blocks() { + let hbs = Handlebars::new(); + + let data = json!({ + "a": [ + {"b": 1}, + {"b": 2}, + ], + "b": 3, + }); + + let template = + "{{#*inline \"test\"}}{{b}}:{{@root.b}};{{/inline}}{{#each a}}{{> test}}{{/each}}"; + assert_eq!(hbs.render_template(template, &data).unwrap(), "1:3;2:3;"); +} + +#[test] +fn test_singular_and_pair_block_params() { + let hbs = Handlebars::new(); + + let data = json!([ + {"value": 11}, + {"value": 22}, + ]); + + let template = + "{{#each this as |b index|}}{{b.value}}{{#each this as |value key|}}:{{key}},{{/each}}{{/each}}"; + assert_eq!( + hbs.render_template(template, &data).unwrap(), + "11:value,22:value," + ); +} + +#[test] +fn test_nested_each() { + let hbs = Handlebars::new(); + + let data = json!({ + "classes": [ + { + "methods": [ + {"id": 1}, + {"id": 2} + ] + }, + { + "methods": [ + {"id": 3}, + {"id": 4} + ] + }, + ], + }); + + let template = "{{#each classes as |class|}}{{#each class.methods as |method|}}{{method.id}};{{/each}}{{/each}}"; + assert_eq!(hbs.render_template(template, &data).unwrap(), "1;2;3;4;"); +} + +#[test] +fn test_referencing_block_param_from_upper_scope() { + let hbs = Handlebars::new(); + + let data = json!({ + "classes": [ + { + "methods": [ + {"id": 1}, + {"id": 2} + ], + "private": false + }, + { + "methods": [ + {"id": 3}, + {"id": 4} + ], + "private": true + }, + ], + }); + + let template = "{{#each classes as |class|}}{{#each class.methods as |method|}}{{class.private}}|{{method.id}};{{/each}}{{/each}}"; + assert_eq!( + hbs.render_template(template, &data).unwrap(), + "false|1;false|2;true|3;true|4;" + ); +} |