| 1 | #[macro_use] |
| 2 | extern crate handlebars; |
| 3 | #[macro_use] |
| 4 | extern crate serde_json; |
| 5 | |
| 6 | use handlebars::Handlebars; |
| 7 | use time::format_description::{parse, well_known::Rfc2822}; |
| 8 | use time::OffsetDateTime; |
| 9 | |
| 10 | handlebars_helper!(lower: |s: str| s.to_lowercase()); |
| 11 | handlebars_helper!(upper: |s: str| s.to_uppercase()); |
| 12 | handlebars_helper!(hex: |v: i64| format!("0x{:x}", v)); |
| 13 | handlebars_helper!(money: |v: i64, {cur: str="$"}| format!("{}{}.00", cur, v)); |
| 14 | handlebars_helper!(all_hash: |{cur: str="$"}| cur); |
| 15 | handlebars_helper!(nargs: |*args| args.len()); |
| 16 | handlebars_helper!(has_a: |{a:i64 = 99}, **kwargs| |
| 17 | format!("{}, {}", a, kwargs.get("a").is_some())); |
| 18 | handlebars_helper!(tag: |t: str| format!("<{}>", t)); |
| 19 | handlebars_helper!(date: |dt: OffsetDateTime| dt.format(&parse("[year]-[month]-[day]").unwrap()).unwrap()); |
| 20 | |
| 21 | #[test] |
| 22 | fn test_macro_helper() { |
| 23 | let mut hbs = Handlebars::new(); |
| 24 | |
| 25 | hbs.register_helper("lower", Box::new(lower)); |
| 26 | hbs.register_helper("upper", Box::new(upper)); |
| 27 | hbs.register_helper("hex", Box::new(hex)); |
| 28 | hbs.register_helper("money", Box::new(money)); |
| 29 | hbs.register_helper("nargs", Box::new(nargs)); |
| 30 | hbs.register_helper("has_a", Box::new(has_a)); |
| 31 | hbs.register_helper("tag", Box::new(tag)); |
| 32 | hbs.register_helper("date", Box::new(date)); |
| 33 | |
| 34 | let data = json!("Teixeira"); |
| 35 | |
| 36 | assert_eq!( |
| 37 | hbs.render_template("{{lower this}}", &data).unwrap(), |
| 38 | "teixeira" |
| 39 | ); |
| 40 | assert_eq!( |
| 41 | hbs.render_template("{{upper this}}", &data).unwrap(), |
| 42 | "TEIXEIRA" |
| 43 | ); |
| 44 | assert_eq!(hbs.render_template("{{hex 16}}", &()).unwrap(), "0x10"); |
| 45 | |
| 46 | assert_eq!( |
| 47 | hbs.render_template("{{money 5000}}", &()).unwrap(), |
| 48 | "$5000.00" |
| 49 | ); |
| 50 | assert_eq!( |
| 51 | hbs.render_template("{{money 5000 cur=\"£\"}}", &()) |
| 52 | .unwrap(), |
| 53 | "£5000.00" |
| 54 | ); |
| 55 | assert_eq!( |
| 56 | hbs.render_template("{{nargs 1 1 1 1 1}}", &()).unwrap(), |
| 57 | "5" |
| 58 | ); |
| 59 | assert_eq!(hbs.render_template("{{nargs}}", &()).unwrap(), "0"); |
| 60 | |
| 61 | assert_eq!( |
| 62 | hbs.render_template("{{has_a a=1 b=2}}", &()).unwrap(), |
| 63 | "1, true" |
| 64 | ); |
| 65 | |
| 66 | assert_eq!( |
| 67 | hbs.render_template("{{has_a x=1 b=2}}", &()).unwrap(), |
| 68 | "99, false" |
| 69 | ); |
| 70 | |
| 71 | assert_eq!( |
| 72 | hbs.render_template("{{tag \"html\"}}", &()).unwrap(), |
| 73 | "<html>" |
| 74 | ); |
| 75 | |
| 76 | assert_eq!( |
| 77 | hbs.render_template("{{{tag \"html\"}}}", &()).unwrap(), |
| 78 | "<html>" |
| 79 | ); |
| 80 | |
| 81 | assert_eq!( |
| 82 | hbs.render_template( |
| 83 | "{{date this}}", |
| 84 | &OffsetDateTime::parse("Wed, 18 Feb 2015 23:16:09 GMT", &Rfc2822).unwrap() |
| 85 | ) |
| 86 | .unwrap(), |
| 87 | "2015-02-18" |
| 88 | ); |
| 89 | |
| 90 | assert_eq!( |
| 91 | hbs.render_template("{{eq image.link null}}", &json!({"image": {"link": null}})) |
| 92 | .unwrap(), |
| 93 | "true" |
| 94 | ); |
| 95 | |
| 96 | assert_eq!( |
| 97 | hbs.render_template( |
| 98 | "{{eq image.link null}}", |
| 99 | &json!({"image": {"link": "https://url"}}) |
| 100 | ) |
| 101 | .unwrap(), |
| 102 | "false" |
| 103 | ); |
| 104 | |
| 105 | assert_eq!( |
| 106 | hbs.render_template("{{tag 'html'}}", &()).unwrap(), |
| 107 | "<html>" |
| 108 | ); |
| 109 | } |