From 94cedef3ef66787df227930cb0c7ba26b5b799ca Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 16 May 2020 12:23:08 -0400 Subject: [PATCH 1/2] Added a test for not equals which is roughly a copy of the equals test. --- js/test_cases/helpers_ne/README.md | 10 ++++++++++ js/test_cases/helpers_ne/input1.json | 7 +++++++ js/test_cases/helpers_ne/main.dust | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 js/test_cases/helpers_ne/README.md create mode 100644 js/test_cases/helpers_ne/input1.json create mode 100644 js/test_cases/helpers_ne/main.dust diff --git a/js/test_cases/helpers_ne/README.md b/js/test_cases/helpers_ne/README.md new file mode 100644 index 0000000..42d393a --- /dev/null +++ b/js/test_cases/helpers_ne/README.md @@ -0,0 +1,10 @@ +Without a key parameter, neither the main block nor the else block is rendered. + +Literal values work in both keys and values. + +Can't Walk Theory +----------------- + +Assuming a missing value = cantwalk and a non-existent key = cantwalk then their equality makes sense. + +The null tests have proven that absent parameters and missing values do not equal null and therefore it is not just making all falsey values equal. diff --git a/js/test_cases/helpers_ne/input1.json b/js/test_cases/helpers_ne/input1.json new file mode 100644 index 0000000..ece58d2 --- /dev/null +++ b/js/test_cases/helpers_ne/input1.json @@ -0,0 +1,7 @@ +{ + "str": "master", + "int": 7, + "alpha": 21, + "beta": "21", + "null": null +} diff --git a/js/test_cases/helpers_ne/main.dust b/js/test_cases/helpers_ne/main.dust new file mode 100644 index 0000000..f9ed426 --- /dev/null +++ b/js/test_cases/helpers_ne/main.dust @@ -0,0 +1,18 @@ +Testing helpers:{~n} +str is {str}{~n} +int is {int}{~n} +alpha is {alpha}{~n} +beta is {beta}{~n} +{@ne key=str value="master"}str does not equal "master"{:else}str is equal to "master"{/ne}{~n} +{@ne key=str value="7"}str does not equal "7"{:else}str is equal to "7"{/ne}{~n} +{@ne key=int value="7"}int does not equal "7"{:else}int is equal to "7"{/ne}{~n} +{@ne key=int value=7}int does not equal 7{:else}int is equal to 7{/ne}{~n} +{@ne key=alpha value=beta}alpha does not equal beta{:else}alpha is equal to beta{/ne}{~n} +{@ne value=beta}missing key is true{:else}missing key is false{/ne}{~n} +{@ne value=gamma}missing key and non-existent value is true{:else}missing key and non-existent value is false{/ne}{~n} +{@ne key=alpha}missing value is true{:else}missing value is false{/ne}{~n} +{@ne key=gamma}missing value and non-existent key is true{:else}missing value and non-existent key is false{/ne}{~n} +{@ne key="master" value="master"}"master" does not equal "master"{:else}"master" is equal to "master"{/ne}{~n} +{@ne key=null}null does not equal a missing value{:else}null equals a missing value{/ne}{~n} +{@ne key=null value=gamma}null does not equal non-existent value{:else}null equals a non-existent value{/ne}{~n} +{@ne}no parameters is true{:else}no parameters if false{/ne}{~n} From f4a935224c53874c19c423fde4657f2704edaffd Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 16 May 2020 12:26:28 -0400 Subject: [PATCH 2/2] Implemented the not equals helper. --- src/renderer/renderer.rs | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index e612cf5..addfa85 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -265,6 +265,47 @@ impl<'a> DustRenderer<'a> { } } } + DustTag::DTHelperNotEquals(parameterized_block) => { + let param_map: HashMap<&str, &RValue<'a>> = parameterized_block + .params + .iter() + .map(|pair: &KVPair<'a>| (pair.key, &pair.value)) + .collect(); + let left_side: Result<&dyn ContextElement, WalkError> = match param_map.get("key") { + None => return Ok("".to_owned()), + Some(rval) => match rval { + RValue::RVString(text) => Ok(text), + RValue::RVPath(path) => walk_path(breadcrumbs, &path.keys), + RValue::RVPositiveInteger(num) => Ok(num), + }, + }; + let right_side: Result<&dyn ContextElement, WalkError> = + match param_map.get("value") { + None => Err(WalkError::CantWalk), + Some(rval) => match rval { + RValue::RVString(text) => Ok(text), + RValue::RVPath(path) => walk_path(breadcrumbs, &path.keys), + RValue::RVPositiveInteger(num) => Ok(num), + }, + }; + if left_side != right_side { + match ¶meterized_block.contents { + None => return Ok("".to_owned()), + Some(body) => { + let rendered_content = self.render_body(body, breadcrumbs, blocks)?; + return Ok(rendered_content); + } + } + } else { + match ¶meterized_block.else_contents { + None => return Ok("".to_owned()), + Some(body) => { + let rendered_content = self.render_body(body, breadcrumbs, blocks)?; + return Ok(rendered_content); + } + } + } + } _ => (), // TODO: Implement the rest } Ok("".to_owned())