1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
|
# coding: utf-8
require 'helper'
class TestFilters < JekyllUnitTest
class JekyllFilter
include Jekyll::Filters
attr_accessor :site, :context
def initialize(opts = {})
@site = Jekyll::Site.new(Jekyll.configuration(opts.merge('skip_config_files' => true)))
@context = Liquid::Context.new({}, {}, { :site => @site })
end
end
context "filters" do
setup do
@filter = JekyllFilter.new({"source" => source_dir, "destination" => dest_dir, "timezone" => "UTC"})
@sample_time = Time.utc(2013, 03, 27, 11, 22, 33)
@sample_date = Date.parse("2013-03-27")
@time_as_string = "September 11, 2001 12:46:30 -0000"
@time_as_numeric = 1399680607
@array_of_objects = [
{ "color" => "red", "size" => "large" },
{ "color" => "red", "size" => "medium" },
{ "color" => "blue", "size" => "medium" }
]
end
should "markdownify with simple string" do
assert_equal "<p>something <strong>really</strong> simple</p>\n", @filter.markdownify("something **really** simple")
end
context "smartify filter" do
should "convert quotes and typographic characters" do
assert_equal "SmartyPants is *not* Markdown", @filter.smartify("SmartyPants is *not* Markdown")
assert_equal "“This filter’s test…”", @filter.smartify(%q{"This filter's test..."})
end
should "escapes special characters when configured to do so" do
kramdown = JekyllFilter.new({:kramdown => {:entity_output => :symbolic}})
assert_equal "“This filter’s test…”", kramdown.smartify(%q{"This filter's test..."})
end
should "convert HTML entities to unicode characters" do
assert_equal "’", @filter.smartify("’")
assert_equal "“", @filter.smartify("“")
end
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 "<div>Block HTML is not escaped</div>", @filter.smartify("<div>Block HTML is not escaped</div>")
end
should "escape special characters" do
assert_equal "3 < 4", @filter.smartify("3 < 4")
assert_equal "5 > 4", @filter.smartify("5 > 4")
assert_equal "This & that", @filter.smartify("This & that")
end
end
should "sassify with simple string" do
assert_equal "p {\n color: #123456; }\n", @filter.sassify("$blue:#123456\np\n color: $blue")
end
should "scssify with simple string" do
assert_equal "p {\n color: #123456; }\n", @filter.scssify("$blue:#123456; p{color: $blue}")
end
should "convert array to sentence string with no args" do
assert_equal "", @filter.array_to_sentence_string([])
end
should "convert array to sentence string with one arg" do
assert_equal "1", @filter.array_to_sentence_string([1])
assert_equal "chunky", @filter.array_to_sentence_string(["chunky"])
end
should "convert array to sentence string with two args" do
assert_equal "1 and 2", @filter.array_to_sentence_string([1, 2])
assert_equal "chunky and bacon", @filter.array_to_sentence_string(["chunky", "bacon"])
end
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 "chunky, bacon, bits, and pieces", @filter.array_to_sentence_string(["chunky", "bacon", "bits", "pieces"])
end
context "date filters" do
context "with Time object" do
should "format a date with short format" do
assert_equal "27 Mar 2013", @filter.date_to_string(@sample_time)
end
should "format a date with long format" do
assert_equal "27 March 2013", @filter.date_to_long_string(@sample_time)
end
should "format a time with xmlschema" do
assert_equal "2013-03-27T11:22:33+00:00", @filter.date_to_xmlschema(@sample_time)
end
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)
end
end
context "with Date object" do
should "format a date with short format" do
assert_equal "27 Mar 2013", @filter.date_to_string(@sample_date)
end
should "format a date with long format" do
assert_equal "27 March 2013", @filter.date_to_long_string(@sample_date)
end
should "format a time with xmlschema" do
assert_equal "2013-03-27T00:00:00+00:00", @filter.date_to_xmlschema(@sample_date)
end
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)
end
end
context "with String object" do
should "format a date with short format" do
assert_equal "11 Sep 2001", @filter.date_to_string(@time_as_string)
end
should "format a date with long format" do
assert_equal "11 September 2001", @filter.date_to_long_string(@time_as_string)
end
should "format a time with xmlschema" do
assert_equal "2001-09-11T12:46:30+00:00", @filter.date_to_xmlschema(@time_as_string)
end
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)
end
end
context "with a Numeric object" do
should "format a date with short format" do
assert_equal "10 May 2014", @filter.date_to_string(@time_as_numeric)
end
should "format a date with long format" do
assert_equal "10 May 2014", @filter.date_to_long_string(@time_as_numeric)
end
should "format a time with xmlschema" do
assert_match /2014-05-10T00:10:07/, @filter.date_to_xmlschema(@time_as_numeric)
end
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)
end
end
end
should "escape xml with ampersands" do
assert_equal "AT&T", @filter.xml_escape("AT&T")
assert_equal "<code>command &lt;filename&gt;</code>", @filter.xml_escape("<code>command <filename></code>")
end
should "not error when xml escaping nil" do
assert_equal "", @filter.xml_escape(nil)
end
should "escape space as plus" do
assert_equal "my+things", @filter.cgi_escape("my things")
end
should "escape special characters" do
assert_equal "hey%21", @filter.cgi_escape("hey!")
end
should "escape space as %20" do
assert_equal "my%20things", @filter.uri_escape("my things")
end
context "jsonify filter" do
should "convert hash to json" do
assert_equal "{\"age\":18}", @filter.jsonify({:age => 18})
end
should "convert array to json" do
assert_equal "[1,2]", @filter.jsonify([1, 2])
assert_equal "[{\"name\":\"Jack\"},{\"name\":\"Smith\"}]", @filter.jsonify([{:name => 'Jack'}, {:name => 'Smith'}])
end
should "convert drop to json" do
@filter.site.read
expected = {
"path" => "_posts/2008-02-02-published.markdown",
"previous" => nil,
"output" => nil,
"content" => "This should be published.\n",
"id" => "/publish_test/2008/02/02/published",
"url" => "/publish_test/2008/02/02/published.html",
"relative_path" => "_posts/2008-02-02-published.markdown",
"collection" => "posts",
"excerpt" => "<p>This should be published.</p>\n",
"draft" => false,
"categories" => [
"publish_test"
],
"layout" => "default",
"title" => "Publish",
"category" => "publish_test",
"date" => "2008-02-02 00:00:00 +0000",
"slug" => "published",
"ext" => ".markdown",
"tags" => []
}
actual = JSON.parse(@filter.jsonify(@filter.site.docs_to_write.first.to_liquid))
next_doc = actual.delete("next")
refute_nil next_doc
assert next_doc.is_a?(Hash), "doc.next should be an object"
assert_equal expected, actual
end
should "convert drop with drops to json" do
@filter.site.read
actual = @filter.jsonify(@filter.site.to_liquid)
assert_equal JSON.parse(actual)["jekyll"], {
"environment" => "development",
"version" => Jekyll::VERSION
}
end
class M < Struct.new(:message)
def to_liquid
[message]
end
end
class T < Struct.new(:name)
def to_liquid
{ "name" => name, :v => 1, :thing => M.new({:kay => "jewelers"}), :stuff => true }
end
end
should "call #to_liquid " do
expected = [
{
"name" => "Jeremiah",
"v" => 1,
"thing" => [
{
"kay" => "jewelers"
}
],
"stuff" => true
},
{
"name" => "Smathers",
"v" => 1,
"thing" => [
{
"kay" => "jewelers"
}
],
"stuff" => true
}
]
result = @filter.jsonify([T.new("Jeremiah"), T.new("Smathers")])
assert_equal expected, JSON.parse(result)
end
should "handle hashes with all sorts of weird keys and values" do
my_hash = { "posts" => Array.new(3) { |i| T.new(i) } }
expected = {
"posts" => [
{
"name" => 0,
"v" => 1,
"thing" => [
{
"kay" => "jewelers"
}
],
"stuff" => true
},
{
"name" => 1,
"v" => 1,
"thing" => [
{
"kay" => "jewelers"
}
],
"stuff" => true
},
{
"name" => 2,
"v" => 1,
"thing" => [
{
"kay" => "jewelers"
}
],
"stuff" => true
}
]
}
result = @filter.jsonify(my_hash)
assert_equal expected, JSON.parse(result)
end
end
context "group_by filter" do
should "successfully group array of Jekyll::Page's" do
@filter.site.process
grouping = @filter.group_by(@filter.site.pages, "layout")
grouping.each do |g|
assert ["default", "nil", ""].include?(g["name"]), "#{g['name']} isn't a valid grouping."
case g["name"]
when "default"
assert g["items"].is_a?(Array), "The list of grouped items for 'default' is not an Array."
assert_equal 5, g["items"].size
when "nil"
assert g["items"].is_a?(Array), "The list of grouped items for 'nil' is not an Array."
assert_equal 2, g["items"].size
when ""
assert g["items"].is_a?(Array), "The list of grouped items for '' is not an Array."
assert_equal 13, g["items"].size
end
end
end
end
context "where filter" do
should "return any input that is not an array" do
assert_equal "some string", @filter.where("some string", "la", "le")
end
should "filter objects in a hash appropriately" do
hash = {"a"=>{"color"=>"red"}, "b"=>{"color"=>"blue"}}
assert_equal 1, @filter.where(hash, "color", "red").length
assert_equal [{"color"=>"red"}], @filter.where(hash, "color", "red")
end
should "filter objects appropriately" do
assert_equal 2, @filter.where(@array_of_objects, "color", "red").length
end
should "stringify during comparison for compatibility with liquid parsing" do
hash = {
"The Words" => {"rating" => 1.2, "featured" => false},
"Limitless" => {"rating" => 9.2, "featured" => true},
"Hustle" => {"rating" => 4.7, "featured" => true},
}
results = @filter.where(hash, "featured", "true")
assert_equal 2, results.length
assert_equal 9.2, results[0]["rating"]
assert_equal 4.7, results[1]["rating"]
results = @filter.where(hash, "rating", 4.7)
assert_equal 1, results.length
assert_equal 4.7, results[0]["rating"]
end
end
context "sort filter" do
should "raise Exception when input is nil" do
err = assert_raises ArgumentError do
@filter.sort(nil)
end
assert_equal "Cannot sort a null object.", err.message
end
should "return sorted numbers" do
assert_equal [1, 2, 2.2, 3], @filter.sort([3, 2.2, 2, 1])
end
should "return sorted strings" do
assert_equal ["10", "2"], @filter.sort(["10", "2"])
assert_equal [{"a" => "10"}, {"a" => "2"}], @filter.sort([{"a" => "10"}, {"a" => "2"}], "a")
assert_equal ["FOO", "Foo", "foo"], @filter.sort(["foo", "Foo", "FOO"])
assert_equal ["_foo", "foo", "foo_"], @filter.sort(["foo_", "_foo", "foo"])
# Cyrillic
assert_equal ["ВУЗ", "Вуз", "вуз"], @filter.sort(["Вуз", "вуз", "ВУЗ"])
assert_equal ["_вуз", "вуз", "вуз_"], @filter.sort(["вуз_", "_вуз", "вуз"])
# Hebrew
assert_equal ["אלף", "בית"], @filter.sort(["בית", "אלף"])
end
should "return sorted by property array" do
assert_equal [{"a" => 1}, {"a" => 2}, {"a" => 3}, {"a" => 4}],
@filter.sort([{"a" => 4}, {"a" => 3}, {"a" => 1}, {"a" => 2}], "a")
end
should "return sorted by property array with nils first" do
ary = [{"a" => 2}, {"b" => 1}, {"a" => 1}]
assert_equal [{"b" => 1}, {"a" => 1}, {"a" => 2}], @filter.sort(ary, "a")
assert_equal @filter.sort(ary, "a"), @filter.sort(ary, "a", "first")
end
should "return sorted by property array with nils last" do
assert_equal [{"a" => 1}, {"a" => 2}, {"b" => 1}],
@filter.sort([{"a" => 2}, {"b" => 1}, {"a" => 1}], "a", "last")
end
end
context "inspect filter" do
should "return a HTML-escaped string representation of an object" do
assert_equal "{"<a>"=>1}", @filter.inspect({ "<a>" => 1 })
end
end
context "slugify filter" do
should "return a slugified string" do
assert_equal "q-bert-says", @filter.slugify(" Q*bert says @!#?@!")
end
should "return a slugified string with mode" do
assert_equal "q-bert-says-@!-@!", @filter.slugify(" Q*bert says @!#?@!", "pretty")
end
end
context "push filter" 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")
end
end
context "pop filter" do
should "return a new array with the last element popped" do
assert_equal %w{hi there}, @filter.pop(%w{hi there bernie})
end
should "allow multiple els to be popped" do
assert_equal %w{hi there bert}, @filter.pop(%w{hi there bert and ernie}, 2)
end
should "cast string inputs for # into nums" do
assert_equal %w{hi there bert}, @filter.pop(%w{hi there bert and ernie}, "2")
end
end
context "shift filter" 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})
end
should "allow multiple els to be shifted" do
assert_equal %w{bert and ernie}, @filter.shift(%w{hi there bert and ernie}, 2)
end
should "cast string inputs for # into nums" do
assert_equal %w{bert and ernie}, @filter.shift(%w{hi there bert and ernie}, "2")
end
end
context "unshift filter" 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")
end
end
context "sample filter" do
should "return a random item from the array" do
input = %w(hey there bernie)
assert_includes input, @filter.sample(input)
end
should "allow sampling of multiple values (n > 1)" do
input = %w(hey there bernie)
@filter.sample(input, 2).each do |val|
assert_includes input, val
end
end
end
end
end
|