Added test coverage for sort_natural

This commit is contained in:
Eric Chan
2017-09-13 22:17:59 -04:00
parent 59950bff87
commit cfe1844de9
2 changed files with 42 additions and 1 deletions

View File

@@ -151,7 +151,7 @@ module Liquid
a = a[property]
b = b[property]
if a && b
a[property].to_s.casecmp(b[property].to_s)
a.to_s.casecmp(b.to_s)
else
a ? -1 : 1
end

View File

@@ -208,6 +208,47 @@ class StandardFiltersTest < Minitest::Test
assert_equal expectation, @filters.sort(input, "price")
end
def test_sort_natural_when_property_is_sometimes_missing_puts_nils_last
input = [
{ "price" => "4", "handle" => "alpha" },
{ "handle" => "beta" },
{ "price" => "1", "handle" => "gamma" },
{ "handle" => "delta" },
{ "price" => 2, "handle" => "epsilon" }
]
expectation = [
{ "price" => "1", "handle" => "gamma" },
{ "price" => 2, "handle" => "epsilon" },
{ "price" => "4", "handle" => "alpha" },
{ "handle" => "delta" },
{ "handle" => "beta" }
]
assert_equal expectation, @filters.sort_natural(input, "price")
end
def test_sort_natural_case_check
input = [
{ "key" => "X" },
{ "key" => "Y" },
{ "key" => "Z" },
{ "fake" => "t" },
{ "key" => "a" },
{ "key" => "b" },
{ "key" => "c" }
]
expectation = [
{ "key" => "a" },
{ "key" => "b" },
{ "key" => "c" },
{ "key" => "X" },
{ "key" => "Y" },
{ "key" => "Z" },
{ "fake" => "t" }
]
assert_equal expectation, @filters.sort_natural(input, "key")
assert_equal ["a", "b", "c", "X", "Y", "Z"], @filters.sort_natural(["X", "Y", "Z", "a", "b", "c"])
end
def test_sort_empty_array
assert_equal [], @filters.sort([], "a")
end