From 256aed5487dd492b04dd07f181d5a695a4c0f19a Mon Sep 17 00:00:00 2001 From: Ian Schell Date: Fri, 27 Sep 2019 17:59:32 -0400 Subject: [PATCH 1/2] Switch the or helper to take in multiple values --- src/transform/helper_or.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/transform/helper_or.rs b/src/transform/helper_or.rs index e77483f..3811ba3 100644 --- a/src/transform/helper_or.rs +++ b/src/transform/helper_or.rs @@ -12,17 +12,13 @@ impl HelperDef for OrHelper { rc: &mut RenderContext<'reg>, out: &mut dyn Output, ) -> HelperResult { - let lvalue = h - .param(0) - .ok_or_else(|| RenderError::new("Left param not found for helper \"or\""))? - .value(); - let rvalue = h - .param(1) - .ok_or_else(|| RenderError::new("Right param not found for helper \"or\""))? - .value(); - let comparison = lvalue.as_str().map_or(false, |v| !v.is_empty()) - || rvalue.as_str().map_or(false, |v| !v.is_empty()); + + if h.params().len() < 2 { + RenderError::new("'or' requires at least 2 parameters"); + } + + let comparison = h.params().into_iter().any(|p| p.value().as_str().map_or(false, |v| !v.is_empty())); if h.is_block() { let template = if comparison { @@ -74,6 +70,10 @@ mod test { r#"{{#if (or (eq Region.Key null) (eq Region.Key "NO"))}}{{else}}Bar{{/if}}"#, "Bar", ), + ( + r#"{{#or (eq Region.Key "NO") (eq Region.Key "TEST2") (eq Region.Key "TEST")}}Foo{{/or}}"#, + "Foo", + ), ]; for (template, expected) in templates { From 57e0bf9bac5deeba613362b3e05b77ae2bb59237 Mon Sep 17 00:00:00 2001 From: Ian Schell Date: Mon, 30 Sep 2019 12:36:17 -0400 Subject: [PATCH 2/2] Added test for error condition on or helper --- src/transform/helper_or.rs | 14 +++++++++++++- src/transform/mod.rs | 6 ++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/transform/helper_or.rs b/src/transform/helper_or.rs index 3811ba3..9c649a1 100644 --- a/src/transform/helper_or.rs +++ b/src/transform/helper_or.rs @@ -15,7 +15,7 @@ impl HelperDef for OrHelper { if h.params().len() < 2 { - RenderError::new("'or' requires at least 2 parameters"); + Err(RenderError::new("'or' requires at least 2 parameters"))?; } let comparison = h.params().into_iter().any(|p| p.value().as_str().map_or(false, |v| !v.is_empty())); @@ -46,6 +46,7 @@ mod test { use super::*; use crate::transform::helper_equal::EqualHelper; use crate::transform::test::test_against_configs; + use crate::transform::test::test_error_against_configs; #[test] fn test_or() { @@ -76,8 +77,19 @@ mod test { ), ]; + let error_templates = vec![ + ( + r#"{{#or (eq Region.Key "NO") }}Foo{{/or}}"#, + "'or' requires at least 2 parameters", + ), + ]; + for (template, expected) in templates { test_against_configs(&handlebars, template, expected) } + + for (template, expected) in error_templates { + test_error_against_configs(&handlebars, template, expected) + } } } diff --git a/src/transform/mod.rs b/src/transform/mod.rs index f38a839..965039f 100644 --- a/src/transform/mod.rs +++ b/src/transform/mod.rs @@ -56,4 +56,10 @@ mod test { assert!(null_rendered.is_ok()); assert_eq!(&null_rendered.unwrap(), ""); } + + pub(crate) fn test_error_against_configs(handlebars: &Handlebars, template: &str, expected: &str) { + let config_rendered = handlebars.render_template(template, &config_fixture()); + assert!(!config_rendered.is_ok()); + assert_eq!(&config_rendered.unwrap_err().as_render_error().unwrap().desc, expected); + } }