]>
git.proxmox.com Git - rustc.git/blob - vendor/handlebars-3.5.5/src/helpers/helper_boolean.rs
1 //! Helpers for boolean operations
3 use crate::json
::value
::JsonTruthy
;
5 handlebars_helper
!(eq
: |x
: Json
, y
: Json
| x
== y
);
6 handlebars_helper
!(ne
: |x
: Json
, y
: Json
| x
!= y
);
7 handlebars_helper
!(gt
: |x
: i64, y
: i64| x
> y
);
8 handlebars_helper
!(gte
: |x
: i64, y
: i64| x
>= y
);
9 handlebars_helper
!(lt
: |x
: i64, y
: i64| x
< y
);
10 handlebars_helper
!(lte
: |x
: i64, y
: i64| x
<= y
);
11 handlebars_helper
!(and
: |x
: Json
, y
: Json
| x
.is_truthy(false) && y
.is_truthy(false));
12 handlebars_helper
!(or
: |x
: Json
, y
: Json
| x
.is_truthy(false) || y
.is_truthy(false));
13 handlebars_helper
!(not
: |x
: Json
| !x
.is_truthy(false));
17 fn test_condition(condition
: &str, expected
: bool
) {
18 let handlebars
= crate::Handlebars
::new();
20 let result
= handlebars
23 "{{{{#if {condition}}}}}lorem{{{{else}}}}ipsum{{{{/if}}}}",
29 assert_eq
!(&result
, if expected { "lorem" }
else { "ipsum" }
);
34 test_condition("(gt 5 3)", true);
35 test_condition("(gt 3 5)", false);
36 test_condition("(or (gt 3 5) (gt 5 3))", true);
37 test_condition("(not [])", true);
38 test_condition("(and null 4)", false);
43 test_condition("(eq 5 5)", true);
44 test_condition("(eq 5 6)", false);
45 test_condition(r
#"(eq "foo" "foo")"#, true);
46 test_condition(r
#"(eq "foo" "Foo")"#, false);
47 test_condition(r
#"(eq [5] [5])"#, true);
48 test_condition(r
#"(eq [5] [4])"#, false);
49 test_condition(r
#"(eq 5 "5")"#, false);
50 test_condition(r
#"(eq 5 [5])"#, false);
55 test_condition("(ne 5 6)", true);
56 test_condition("(ne 5 5)", false);
57 test_condition(r
#"(ne "foo" "foo")"#, false);
58 test_condition(r
#"(ne "foo" "Foo")"#, true);
62 fn nested_conditions() {
63 let handlebars
= crate::Handlebars
::new();
65 let result
= handlebars
66 .render_template("{{#if (gt 5 3)}}lorem{{else}}ipsum{{/if}}", &json
!({}
))
68 assert_eq
!(&result
, "lorem");
70 let result
= handlebars
72 "{{#if (not (gt 5 3))}}lorem{{else}}ipsum{{/if}}",
76 assert_eq
!(&result
, "ipsum");