diff --git a/Fluid.Tests/ParserTests.cs b/Fluid.Tests/ParserTests.cs index 3879787c..6945e5b4 100644 --- a/Fluid.Tests/ParserTests.cs +++ b/Fluid.Tests/ParserTests.cs @@ -590,7 +590,30 @@ public Task EmptyShouldEqualToNil(string source, string expected) { return CheckAsync(source, expected, t => t.SetValue("e", "").SetValue("f", "hello")); } + + [Theory] + [InlineData("zero == empty", "false")] + [InlineData("empty == zero", "false")] + [InlineData("zero == blank", "false")] + [InlineData("blank == zero", "false")] + + [InlineData("one == empty", "false")] + [InlineData("empty == one", "false")] + [InlineData("one == blank", "false")] + [InlineData("blank == one", "false")] + public Task EmptyShouldNotEqualNumbers(string source, string expected) + { + return CheckAsync(source, expected, t => t.SetValue("zero", 0).SetValue("one", 1)); + } + [Theory] + [InlineData("blank == false", "true")] + [InlineData("empty == false", "false")] + public Task BlankShouldComparesToFalse(string source, string expected) + { + return CheckAsync(source, expected, t => t.SetValue("zero", 0).SetValue("one", 1)); + } + [Fact] public void CycleShouldHandleNumbers() { diff --git a/Fluid/Values/ArrayValue.cs b/Fluid/Values/ArrayValue.cs index b056213e..3a1d70b1 100644 --- a/Fluid/Values/ArrayValue.cs +++ b/Fluid/Values/ArrayValue.cs @@ -55,7 +55,11 @@ public override bool Equals(FluidValue other) } } } - + else if (other.Type == FluidValues.Empty) + { + return _value.Length == 0; + } + return false; } diff --git a/Fluid/Values/BlankValue.cs b/Fluid/Values/BlankValue.cs index 9f478a43..62994a4b 100644 --- a/Fluid/Values/BlankValue.cs +++ b/Fluid/Values/BlankValue.cs @@ -27,6 +27,7 @@ public override bool Equals(FluidValue other) public override bool ToBooleanValue() { + // The only values that are falsy in Liquid are nil and false return true; } diff --git a/Fluid/Values/BooleanValue.cs b/Fluid/Values/BooleanValue.cs index 95c95eee..20ba25d6 100644 --- a/Fluid/Values/BooleanValue.cs +++ b/Fluid/Values/BooleanValue.cs @@ -28,6 +28,9 @@ public static BooleanValue Create(bool value) public override bool Equals(FluidValue other) { + // blank == false -> true + if (other.Type == FluidValues.Blank) return _value == false; + return _value == other.ToBooleanValue(); } diff --git a/Fluid/Values/DictionaryValue.cs b/Fluid/Values/DictionaryValue.cs index f7ac7f15..e3ab6d26 100644 --- a/Fluid/Values/DictionaryValue.cs +++ b/Fluid/Values/DictionaryValue.cs @@ -46,6 +46,10 @@ public override bool Equals(FluidValue other) } } } + else if (other.Type == FluidValues.Empty) + { + return _value.Count == 0; + } return false; } diff --git a/Fluid/Values/EmptyValue.cs b/Fluid/Values/EmptyValue.cs index 6113f6d9..e94b50e2 100644 --- a/Fluid/Values/EmptyValue.cs +++ b/Fluid/Values/EmptyValue.cs @@ -18,6 +18,7 @@ public override bool Equals(FluidValue other) { if (other.Type == FluidValues.String && other.ToStringValue() == "") return true; if (other.Type == FluidValues.Array && other.ToNumberValue() == 0) return true; + if (other.Type == FluidValues.Dictionary &&other.ToNumberValue() == 0) return true; if (other == BlankValue.Instance) return true; if (other == EmptyValue.Instance) return true; if (other == NilValue.Instance) return false; diff --git a/Fluid/Values/NumberValue.cs b/Fluid/Values/NumberValue.cs index c83f2436..e1676192 100644 --- a/Fluid/Values/NumberValue.cs +++ b/Fluid/Values/NumberValue.cs @@ -35,6 +35,12 @@ private NumberValue(decimal value) public override bool Equals(FluidValue other) { + // Delegating other types + if (other == BlankValue.Instance || other == NilValue.Instance || other == EmptyValue.Instance) + { + return false; + } + return _value == other.ToNumberValue(); } diff --git a/Fluid/Values/StringValue.cs b/Fluid/Values/StringValue.cs index 943bf025..87e41fd3 100644 --- a/Fluid/Values/StringValue.cs +++ b/Fluid/Values/StringValue.cs @@ -82,9 +82,13 @@ internal static StringValue Create(in TextSpan span) public override bool Equals(FluidValue other) { if (other.Type == FluidValues.String) return _value == other.ToStringValue(); - if (other == BlankValue.Instance) return String.IsNullOrWhiteSpace(ToStringValue()); - if (other == EmptyValue.Instance) return ToStringValue().Length == 0; - + + // Delegating other types + if (other == BlankValue.Instance || other == NilValue.Instance || other == EmptyValue.Instance) + { + return other.Equals(this); + } + return false; }