| 1 | extern crate handlebarsrust-analyzer cargo handlebars 5.1.2 handlebars/; |
| 2 | #[macro_use] |
| 3 | extern crate serde_jsonrust-analyzer cargo handlebars 5.1.2 serde_json/; |
| 4 | |
| 5 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::{AtomicU16rust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicU16#, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#}; |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#; |
| 7 | |
| 8 | use handlebarsrust-analyzer cargo handlebars 5.1.2 crate/::{ |
| 9 | Contextrust-analyzer cargo handlebars 5.1.2 context/Context#, Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#, Helperrust-analyzer cargo handlebars 5.1.2 render/Helper#, HelperDefrust-analyzer cargo handlebars 5.1.2 helpers/HelperDef#, RenderContextrust-analyzer cargo handlebars 5.1.2 render/RenderContext#, RenderErrorrust-analyzer cargo handlebars 5.1.2 error/RenderError#, RenderErrorReasonrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#, |
| 10 | ScopedJsonrust-analyzer cargo handlebars 5.1.2 json/value/ScopedJson#, |
| 11 | }; |
| 12 | |
| 13 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 14 | fn test_subexpressionrust-analyzer cargo handlebars 5.1.2 test_subexpression().() { |
| 15 | let hbs = Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#::newrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]new().(); |
| 16 | |
| 17 | let data = jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({"a": 1, "b": 0, "c": 2}); |
| 18 | |
| 19 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 20 | hbs.render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{#if (gt a b)}}Success{{else}}Failed{{/if}}", &data) |
| 21 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 22 | "Success" |
| 23 | ); |
| 24 | |
| 25 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 26 | hbs.render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{#if (gt a c)}}Success{{else}}Failed{{/if}}", &data) |
| 27 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 28 | "Failed" |
| 29 | ); |
| 30 | |
| 31 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 32 | hbs.render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{#if (not (gt a c))}}Success{{else}}Failed{{/if}}", &data) |
| 33 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 34 | "Success" |
| 35 | ); |
| 36 | |
| 37 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 38 | hbs.render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{#if (not (gt a b))}}Success{{else}}Failed{{/if}}", &data) |
| 39 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 40 | "Failed" |
| 41 | ); |
| 42 | |
| 43 | // no argument provided for not |
| 44 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(hbs |
| 45 | .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{#if (not)}}Success{{else}}Failed{{/if}}", &data) |
| 46 | .is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()); |
| 47 | |
| 48 | // json literal |
| 49 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 50 | hbs.render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{#if (not true)}}Success{{else}}Failed{{/if}}", &data) |
| 51 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 52 | "Failed" |
| 53 | ); |
| 54 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 55 | hbs.render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{#if (not false)}}Success{{else}}Failed{{/if}}", &data) |
| 56 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 57 | "Success" |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 62 | fn test_strict_moderust-analyzer cargo handlebars 5.1.2 test_strict_mode().() { |
| 63 | let mut hbs = Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#::newrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]new().(); |
| 64 | hbs.set_strict_moderust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]set_strict_mode().(true); |
| 65 | |
| 66 | let data = jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({"a": 1}); |
| 67 | |
| 68 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(hbs |
| 69 | .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{#if (eq a 1)}}Success{{else}}Failed{{/if}}", &data) |
| 70 | .is_okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_ok().()); |
| 71 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(hbs |
| 72 | .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{#if (eq z 1)}}Success{{else}}Failed{{/if}}", &data) |
| 73 | .is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().()) |
| 74 | } |
| 75 | |
| 76 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 77 | fn invalid_json_pathrust-analyzer cargo handlebars 5.1.2 invalid_json_path().() { |
| 78 | // The data here is not important |
| 79 | let data = &Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::<()>::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 80 | |
| 81 | let hbs = Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#::newrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]new().(); |
| 82 | |
| 83 | let error = hbs.render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().("{{x[]@this}}", &data).unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().(); |
| 84 | let cause = error.reasonrust-analyzer cargo handlebars 5.1.2 error/impl#[RenderError]reason().(); |
| 85 | |
| 86 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(cause, RenderErrorReasonrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#::HelperNotFoundrust-analyzer cargo handlebars 5.1.2 error/RenderErrorReason#HelperNotFound#(_))); |
| 87 | } |
| 88 | |
| 89 | struct MyHelperrust-analyzer cargo handlebars 5.1.2 MyHelper#; |
| 90 | |
| 91 | impl HelperDefrust-analyzer cargo handlebars 5.1.2 helpers/HelperDef# for MyHelperrust-analyzer cargo handlebars 5.1.2 MyHelper# { |
| 92 | fn call_innerrust-analyzer cargo handlebars 5.1.2 impl#[MyHelper][HelperDef]call_inner().<'reg: 'rc, 'rc>( |
| 93 | &self, |
| 94 | _: &Helperrust-analyzer cargo handlebars 5.1.2 render/Helper#<'rc>, |
| 95 | _: &'reg Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#, |
| 96 | _: &'rc Contextrust-analyzer cargo handlebars 5.1.2 context/Context#, |
| 97 | _: &mut RenderContextrust-analyzer cargo handlebars 5.1.2 render/RenderContext#<'reg, 'rc>, |
| 98 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<ScopedJsonrust-analyzer cargo handlebars 5.1.2 json/value/ScopedJson#<'rc>, RenderErrorrust-analyzer cargo handlebars 5.1.2 error/RenderError#> { |
| 99 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ScopedJsonrust-analyzer cargo handlebars 5.1.2 json/value/ScopedJson#::Derivedrust-analyzer cargo handlebars 5.1.2 json/value/ScopedJson#Derived#(jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({ |
| 100 | "a": 1, |
| 101 | "b": 2, |
| 102 | }))) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 107 | fn test_lookup_with_subexpressionrust-analyzer cargo handlebars 5.1.2 test_lookup_with_subexpression().() { |
| 108 | let mut registry = Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#::newrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]new().(); |
| 109 | registry.register_helperrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_helper().("myhelper", Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(MyHelperrust-analyzer cargo handlebars 5.1.2 MyHelper# {})); |
| 110 | registry |
| 111 | .register_template_stringrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_template_string().("t", "{{ lookup (myhelper) \"a\" }}") |
| 112 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 113 | |
| 114 | let result = registry.renderrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render().("t", &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({})).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 115 | |
| 116 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!("1", result); |
| 117 | } |
| 118 | |
| 119 | struct CallCounterHelperrust-analyzer cargo handlebars 5.1.2 CallCounterHelper# { |
| 120 | pub(craterust-analyzer cargo handlebars 5.1.2 crate/) crust-analyzer cargo handlebars 5.1.2 CallCounterHelper#c.: Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#<AtomicU16rust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicU16#>, |
| 121 | } |
| 122 | |
| 123 | impl HelperDefrust-analyzer cargo handlebars 5.1.2 helpers/HelperDef# for CallCounterHelperrust-analyzer cargo handlebars 5.1.2 CallCounterHelper# { |
| 124 | fn call_innerrust-analyzer cargo handlebars 5.1.2 impl#[CallCounterHelper][HelperDef]call_inner().<'reg: 'rc, 'rc>( |
| 125 | &self, |
| 126 | h: &Helperrust-analyzer cargo handlebars 5.1.2 render/Helper#<'rc>, |
| 127 | _: &'reg Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#, |
| 128 | _: &'rc Contextrust-analyzer cargo handlebars 5.1.2 context/Context#, |
| 129 | _: &mut RenderContextrust-analyzer cargo handlebars 5.1.2 render/RenderContext#<'reg, 'rc>, |
| 130 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<ScopedJsonrust-analyzer cargo handlebars 5.1.2 json/value/ScopedJson#<'rc>, RenderErrorrust-analyzer cargo handlebars 5.1.2 error/RenderError#> { |
| 131 | // inc counter |
| 132 | self.crust-analyzer cargo handlebars 5.1.2 CallCounterHelper#c..fetch_addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicU16]fetch_add().(1, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::SeqCstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#SeqCst#); |
| 133 | |
| 134 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(_) = h.paramrust-analyzer cargo handlebars 5.1.2 render/impl#[`Helper<'rc>`]param().(0) { |
| 135 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!({ |
| 136 | "a": 1, |
| 137 | }) |
| 138 | .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 139 | } else { |
| 140 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!(null).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 146 | fn test_helper_call_countrust-analyzer cargo handlebars 5.1.2 test_helper_call_count().() { |
| 147 | let mut registry = Handlebarsrust-analyzer cargo handlebars 5.1.2 registry/Registry#::newrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]new().(); |
| 148 | |
| 149 | let counter = Arcrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/Arc#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T>`]new().(AtomicU16rust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicU16#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicU16]new().(0)); |
| 150 | let helper = Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(CallCounterHelperrust-analyzer cargo handlebars 5.1.2 CallCounterHelper# { crust-analyzer cargo handlebars 5.1.2 CallCounterHelper#c.: counter.clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc sync/impl#[`Arc<T, A>`][Clone]clone().() }); |
| 151 | |
| 152 | registry.register_helperrust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]register_helper().("myhelper", helper); |
| 153 | |
| 154 | registry |
| 155 | .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().( |
| 156 | "{{#if (myhelper a)}}something{{else}}nothing{{/if}}", |
| 157 | &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!(null), |
| 158 | ) // If returns true |
| 159 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 160 | |
| 161 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(1, counter.loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicU16]load().(Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::SeqCstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#SeqCst#)); |
| 162 | |
| 163 | registry |
| 164 | .render_templaterust-analyzer cargo handlebars 5.1.2 registry/impl#[`Registry<'reg>`]render_template().( |
| 165 | "{{#if (myhelper)}}something{{else}}nothing{{/if}}", |
| 166 | &jsonrust-analyzer cargo serde_json 1.0.114 macros/json!!(null), |
| 167 | ) // If returns false |
| 168 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 169 | |
| 170 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(2, counter.loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicU16]load().(Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::SeqCstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#SeqCst#)); |
| 171 | } |