Skip to content

Commit

Permalink
fix(nil): Equality logic missed a case
Browse files Browse the repository at this point in the history
In cobalt, this presented itself as `{if null_var}` evaluating as true.
Its sad we missed this case considering we added code for `Nil` right
before a comment about us not supporting `Nil`.
  • Loading branch information
epage committed Dec 18, 2017
1 parent 930982c commit 111d10a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/tags/if_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn unless_block(_tag_name: &str,
tokens: &[Element],
options: &LiquidOptions)
-> Result<Box<Renderable>> {
let cond = try!(condition(arguments));
let cond = condition(arguments)?;
Ok(Box::new(Conditional {
condition: cond,
mode: false,
Expand All @@ -101,7 +101,7 @@ pub fn if_block(_tag_name: &str,
tokens: &[Element],
options: &LiquidOptions)
-> Result<Box<Renderable>> {
let cond = try!(condition(arguments));
let cond = condition(arguments)?;

let (leading_tokens, trailing_tokens) = split_block(&tokens[..], &["else", "elsif"], options);
let if_false = match trailing_tokens {
Expand Down Expand Up @@ -202,6 +202,16 @@ mod test {
"nope",
"{% endif %}");

let tokens = compiler::tokenize(&text).unwrap();
let template = compiler::parse(&tokens, &options())
.map(interpreter::Template::new)
.unwrap();

let mut context = Context::new();
context.set_global_val("truthy", Value::Nil);
let output = template.render(&mut context).unwrap();
assert_eq!(output, Some("nope".to_owned()));

let tokens = compiler::tokenize(&text).unwrap();
let template = compiler::parse(&tokens, &options())
.map(interpreter::Template::new)
Expand Down
5 changes: 3 additions & 2 deletions src/value/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ impl PartialEq<Value> for Value {
(&Value::Object(ref x), &Value::Object(ref y)) => x == y,
(&Value::Nil, &Value::Nil) => true,

// encode Ruby truthiness; all values except false and nil
// are true, and we don't have a notion of nil
// encode Ruby truthiness: all values except false and nil are true
(&Value::Nil, &Value::Bool(b)) |
(&Value::Bool(b), &Value::Nil) => !b,
(_, &Value::Bool(b)) |
(&Value::Bool(b), _) => b,

Expand Down

0 comments on commit 111d10a

Please sign in to comment.