Skip to content

Commit

Permalink
Merge pull request #34 from ischell/make-or-helper-multi
Browse files Browse the repository at this point in the history
Switch the or helper to take in multiple values
  • Loading branch information
jonathanmorley authored Oct 2, 2019
2 parents 5266c4e + 0b95364 commit bff4cb1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/transform/helper_or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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()));

if h.is_block() {
let template = if comparison {
Expand Down Expand Up @@ -50,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() {
Expand All @@ -74,10 +71,25 @@ 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",
),
];

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)
}
}
}
6 changes: 6 additions & 0 deletions src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit bff4cb1

Please sign in to comment.