Skip to content

Commit

Permalink
change: make nil_safe_casecmp judge compatible for nil-nil comparison
Browse files Browse the repository at this point in the history
Ruby returns 0 (not nil) for nil <=> nil, i.e. nil and nil are judged
as equal for comparison, and so returns nil_safe_compare .
ref: #1476

To make the behavior of nil_safe_casecmp consistent with
nil_safe_compare , change nil_safe_casecmp so that comparison between
nil <=> nil return 0 (equal).

Also change testsuite to reflect this change.

Fixes #1759 .
  • Loading branch information
mtasaka committed Dec 15, 2023
1 parent c658bf9 commit 4924822
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/liquid/standardfilters.rb
Expand Up @@ -944,6 +944,8 @@ def nil_safe_compare(a, b)
def nil_safe_casecmp(a, b)
if !a.nil? && !b.nil?
a.to_s.casecmp(b.to_s)
elsif a.nil? && b.nil?
0
else
a.nil? ? 1 : -1
end
Expand Down
2 changes: 1 addition & 1 deletion test/integration/standard_filter_test.rb
Expand Up @@ -331,8 +331,8 @@ def test_sort_natural_when_property_is_sometimes_missing_puts_nils_last
{ "price" => "1", "handle" => "gamma" },
{ "price" => 2, "handle" => "epsilon" },
{ "price" => "4", "handle" => "alpha" },
{ "handle" => "delta" },
{ "handle" => "beta" },
{ "handle" => "delta" },
]
assert_equal(expectation, @filters.sort_natural(input, "price"))
end
Expand Down

0 comments on commit 4924822

Please sign in to comment.