From cfe1844de920970ceb91db600e987b18bb2c8462 Mon Sep 17 00:00:00 2001 From: Eric Chan Date: Wed, 13 Sep 2017 22:17:59 -0400 Subject: [PATCH] Added test coverage for sort_natural --- lib/liquid/standardfilters.rb | 2 +- test/integration/standard_filter_test.rb | 41 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 9ebb763..f14ba1d 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -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 diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index 9de2106..c53aae1 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -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