summaryrefslogtreecommitdiffstats
path: root/vendor/handlebars/tests/helper_macro.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/handlebars/tests/helper_macro.rs
parentInitial commit. (diff)
downloadrustc-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/helper_macro.rs')
-rw-r--r--vendor/handlebars/tests/helper_macro.rs76
1 files changed, 76 insertions, 0 deletions
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(),
+ "&lt;html&gt;"
+ );
+
+ assert_eq!(
+ hbs.render_template("{{{tag \"html\"}}}", &()).unwrap(),
+ "<html>"
+ );
+}