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 | |
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')
-rw-r--r-- | vendor/handlebars/tests/block_context.rs | 106 | ||||
-rw-r--r-- | vendor/handlebars/tests/data_helper.rs | 29 | ||||
-rw-r--r-- | vendor/handlebars/tests/escape.rs | 43 | ||||
-rw-r--r-- | vendor/handlebars/tests/helper_function_lifetime.rs | 36 | ||||
-rw-r--r-- | vendor/handlebars/tests/helper_macro.rs | 76 | ||||
-rw-r--r-- | vendor/handlebars/tests/helper_with_space.rs | 36 | ||||
-rw-r--r-- | vendor/handlebars/tests/root_var.rs | 21 | ||||
-rw-r--r-- | vendor/handlebars/tests/subexpression.rs | 169 | ||||
-rw-r--r-- | vendor/handlebars/tests/template_names.rs | 35 |
9 files changed, 551 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;" + ); +} diff --git a/vendor/handlebars/tests/data_helper.rs b/vendor/handlebars/tests/data_helper.rs new file mode 100644 index 000000000..7734b0d7a --- /dev/null +++ b/vendor/handlebars/tests/data_helper.rs @@ -0,0 +1,29 @@ +use handlebars::*; +use serde_json::json; + +struct HelperWithBorrowedData<'a>(&'a String); + +impl<'a> HelperDef for HelperWithBorrowedData<'a> { + fn call<'_reg: '_rc, '_rc>( + &self, + _: &Helper<'_reg, '_rc>, + _: &'_reg Handlebars, + _: &Context, + _: &mut RenderContext, + out: &mut dyn Output, + ) -> Result<(), RenderError> { + out.write(self.0).map_err(RenderError::from) + } +} + +#[test] +fn test_helper_with_ref_data() { + let s = "hello helper".to_owned(); + let the_helper = HelperWithBorrowedData(&s); + + let mut r = Handlebars::new(); + r.register_helper("hello", Box::new(the_helper)); + + let s = r.render_template("Output: {{hello}}", &json!({})).unwrap(); + assert_eq!(s, "Output: hello helper".to_owned()); +} diff --git a/vendor/handlebars/tests/escape.rs b/vendor/handlebars/tests/escape.rs new file mode 100644 index 000000000..563ab76b9 --- /dev/null +++ b/vendor/handlebars/tests/escape.rs @@ -0,0 +1,43 @@ +extern crate handlebars; + +#[macro_use] +extern crate serde_json; + +use handlebars::{handlebars_helper, Handlebars}; + +#[test] +fn test_escape_216() { + let hbs = Handlebars::new(); + + let data = json!({ + "FOO": "foo", + "BAR": "bar" + }); + + assert_eq!( + hbs.render_template(r"\\\\ {{FOO}} {{BAR}} {{FOO}}{{BAR}} {{FOO}}#{{BAR}} {{FOO}}//{{BAR}} {{FOO}}\\{{FOO}} {{FOO}}\\\\{{FOO}}\\\{{FOO}} \\\{{FOO}} \{{FOO}} \{{FOO}}", &data).unwrap(), + r"\\\\ foo bar foobar foo#bar foo//bar foo\foo foo\\\foo\\foo \\foo {{FOO}} {{FOO}}" + ); +} + +#[test] +fn test_string_no_escape_422() { + let mut hbs = Handlebars::new(); + + handlebars_helper!(replace: |input: str, from: str, to: str| { + input.replace(from, to) + }); + hbs.register_helper("replace", Box::new(replace)); + + assert_eq!( + r#"some\ path"#, + hbs.render_template(r#"{{replace "some/path" "/" "\\ " }}"#, &()) + .unwrap() + ); + + assert_eq!( + r#"some\path"#, + hbs.render_template(r#"{{replace "some/path" "/" "\\" }}"#, &()) + .unwrap() + ); +} diff --git a/vendor/handlebars/tests/helper_function_lifetime.rs b/vendor/handlebars/tests/helper_function_lifetime.rs new file mode 100644 index 000000000..fd21f3004 --- /dev/null +++ b/vendor/handlebars/tests/helper_function_lifetime.rs @@ -0,0 +1,36 @@ +use handlebars::*; + +fn ifcond<'reg, 'rc>( + h: &Helper<'reg, 'rc>, + handle: &'reg Handlebars, + ctx: &'rc Context, + render_ctx: &mut RenderContext<'reg, 'rc>, + out: &mut dyn Output, +) -> Result<(), RenderError> { + let cond = h + .param(0) + .and_then(|ref v| v.value().as_bool()) + .ok_or(RenderError::new("Ifcond takes a boolean !"))? as bool; + let temp = if cond { h.template() } else { h.inverse() }; + match temp { + Some(t) => t.render(handle, ctx, render_ctx, out), + None => Ok(()), + } +} + +#[test] +fn test_helper() { + let mut handlebars = Handlebars::new(); + + // register some custom helpers + handlebars.register_helper("ifcond", Box::new(ifcond)); + + // make data and render it + let data = true; + assert_eq!( + "yes", + handlebars + .render_template("{{#ifcond this}}yes{{/ifcond}}", &data) + .unwrap() + ); +} diff --git a/vendor/handlebars/tests/helper_macro.rs b/vendor/handlebars/tests/helper_macro.rs new file mode 100644 index 000000000..9a8d2dc07 --- /dev/null +++ b/vendor/handlebars/tests/helper_macro.rs @@ -0,0 +1,76 @@ +#[macro_use] +extern crate handlebars; +#[macro_use] +extern crate serde_json; + +use handlebars::Handlebars; + +handlebars_helper!(lower: |s: str| s.to_lowercase()); +handlebars_helper!(upper: |s: str| s.to_uppercase()); +handlebars_helper!(hex: |v: i64| format!("0x{:x}", v)); +handlebars_helper!(money: |v: i64, {cur: str="$"}| format!("{}{}.00", cur, v)); +handlebars_helper!(all_hash: |{cur: str="$"}| cur); +handlebars_helper!(nargs: |*args| args.len()); +handlebars_helper!(has_a: |{a:i64 = 99}, **kwargs| + format!("{}, {}", a, kwargs.get("a").is_some())); +handlebars_helper!(tag: |t: str| format!("<{}>", t)); + +#[test] +fn test_macro_helper() { + let mut hbs = Handlebars::new(); + + hbs.register_helper("lower", Box::new(lower)); + hbs.register_helper("upper", Box::new(upper)); + hbs.register_helper("hex", Box::new(hex)); + hbs.register_helper("money", Box::new(money)); + hbs.register_helper("nargs", Box::new(nargs)); + hbs.register_helper("has_a", Box::new(has_a)); + hbs.register_helper("tag", Box::new(tag)); + + let data = json!("Teixeira"); + + assert_eq!( + hbs.render_template("{{lower this}}", &data).unwrap(), + "teixeira" + ); + assert_eq!( + hbs.render_template("{{upper this}}", &data).unwrap(), + "TEIXEIRA" + ); + assert_eq!(hbs.render_template("{{hex 16}}", &()).unwrap(), "0x10"); + + assert_eq!( + hbs.render_template("{{money 5000}}", &()).unwrap(), + "$5000.00" + ); + assert_eq!( + hbs.render_template("{{money 5000 cur=\"£\"}}", &()) + .unwrap(), + "£5000.00" + ); + assert_eq!( + hbs.render_template("{{nargs 1 1 1 1 1}}", &()).unwrap(), + "5" + ); + assert_eq!(hbs.render_template("{{nargs}}", &()).unwrap(), "0"); + + assert_eq!( + hbs.render_template("{{has_a a=1 b=2}}", &()).unwrap(), + "1, true" + ); + + assert_eq!( + hbs.render_template("{{has_a x=1 b=2}}", &()).unwrap(), + "99, false" + ); + + assert_eq!( + hbs.render_template("{{tag \"html\"}}", &()).unwrap(), + "<html>" + ); + + assert_eq!( + hbs.render_template("{{{tag \"html\"}}}", &()).unwrap(), + "<html>" + ); +} diff --git a/vendor/handlebars/tests/helper_with_space.rs b/vendor/handlebars/tests/helper_with_space.rs new file mode 100644 index 000000000..5a55ab122 --- /dev/null +++ b/vendor/handlebars/tests/helper_with_space.rs @@ -0,0 +1,36 @@ +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()); +} diff --git a/vendor/handlebars/tests/root_var.rs b/vendor/handlebars/tests/root_var.rs new file mode 100644 index 000000000..fb310f6c1 --- /dev/null +++ b/vendor/handlebars/tests/root_var.rs @@ -0,0 +1,21 @@ +extern crate handlebars; +#[macro_use] +extern crate serde_json; + +use handlebars::Handlebars; + +#[test] +fn test_root_var() { + let hbs = Handlebars::new(); + + let data = json!({ + "a": [1, 2, 3, 4], + "b": "top" + }); + + assert_eq!( + hbs.render_template("{{#each a}}{{@root/b}}: {{this}};{{/each}}", &data) + .unwrap(), + "top: 1;top: 2;top: 3;top: 4;" + ); +} diff --git a/vendor/handlebars/tests/subexpression.rs b/vendor/handlebars/tests/subexpression.rs new file mode 100644 index 000000000..a0ef4090f --- /dev/null +++ b/vendor/handlebars/tests/subexpression.rs @@ -0,0 +1,169 @@ +extern crate handlebars; +#[macro_use] +extern crate serde_json; + +use std::sync::atomic::{AtomicU16, Ordering}; +use std::sync::Arc; + +use handlebars::{Context, Handlebars, Helper, HelperDef, RenderContext, RenderError, ScopedJson}; + +#[test] +fn test_subexpression() { + let hbs = Handlebars::new(); + + let data = json!({"a": 1, "b": 0, "c": 2}); + + assert_eq!( + hbs.render_template("{{#if (gt a b)}}Success{{else}}Failed{{/if}}", &data) + .unwrap(), + "Success" + ); + + assert_eq!( + hbs.render_template("{{#if (gt a c)}}Success{{else}}Failed{{/if}}", &data) + .unwrap(), + "Failed" + ); + + assert_eq!( + hbs.render_template("{{#if (not (gt a c))}}Success{{else}}Failed{{/if}}", &data) + .unwrap(), + "Success" + ); + + assert_eq!( + hbs.render_template("{{#if (not (gt a b))}}Success{{else}}Failed{{/if}}", &data) + .unwrap(), + "Failed" + ); + + // no argument provided for not + assert!(hbs + .render_template("{{#if (not)}}Success{{else}}Failed{{/if}}", &data) + .is_err()); + + // json literal + assert_eq!( + hbs.render_template("{{#if (not true)}}Success{{else}}Failed{{/if}}", &data) + .unwrap(), + "Failed" + ); + assert_eq!( + hbs.render_template("{{#if (not false)}}Success{{else}}Failed{{/if}}", &data) + .unwrap(), + "Success" + ); +} + +#[test] +fn test_strict_mode() { + let mut hbs = Handlebars::new(); + hbs.set_strict_mode(true); + + let data = json!({"a": 1}); + + assert!(hbs + .render_template("{{#if (eq a 1)}}Success{{else}}Failed{{/if}}", &data) + .is_ok()); + assert!(hbs + .render_template("{{#if (eq z 1)}}Success{{else}}Failed{{/if}}", &data) + .is_err()) +} + +#[test] +fn invalid_json_path() { + // The data here is not important + let data = &Vec::<()>::new(); + + let hbs = Handlebars::new(); + + let error = hbs.render_template("{{x[]@this}}", &data).unwrap_err(); + + let expected = "Error rendering \"Unnamed template\" line 1, col 1: Helper not defined: \"x\""; + + assert_eq!(format!("{}", error), expected); +} + +struct MyHelper; + +impl HelperDef for MyHelper { + fn call_inner<'reg: 'rc, 'rc>( + &self, + _: &Helper<'reg, 'rc>, + _: &'reg Handlebars, + _: &'rc Context, + _: &mut RenderContext<'reg, 'rc>, + ) -> Result<ScopedJson<'reg, 'rc>, RenderError> { + Ok(ScopedJson::Derived(json!({ + "a": 1, + "b": 2, + }))) + } +} + +#[test] +fn test_lookup_with_subexpression() { + let mut registry = Handlebars::new(); + registry.register_helper("myhelper", Box::new(MyHelper {})); + registry + .register_template_string("t", "{{ lookup (myhelper) \"a\" }}") + .unwrap(); + + let result = registry.render("t", &json!({})).unwrap(); + + assert_eq!("1", result); +} + +struct CallCounterHelper { + pub(crate) c: Arc<AtomicU16>, +} + +impl HelperDef for CallCounterHelper { + fn call_inner<'reg: 'rc, 'rc>( + &self, + h: &Helper<'reg, 'rc>, + _: &'reg Handlebars, + _: &'rc Context, + _: &mut RenderContext<'reg, 'rc>, + ) -> Result<ScopedJson<'reg, 'rc>, RenderError> { + // inc counter + self.c.fetch_add(1, Ordering::SeqCst); + + if let Some(_) = h.param(0) { + Ok(json!({ + "a": 1, + }) + .into()) + } else { + Ok(json!(null).into()) + } + } +} + +#[test] +fn test_helper_call_count() { + let mut registry = Handlebars::new(); + + let counter = Arc::new(AtomicU16::new(0)); + let helper = Box::new(CallCounterHelper { c: counter.clone() }); + + registry.register_helper("myhelper", helper); + + registry + .render_template( + "{{#if (myhelper a)}}something{{else}}nothing{{/if}}", + &json!(null), + ) // If returns true + .unwrap(); + + assert_eq!(1, counter.load(Ordering::SeqCst)); + + registry + .render_template( + "{{#if (myhelper)}}something{{else}}nothing{{/if}}", + &json!(null), + ) // If returns false + .unwrap(); + + assert_eq!(2, counter.load(Ordering::SeqCst)); +} diff --git a/vendor/handlebars/tests/template_names.rs b/vendor/handlebars/tests/template_names.rs new file mode 100644 index 000000000..eea9e033e --- /dev/null +++ b/vendor/handlebars/tests/template_names.rs @@ -0,0 +1,35 @@ +extern crate handlebars; +#[macro_use] +extern crate serde_json; + +use handlebars::Handlebars; + +#[test] +fn test_walk_dir_template_name() { + let mut hbs = Handlebars::new(); + + let data = json!({ + "a": [1, 2, 3, 4], + "b": "top" + }); + + hbs.register_template_string("foo/bar", "{{@root/b}}") + .unwrap(); + assert_eq!(hbs.render_template("{{> foo/bar }}", &data).unwrap(), "top"); +} + +#[test] +fn test_walk_dir_template_name_with_args() { + let mut hbs = Handlebars::new(); + + let data = json!({ + "a": [1, 2, 3, 4], + "b": "top" + }); + + hbs.register_template_string("foo/bar", "{{this}}").unwrap(); + assert_eq!( + hbs.render_template("{{> foo/bar b }}", &data).unwrap(), + "top" + ); +} |