Skip to content

Commit

Permalink
Fix comparisons with empty and blank (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored Apr 9, 2021
1 parent 0947eb1 commit a990d02
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 4 deletions.
23 changes: 23 additions & 0 deletions Fluid.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
6 changes: 5 additions & 1 deletion Fluid/Values/ArrayValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public override bool Equals(FluidValue other)
}
}
}

else if (other.Type == FluidValues.Empty)
{
return _value.Length == 0;
}

return false;
}

Expand Down
1 change: 1 addition & 0 deletions Fluid/Values/BlankValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions Fluid/Values/BooleanValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
4 changes: 4 additions & 0 deletions Fluid/Values/DictionaryValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public override bool Equals(FluidValue other)
}
}
}
else if (other.Type == FluidValues.Empty)
{
return _value.Count == 0;
}

return false;
}
Expand Down
1 change: 1 addition & 0 deletions Fluid/Values/EmptyValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions Fluid/Values/NumberValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
10 changes: 7 additions & 3 deletions Fluid/Values/StringValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit a990d02

Please sign in to comment.