Rubocop: test/test_filters.rb

This commit is contained in:
Pat Hawks 2016-05-25 21:59:11 -05:00
parent 746e360c63
commit 6ef2bcb29a
1 changed files with 192 additions and 86 deletions

View File

@ -1,6 +1,6 @@
# coding: utf-8 # coding: utf-8
require 'helper' require "helper"
class TestFilters < JekyllUnitTest class TestFilters < JekyllUnitTest
class JekyllFilter class JekyllFilter
@ -8,18 +8,24 @@ class TestFilters < JekyllUnitTest
attr_accessor :site, :context attr_accessor :site, :context
def initialize(opts = {}) def initialize(opts = {})
@site = Jekyll::Site.new(Jekyll.configuration(opts.merge('skip_config_files' => true))) @site = Jekyll::Site.new(
Jekyll.configuration(opts.merge("skip_config_files" => true))
)
@context = Liquid::Context.new({}, {}, { :site => @site }) @context = Liquid::Context.new({}, {}, { :site => @site })
end end
end end
context "filters" do context "filters" do
setup do setup do
@filter = JekyllFilter.new({"source" => source_dir, "destination" => dest_dir, "timezone" => "UTC"}) @filter = JekyllFilter.new({
"source" => source_dir,
"destination" => dest_dir,
"timezone" => "UTC"
})
@sample_time = Time.utc(2013, 03, 27, 11, 22, 33) @sample_time = Time.utc(2013, 03, 27, 11, 22, 33)
@sample_date = Date.parse("2013-03-27") @sample_date = Date.parse("2013-03-27")
@time_as_string = "September 11, 2001 12:46:30 -0000" @time_as_string = "September 11, 2001 12:46:30 -0000"
@time_as_numeric = 1399680607 @time_as_numeric = 1_399_680_607
@array_of_objects = [ @array_of_objects = [
{ "color" => "red", "size" => "large" }, { "color" => "red", "size" => "large" },
{ "color" => "red", "size" => "medium" }, { "color" => "red", "size" => "medium" },
@ -28,18 +34,30 @@ class TestFilters < JekyllUnitTest
end end
should "markdownify with simple string" do should "markdownify with simple string" do
assert_equal "<p>something <strong>really</strong> simple</p>\n", @filter.markdownify("something **really** simple") assert_equal(
"<p>something <strong>really</strong> simple</p>\n",
@filter.markdownify("something **really** simple")
)
end end
context "smartify filter" do context "smartify filter" do
should "convert quotes and typographic characters" do should "convert quotes and typographic characters" do
assert_equal "SmartyPants is *not* Markdown", @filter.smartify("SmartyPants is *not* Markdown") assert_equal(
assert_equal "“This filters test…”", @filter.smartify(%q{"This filter's test..."}) "SmartyPants is *not* Markdown",
@filter.smartify("SmartyPants is *not* Markdown")
)
assert_equal(
"“This filters test…”",
@filter.smartify(%q{"This filter's test..."})
)
end end
should "escapes special characters when configured to do so" do should "escapes special characters when configured to do so" do
kramdown = JekyllFilter.new({ :kramdown => { :entity_output => :symbolic } }) kramdown = JekyllFilter.new({ :kramdown => { :entity_output => :symbolic } })
assert_equal "&ldquo;This filter&rsquo;s test&hellip;&rdquo;", kramdown.smartify(%q{"This filter's test..."}) assert_equal(
"&ldquo;This filter&rsquo;s test&hellip;&rdquo;",
kramdown.smartify(%q{"This filter's test..."})
)
end end
should "convert HTML entities to unicode characters" do should "convert HTML entities to unicode characters" do
@ -48,8 +66,14 @@ class TestFilters < JekyllUnitTest
end end
should "allow raw HTML passthrough" do should "allow raw HTML passthrough" do
assert_equal "Span HTML is <em>not</em> escaped", @filter.smartify("Span HTML is <em>not</em> escaped") assert_equal(
assert_equal "<div>Block HTML is not escaped</div>", @filter.smartify("<div>Block HTML is not escaped</div>") "Span HTML is <em>not</em> escaped",
@filter.smartify("Span HTML is <em>not</em> escaped")
)
assert_equal(
"<div>Block HTML is not escaped</div>",
@filter.smartify("<div>Block HTML is not escaped</div>")
)
end end
should "escape special characters" do should "escape special characters" do
@ -60,11 +84,17 @@ class TestFilters < JekyllUnitTest
end end
should "sassify with simple string" do should "sassify with simple string" do
assert_equal "p {\n color: #123456; }\n", @filter.sassify("$blue:#123456\np\n color: $blue") assert_equal(
"p {\n color: #123456; }\n",
@filter.sassify("$blue:#123456\np\n color: $blue")
)
end end
should "scssify with simple string" do should "scssify with simple string" do
assert_equal "p {\n color: #123456; }\n", @filter.scssify("$blue:#123456; p{color: $blue}") assert_equal(
"p {\n color: #123456; }\n",
@filter.scssify("$blue:#123456; p{color: $blue}")
)
end end
should "convert array to sentence string with no args" do should "convert array to sentence string with no args" do
@ -78,12 +108,15 @@ class TestFilters < JekyllUnitTest
should "convert array to sentence string with two args" do should "convert array to sentence string with two args" do
assert_equal "1 and 2", @filter.array_to_sentence_string([1, 2]) assert_equal "1 and 2", @filter.array_to_sentence_string([1, 2])
assert_equal "chunky and bacon", @filter.array_to_sentence_string(["chunky", "bacon"]) assert_equal "chunky and bacon", @filter.array_to_sentence_string(%w(chunky bacon))
end end
should "convert array to sentence string with multiple args" do should "convert array to sentence string with multiple args" do
assert_equal "1, 2, 3, and 4", @filter.array_to_sentence_string([1, 2, 3, 4]) assert_equal "1, 2, 3, and 4", @filter.array_to_sentence_string([1, 2, 3, 4])
assert_equal "chunky, bacon, bits, and pieces", @filter.array_to_sentence_string(["chunky", "bacon", "bits", "pieces"]) assert_equal(
"chunky, bacon, bits, and pieces",
@filter.array_to_sentence_string(%w(chunky bacon bits pieces))
)
end end
context "date filters" do context "date filters" do
@ -97,11 +130,17 @@ class TestFilters < JekyllUnitTest
end end
should "format a time with xmlschema" do should "format a time with xmlschema" do
assert_equal "2013-03-27T11:22:33+00:00", @filter.date_to_xmlschema(@sample_time) assert_equal(
"2013-03-27T11:22:33+00:00",
@filter.date_to_xmlschema(@sample_time)
)
end end
should "format a time according to RFC-822" do should "format a time according to RFC-822" do
assert_equal "Wed, 27 Mar 2013 11:22:33 +0000", @filter.date_to_rfc822(@sample_time) assert_equal(
"Wed, 27 Mar 2013 11:22:33 +0000",
@filter.date_to_rfc822(@sample_time)
)
end end
should "not modify a time in-place when using filters" do should "not modify a time in-place when using filters" do
@ -122,11 +161,17 @@ class TestFilters < JekyllUnitTest
end end
should "format a time with xmlschema" do should "format a time with xmlschema" do
assert_equal "2013-03-27T00:00:00+00:00", @filter.date_to_xmlschema(@sample_date) assert_equal(
"2013-03-27T00:00:00+00:00",
@filter.date_to_xmlschema(@sample_date)
)
end end
should "format a time according to RFC-822" do should "format a time according to RFC-822" do
assert_equal "Wed, 27 Mar 2013 00:00:00 +0000", @filter.date_to_rfc822(@sample_date) assert_equal(
"Wed, 27 Mar 2013 00:00:00 +0000",
@filter.date_to_rfc822(@sample_date)
)
end end
end end
@ -140,11 +185,17 @@ class TestFilters < JekyllUnitTest
end end
should "format a time with xmlschema" do should "format a time with xmlschema" do
assert_equal "2001-09-11T12:46:30+00:00", @filter.date_to_xmlschema(@time_as_string) assert_equal(
"2001-09-11T12:46:30+00:00",
@filter.date_to_xmlschema(@time_as_string)
)
end end
should "format a time according to RFC-822" do should "format a time according to RFC-822" do
assert_equal "Tue, 11 Sep 2001 12:46:30 +0000", @filter.date_to_rfc822(@time_as_string) assert_equal(
"Tue, 11 Sep 2001 12:46:30 +0000",
@filter.date_to_rfc822(@time_as_string)
)
end end
end end
@ -158,18 +209,27 @@ class TestFilters < JekyllUnitTest
end end
should "format a time with xmlschema" do should "format a time with xmlschema" do
assert_match(/2014-05-10T00:10:07/, @filter.date_to_xmlschema(@time_as_numeric)) assert_match(
"2014-05-10T00:10:07",
@filter.date_to_xmlschema(@time_as_numeric)
)
end end
should "format a time according to RFC-822" do should "format a time according to RFC-822" do
assert_equal "Sat, 10 May 2014 00:10:07 +0000", @filter.date_to_rfc822(@time_as_numeric) assert_equal(
"Sat, 10 May 2014 00:10:07 +0000",
@filter.date_to_rfc822(@time_as_numeric)
)
end end
end end
end end
should "escape xml with ampersands" do should "escape xml with ampersands" do
assert_equal "AT&amp;T", @filter.xml_escape("AT&T") assert_equal "AT&amp;T", @filter.xml_escape("AT&T")
assert_equal "&lt;code&gt;command &amp;lt;filename&amp;gt;&lt;/code&gt;", @filter.xml_escape("<code>command &lt;filename&gt;</code>") assert_equal(
"&lt;code&gt;command &amp;lt;filename&amp;gt;&lt;/code&gt;",
@filter.xml_escape("<code>command &lt;filename&gt;</code>")
)
end end
should "not error when xml escaping nil" do should "not error when xml escaping nil" do
@ -195,7 +255,10 @@ class TestFilters < JekyllUnitTest
should "convert array to json" do should "convert array to json" do
assert_equal "[1,2]", @filter.jsonify([1, 2]) assert_equal "[1,2]", @filter.jsonify([1, 2])
assert_equal "[{\"name\":\"Jack\"},{\"name\":\"Smith\"}]", @filter.jsonify([{:name => 'Jack'}, {:name => 'Smith'}]) assert_equal(
"[{\"name\":\"Jack\"},{\"name\":\"Smith\"}]",
@filter.jsonify([{ :name => "Jack" }, { :name => "Smith" }])
)
end end
should "convert drop to json" do should "convert drop to json" do
@ -240,6 +303,7 @@ class TestFilters < JekyllUnitTest
} }
end end
# rubocop:disable Style/StructInheritance
class M < Struct.new(:message) class M < Struct.new(:message)
def to_liquid def to_liquid
[message] [message]
@ -247,7 +311,12 @@ class TestFilters < JekyllUnitTest
end end
class T < Struct.new(:name) class T < Struct.new(:name)
def to_liquid def to_liquid
{ "name" => name, :v => 1, :thing => M.new({:kay => "jewelers"}), :stuff => true } {
"name" => name,
:v => 1,
:thing => M.new({ :kay => "jewelers" }),
:stuff => true
}
end end
end end
@ -277,6 +346,7 @@ class TestFilters < JekyllUnitTest
result = @filter.jsonify([T.new("Jeremiah"), T.new("Smathers")]) result = @filter.jsonify([T.new("Jeremiah"), T.new("Smathers")])
assert_equal expected, JSON.parse(result) assert_equal expected, JSON.parse(result)
end end
# rubocop:enable Style/StructInheritance
should "handle hashes with all sorts of weird keys and values" do should "handle hashes with all sorts of weird keys and values" do
my_hash = { "posts" => Array.new(3) { |i| T.new(i) } } my_hash = { "posts" => Array.new(3) { |i| T.new(i) } }
@ -324,16 +394,28 @@ class TestFilters < JekyllUnitTest
@filter.site.process @filter.site.process
grouping = @filter.group_by(@filter.site.pages, "layout") grouping = @filter.group_by(@filter.site.pages, "layout")
grouping.each do |g| grouping.each do |g|
assert ["default", "nil", ""].include?(g["name"]), "#{g['name']} isn't a valid grouping." assert(
["default", "nil", ""].include?(g["name"]),
"#{g["name"]} isn't a valid grouping."
)
case g["name"] case g["name"]
when "default" when "default"
assert g["items"].is_a?(Array), "The list of grouped items for 'default' is not an Array." assert(
g["items"].is_a?(Array),
"The list of grouped items for 'default' is not an Array."
)
assert_equal 5, g["items"].size assert_equal 5, g["items"].size
when "nil" when "nil"
assert g["items"].is_a?(Array), "The list of grouped items for 'nil' is not an Array." assert(
g["items"].is_a?(Array),
"The list of grouped items for 'nil' is not an Array."
)
assert_equal 2, g["items"].size assert_equal 2, g["items"].size
when "" when ""
assert g["items"].is_a?(Array), "The list of grouped items for '' is not an Array." assert(
g["items"].is_a?(Array),
"The list of grouped items for '' is not an Array."
)
assert_equal 13, g["items"].size assert_equal 13, g["items"].size
end end
end end
@ -343,7 +425,11 @@ class TestFilters < JekyllUnitTest
grouping = @filter.group_by(@filter.site.pages, "layout") grouping = @filter.group_by(@filter.site.pages, "layout")
grouping.each do |g| grouping.each do |g|
p g p g
assert_equal g["items"].size, g["size"], "The size property for '#{g["name"]}' doesn't match the size of the Array." assert_equal(
g["items"].size,
g["size"],
"The size property for '#{g["name"]}' doesn't match the size of the Array."
)
end end
end end
end end
@ -364,17 +450,29 @@ class TestFilters < JekyllUnitTest
end end
should "filter array properties appropriately" do should "filter array properties appropriately" do
hash = {"a"=>{"tags"=>["x","y"]}, "b"=>{"tags"=>["x"]}, "c"=>{"tags"=>["y","z"]}} hash = {
"a" => { "tags"=>%w(x y) },
"b" => { "tags"=>["x"] },
"c" => { "tags"=>%w(y z) }
}
assert_equal 2, @filter.where(hash, "tags", "x").length assert_equal 2, @filter.where(hash, "tags", "x").length
end end
should "filter array properties alongside string properties" do should "filter array properties alongside string properties" do
hash = {"a"=>{"tags"=>["x","y"]}, "b"=>{"tags"=>"x"}, "c"=>{"tags"=>["y","z"]}} hash = {
"a" => { "tags"=>%w(x y) },
"b" => { "tags"=>"x" },
"c" => { "tags"=>%w(y z) }
}
assert_equal 2, @filter.where(hash, "tags", "x").length assert_equal 2, @filter.where(hash, "tags", "x").length
end end
should "not match substrings" do should "not match substrings" do
hash = {"a"=>{"category"=>"bear"}, "b"=>{"category"=>"wolf"}, "c"=>{"category"=>["bear","lion"]}} hash = {
"a" => { "category"=>"bear" },
"b" => { "category"=>"wolf" },
"c" => { "category"=>%w(bear lion) }
}
assert_equal 0, @filter.where(hash, "category", "ear").length assert_equal 0, @filter.where(hash, "category", "ear").length
end end
@ -382,7 +480,7 @@ class TestFilters < JekyllUnitTest
hash = { hash = {
"The Words" => { "rating" => 1.2, "featured" => false }, "The Words" => { "rating" => 1.2, "featured" => false },
"Limitless" => { "rating" => 9.2, "featured" => true }, "Limitless" => { "rating" => 9.2, "featured" => true },
"Hustle" => {"rating" => 4.7, "featured" => true}, "Hustle" => { "rating" => 4.7, "featured" => true }
} }
results = @filter.where(hash, "featured", "true") results = @filter.where(hash, "featured", "true")
@ -404,18 +502,24 @@ class TestFilters < JekyllUnitTest
should "filter objects in a hash appropriately" do should "filter objects in a hash appropriately" do
hash = { "a"=>{ "color"=>"red" }, "b"=>{ "color"=>"blue" } } hash = { "a"=>{ "color"=>"red" }, "b"=>{ "color"=>"blue" } }
assert_equal 1, @filter.where_exp(hash, "item", "item.color == 'red'").length assert_equal 1, @filter.where_exp(hash, "item", "item.color == 'red'").length
assert_equal [{"color"=>"red"}], @filter.where_exp(hash, "item", "item.color == 'red'") assert_equal(
[{ "color"=>"red" }],
@filter.where_exp(hash, "item", "item.color == 'red'")
)
end end
should "filter objects appropriately" do should "filter objects appropriately" do
assert_equal 2, @filter.where_exp(@array_of_objects, "item", "item.color == 'red'").length assert_equal(
2,
@filter.where_exp(@array_of_objects, "item", "item.color == 'red'").length
)
end end
should "stringify during comparison for compatibility with liquid parsing" do should "stringify during comparison for compatibility with liquid parsing" do
hash = { hash = {
"The Words" => { "rating" => 1.2, "featured" => false }, "The Words" => { "rating" => 1.2, "featured" => false },
"Limitless" => { "rating" => 9.2, "featured" => true }, "Limitless" => { "rating" => 9.2, "featured" => true },
"Hustle" => {"rating" => 4.7, "featured" => true}, "Hustle" => { "rating" => 4.7, "featured" => true }
} }
results = @filter.where_exp(hash, "item", "item.featured == true") results = @filter.where_exp(hash, "item", "item.featured == true")
@ -465,15 +569,18 @@ class TestFilters < JekyllUnitTest
assert_equal [1, 2, 2.2, 3], @filter.sort([3, 2.2, 2, 1]) assert_equal [1, 2, 2.2, 3], @filter.sort([3, 2.2, 2, 1])
end end
should "return sorted strings" do should "return sorted strings" do
assert_equal ["10", "2"], @filter.sort(["10", "2"]) assert_equal %w(10 2), @filter.sort(%w(10 2))
assert_equal [{"a" => "10"}, {"a" => "2"}], @filter.sort([{"a" => "10"}, {"a" => "2"}], "a") assert_equal(
assert_equal ["FOO", "Foo", "foo"], @filter.sort(["foo", "Foo", "FOO"]) [{ "a" => "10" }, { "a" => "2" }],
assert_equal ["_foo", "foo", "foo_"], @filter.sort(["foo_", "_foo", "foo"]) @filter.sort([{ "a" => "10" }, { "a" => "2" }], "a")
)
assert_equal %w(FOO Foo foo), @filter.sort(%w(foo Foo FOO))
assert_equal %w(_foo foo foo_), @filter.sort(%w(foo_ _foo foo))
# Cyrillic # Cyrillic
assert_equal ["ВУЗ", "Вуз", "вуз"], @filter.sort(["Вуз", "вуз", "ВУЗ"]) assert_equal %w(ВУЗ Вуз вуз), @filter.sort(%w(Вуз вуз ВУЗ))
assert_equal ["уз", "вуз", "вуз_"], @filter.sort(["вуз_", "уз", "вуз"]) assert_equal %w(уз вуз вуз_), @filter.sort(%w(вуз_ _вуз вуз))
# Hebrew # Hebrew
assert_equal ["אלף", "בית"], @filter.sort(["בית", "אלף"]) assert_equal %w(אלף בית), @filter.sort(%w(בית אלף))
end end
should "return sorted by property array" do should "return sorted by property array" do
assert_equal [{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }], assert_equal [{ "a" => 1 }, { "a" => 2 }, { "a" => 3 }, { "a" => 4 }],
@ -512,41 +619,41 @@ class TestFilters < JekyllUnitTest
context "push filter" do context "push filter" do
should "return a new array with the element pushed to the end" do should "return a new array with the element pushed to the end" do
assert_equal %w{hi there bernie}, @filter.push(%w{hi there}, "bernie") assert_equal %w(hi there bernie), @filter.push(%w(hi there), "bernie")
end end
end end
context "pop filter" do context "pop filter" do
should "return a new array with the last element popped" do should "return a new array with the last element popped" do
assert_equal %w{hi there}, @filter.pop(%w{hi there bernie}) assert_equal %w(hi there), @filter.pop(%w(hi there bernie))
end end
should "allow multiple els to be popped" do should "allow multiple els to be popped" do
assert_equal %w{hi there bert}, @filter.pop(%w{hi there bert and ernie}, 2) assert_equal %w(hi there bert), @filter.pop(%w(hi there bert and ernie), 2)
end end
should "cast string inputs for # into nums" do should "cast string inputs for # into nums" do
assert_equal %w{hi there bert}, @filter.pop(%w{hi there bert and ernie}, "2") assert_equal %w(hi there bert), @filter.pop(%w(hi there bert and ernie), "2")
end end
end end
context "shift filter" do context "shift filter" do
should "return a new array with the element removed from the front" do should "return a new array with the element removed from the front" do
assert_equal %w{a friendly greeting}, @filter.shift(%w{just a friendly greeting}) assert_equal %w(a friendly greeting), @filter.shift(%w(just a friendly greeting))
end end
should "allow multiple els to be shifted" do should "allow multiple els to be shifted" do
assert_equal %w{bert and ernie}, @filter.shift(%w{hi there bert and ernie}, 2) assert_equal %w(bert and ernie), @filter.shift(%w(hi there bert and ernie), 2)
end end
should "cast string inputs for # into nums" do should "cast string inputs for # into nums" do
assert_equal %w{bert and ernie}, @filter.shift(%w{hi there bert and ernie}, "2") assert_equal %w(bert and ernie), @filter.shift(%w(hi there bert and ernie), "2")
end end
end end
context "unshift filter" do context "unshift filter" do
should "return a new array with the element put at the front" do should "return a new array with the element put at the front" do
assert_equal %w{aloha there bernie}, @filter.unshift(%w{there bernie}, "aloha") assert_equal %w(aloha there bernie), @filter.unshift(%w(there bernie), "aloha")
end end
end end
@ -563,6 +670,5 @@ class TestFilters < JekyllUnitTest
end end
end end
end end
end end
end end