1//! Helpers for boolean operations
2use serde_json::Value as Json;
3
4use crate::json::value::JsonTruthy;
5
6handlebars_helper!(eq: |x: Json, y: Json| x == y);
7handlebars_helper!(ne: |x: Json, y: Json| x != y);
8handlebars_helper!(gt: |x: i64, y: i64| x > y);
9handlebars_helper!(gte: |x: i64, y: i64| x >= y);
10handlebars_helper!(lt: |x: i64, y: i64| x < y);
11handlebars_helper!(lte: |x: i64, y: i64| x <= y);
12handlebars_helper!(and: |x: Json, y: Json| x.is_truthy(false) && y.is_truthy(false));
13handlebars_helper!(or: |x: Json, y: Json| x.is_truthy(false) || y.is_truthy(false));
14handlebars_helper!(not: |x: Json| !x.is_truthy(false));
15handlebars_helper!(len: |x: Json| {
16 match x {
17 Json::Array(a) => a.len(),
18 Json::Object(m) => m.len(),
19 Json::String(s) => s.len(),
20 _ => 0
21 }
22});
23
24#[cfg(test)]
25mod test_conditions {
26 fn test_condition(condition: &str, expected: bool) {
27 let handlebars = crate::Handlebars::new();
28
29 let result = handlebars
30 .render_template(
31 &format!(
32 "{{{{#if {condition}}}}}lorem{{{{else}}}}ipsum{{{{/if}}}}",
33 condition = condition
34 ),
35 &json!({}),
36 )
37 .unwrap();
38 assert_eq!(&result, if expected { "lorem" } else { "ipsum" });
39 }
40
41 #[test]
42 fn foo() {
43 test_condition("(gt 5 3)", true);
44 test_condition("(gt 3 5)", false);
45 test_condition("(or (gt 3 5) (gt 5 3))", true);
46 test_condition("(not [])", true);
47 test_condition("(and null 4)", false);
48 }
49
50 #[test]
51 fn test_eq() {
52 test_condition("(eq 5 5)", true);
53 test_condition("(eq 5 6)", false);
54 test_condition(r#"(eq "foo" "foo")"#, true);
55 test_condition(r#"(eq "foo" "Foo")"#, false);
56 test_condition(r#"(eq [5] [5])"#, true);
57 test_condition(r#"(eq [5] [4])"#, false);
58 test_condition(r#"(eq 5 "5")"#, false);
59 test_condition(r#"(eq 5 [5])"#, false);
60 }
61
62 #[test]
63 fn test_ne() {
64 test_condition("(ne 5 6)", true);
65 test_condition("(ne 5 5)", false);
66 test_condition(r#"(ne "foo" "foo")"#, false);
67 test_condition(r#"(ne "foo" "Foo")"#, true);
68 }
69
70 #[test]
71 fn nested_conditions() {
72 let handlebars = crate::Handlebars::new();
73
74 let result = handlebars
75 .render_template("{{#if (gt 5 3)}}lorem{{else}}ipsum{{/if}}", &json!({}))
76 .unwrap();
77 assert_eq!(&result, "lorem");
78
79 let result = handlebars
80 .render_template(
81 "{{#if (not (gt 5 3))}}lorem{{else}}ipsum{{/if}}",
82 &json!({}),
83 )
84 .unwrap();
85 assert_eq!(&result, "ipsum");
86 }
87
88 #[test]
89 fn test_len() {
90 let handlebars = crate::Handlebars::new();
91
92 let result = handlebars
93 .render_template("{{len value}}", &json!({"value": [1,2,3]}))
94 .unwrap();
95 assert_eq!(&result, "3");
96
97 let result = handlebars
98 .render_template("{{len value}}", &json!({"value": {"a" :1, "b": 2}}))
99 .unwrap();
100 assert_eq!(&result, "2");
101
102 let result = handlebars
103 .render_template("{{len value}}", &json!({"value": "tomcat"}))
104 .unwrap();
105 assert_eq!(&result, "6");
106
107 let result = handlebars
108 .render_template("{{len value}}", &json!({"value": 3}))
109 .unwrap();
110 assert_eq!(&result, "0");
111 }
112}
113