File: test_document.rb

package info (click to toggle)
jekyll 3.9.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,604 kB
  • sloc: ruby: 15,325; javascript: 1,455; sh: 214; xml: 29; makefile: 7
file content (606 lines) | stat: -rw-r--r-- 18,005 bytes parent folder | download | duplicates (2)
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# frozen_string_literal: true

require "helper"

class TestDocument < JekyllUnitTest
  def assert_equal_value(key, one, other)
    assert_equal(one[key], other[key])
  end

  def setup_encoded_document(filename)
    site = fixture_site("collections" => ["encodings"])
    site.process
    Document.new(site.in_source_dir(File.join("_encodings", filename)), {
      :site       => site,
      :collection => site.collections["encodings"],
    }).tap(&:read)
  end

  def setup_document_with_dates(filename)
    site = fixture_site("collections" => ["dates"])
    site.process
    docs = nil
    with_env("TZ", "UTC") do
      docs = Document.new(site.in_source_dir(File.join("_dates", filename)), {
        :site       => site,
        :collection => site.collections["dates"],
      }).tap(&:read)
    end
    docs
  end

  context "a document in a collection" do
    setup do
      @site = fixture_site({
        "collections" => ["methods"],
      })
      @site.process
      @document = @site.collections["methods"].docs.detect do |d|
        d.relative_path == "_methods/configuration.md"
      end
    end

    should "exist" do
      assert !@document.nil?
    end

    should "know its relative path" do
      assert_equal "_methods/configuration.md", @document.relative_path
    end

    should "knows its extname" do
      assert_equal ".md", @document.extname
    end

    should "know its basename" do
      assert_equal "configuration.md", @document.basename
    end

    should "know its basename without extname" do
      assert_equal "configuration", @document.basename_without_ext
    end

    should "know whether it's a YAML file" do
      assert_equal false, @document.yaml_file?
    end

    should "know its data" do
      assert_equal "Jekyll.configuration", @document.data["title"]
      assert_equal "foo.bar", @document.data["whatever"]
    end

    should "be jsonify-able" do
      page_json = @document.to_liquid.to_json
      parsed = JSON.parse(page_json)

      assert_equal "Jekyll.configuration", parsed["title"]
      assert_equal "foo.bar", parsed["whatever"]
      assert_equal "_methods/collection/entries", parsed["previous"]["path"]
      assert_equal "Collection#entries", parsed["previous"]["title"]

      next_doc = parsed["next"]
      assert_equal "_methods/escape-+ #%20[].md", next_doc["path"]
      assert_equal "Jekyll.escape", next_doc["title"]

      next_prev_doc = next_doc["previous"]
      assert_equal "Jekyll.configuration", next_prev_doc["title"]
      assert_equal "_methods/configuration.md", next_prev_doc["path"]
      assert_equal "_methods/escape-+ #%20[].md", next_prev_doc["next"]["path"]
      assert_equal "_methods/collection/entries", next_prev_doc["previous"]["path"]
      assert_nil next_prev_doc["next"]["next"]
      assert_nil next_prev_doc["next"]["previous"]
      assert_nil next_prev_doc["next"]["content"]
      assert_nil next_prev_doc["next"]["excerpt"]
      assert_nil next_prev_doc["next"]["output"]

      next_next_doc = next_doc["next"]
      assert_equal "Jekyll.sanitized_path", next_next_doc["title"]
      assert_equal "_methods/sanitized_path.md", next_next_doc["path"]
      assert_equal "_methods/escape-+ #%20[].md", next_next_doc["previous"]["path"]
      assert_equal "_methods/site/generate.md", next_next_doc["next"]["path"]
      assert_nil next_next_doc["next"]["next"]
      assert_nil next_next_doc["next"]["previous"]
      assert_nil next_next_doc["next"]["content"]
      assert_nil next_next_doc["next"]["excerpt"]
      assert_nil next_next_doc["next"]["output"]
      assert_nil next_next_doc["previous"]["next"]
      assert_nil next_next_doc["previous"]["previous"]
      assert_nil next_next_doc["previous"]["content"]
      assert_nil next_next_doc["previous"]["excerpt"]
      assert_nil next_next_doc["previous"]["output"]
    end

    context "with YAML ending in three dots" do
      setup do
        @site = fixture_site({ "collections" => ["methods"] })
        @site.process
        @document = @site.collections["methods"].docs.detect do |d|
          d.relative_path == "_methods/yaml_with_dots.md"
        end
      end

      should "know its data" do
        assert_equal "YAML with Dots", @document.data["title"]
        assert_equal "foo.bar", @document.data["whatever"]
      end
    end

    should "output the collection name in the #to_liquid method" do
      assert_equal @document.to_liquid["collection"], "methods"
    end

    should "output its relative path as path in Liquid" do
      assert_equal @document.to_liquid["path"], "_methods/configuration.md"
    end
  end

  context "a document as part of a collection with front matter defaults" do
    setup do
      @site = fixture_site({
        "collections" => ["slides"],
        "defaults"    => [{
          "scope"  => { "path" => "", "type" => "slides" },
          "values" => {
            "nested" => {
              "key" => "myval",
            },
          },
        },],
      })
      @site.process
      @document = @site.collections["slides"].docs.select { |d| d.is_a?(Document) }.first
    end

    should "know the front matter defaults" do
      assert_equal "Example slide", @document.data["title"]
      assert_equal "slide", @document.data["layout"]
      assert_equal({ "key"=>"myval" }, @document.data["nested"])
    end

    should "return front matter defaults via to_liquid" do
      hash = @document.to_liquid
      assert hash.key? "nested"
      assert_equal({ "key"=>"myval" }, hash["nested"])
    end
  end

  context "a document as part of a collection with overridden default values" do
    setup do
      @site = fixture_site({
        "collections" => ["slides"],
        "defaults"    => [{
          "scope"  => { "path" => "", "type" => "slides" },
          "values" => {
            "nested" => {
              "test1" => "default1",
              "test2" => "default1",
            },
          },
        },],
      })
      @site.process
      @document = @site.collections["slides"].docs[1]
    end

    should "override default values in the document front matter" do
      assert_equal "Override title", @document.data["title"]
      assert_equal "slide", @document.data["layout"]
      assert_equal(
        { "test1" => "override1", "test2" => "override2" },
        @document.data["nested"]
      )
    end
  end

  context "a document as part of a collection with valid path" do
    setup do
      @site = fixture_site({
        "collections" => ["slides"],
        "defaults"    => [{
          "scope"  => { "path" => "_slides", "type" => "slides" },
          "values" => {
            "nested" => {
              "key" => "value123",
            },
          },
        },],
      })
      @site.process
      @document = @site.collections["slides"].docs.first
    end

    should "know the front matter defaults" do
      assert_equal "Example slide", @document.data["title"]
      assert_equal "slide", @document.data["layout"]
      assert_equal({ "key"=>"value123" }, @document.data["nested"])
    end
  end

  context "a document as part of a collection with invalid path" do
    setup do
      @site = fixture_site({
        "collections" => ["slides"],
        "defaults"    => [{
          "scope"  => { "path" => "somepath", "type" => "slides" },
          "values" => {
            "nested" => {
              "key" => "myval",
            },
          },
        },],
      })
      @site.process
      @document = @site.collections["slides"].docs.first
    end

    should "not know the specified front matter defaults" do
      assert_equal "Example slide", @document.data["title"]
      assert_equal "slide", @document.data["layout"]
      assert_nil @document.data["nested"]
    end
  end

  context "a document in a collection with a custom permalink" do
    setup do
      @site = fixture_site({
        "collections" => ["slides"],
      })
      @site.process
      @document = @site.collections["slides"].docs[2]
      @dest_file = dest_dir("slide/3/index.html")
    end

    should "know its permalink" do
      assert_equal "/slide/3/", @document.permalink
    end

    should "produce the right URL" do
      assert_equal "/slide/3/", @document.url
    end
  end

  context "a document in a collection with custom filename permalinks" do
    setup do
      @site = fixture_site({
        "collections" => {
          "slides" => {
            "output"    => true,
            "permalink" => "/slides/test/:name",
          },
        },
        "permalink"   => "pretty",
      })
      @site.process
      @document = @site.collections["slides"].docs[0]
      @dest_file = dest_dir("slides/test/example-slide-1.html")
    end

    should "produce the right URL" do
      assert_equal "/slides/test/example-slide-1", @document.url
    end

    should "produce the right destination file" do
      assert_equal @dest_file, @document.destination(dest_dir)
    end

    should "honor the output extension of its permalink" do
      assert_equal ".html", @document.output_ext
    end
  end

  context "a document in a collection with pretty permalink style" do
    setup do
      @site = fixture_site({
        "collections" => {
          "slides" => {
            "output" => true,
          },
        },
      })
      @site.permalink_style = :pretty
      @site.process
      @document = @site.collections["slides"].docs[0]
      @dest_file = dest_dir("slides/example-slide-1/index.html")
    end

    should "produce the right URL" do
      assert_equal "/slides/example-slide-1/", @document.url
    end

    should "produce the right destination file" do
      assert_equal @dest_file, @document.destination(dest_dir)
    end
  end

  context "a document in a collection with cased file name" do
    setup do
      @site = fixture_site({
        "collections" => {
          "slides" => {
            "output" => true,
          },
        },
      })
      @site.permalink_style = :pretty
      @site.process
      @document = @site.collections["slides"].docs[7]
      @dest_file = dest_dir("slides/example-slide-Upper-Cased/index.html")
    end

    should "produce the right cased URL" do
      assert_equal "/slides/example-slide-Upper-Cased/", @document.url
    end
  end

  context "a document in a collection with cased file name" do
    setup do
      @site = fixture_site({
        "collections" => {
          "slides" => {
            "output" => true,
          },
        },
      })
      @site.process
      @document = @site.collections["slides"].docs[6]
      @dest_file = dest_dir("slides/example-slide-7.php")
    end

    should "be written out properly" do
      assert_exist @dest_file
    end

    should "produce the permalink as the url" do
      assert_equal "/slides/example-slide-7.php", @document.url
    end

    should "be written to the proper directory" do
      assert_equal @dest_file, @document.destination(dest_dir)
    end

    should "honor the output extension of its permalink" do
      assert_equal ".php", @document.output_ext
    end
  end

  context "documents in a collection with custom title permalinks" do
    setup do
      @site = fixture_site({
        "collections" => {
          "slides" => {
            "output"    => true,
            "permalink" => "/slides/:title",
          },
        },
      })
      @site.process
      @document = @site.collections["slides"].docs[3]
      @document_without_slug = @site.collections["slides"].docs[4]
      @document_with_strange_slug = @site.collections["slides"].docs[5]
    end

    should "produce the right URL if they have a slug" do
      assert_equal "/slides/so-what-is-jekyll-exactly", @document.url
    end

    should "produce the right destination file if they have a slug" do
      dest_file = dest_dir("slides/so-what-is-jekyll-exactly.html")
      assert_equal dest_file, @document.destination(dest_dir)
    end

    should "produce the right URL if they don't have a slug" do
      assert_equal "/slides/example-slide-5", @document_without_slug.url
    end
    should "produce the right destination file if they don't have a slug" do
      dest_file = dest_dir("slides/example-slide-5.html")
      assert_equal dest_file, @document_without_slug.destination(dest_dir)
    end

    should "produce the right URL if they have a wild slug" do
      assert_equal(
        "/slides/Well,-so-what-is-Jekyll,-then",
        @document_with_strange_slug.url
      )
    end
    should "produce the right destination file if they have a wild slug" do
      dest_file = dest_dir("/slides/Well,-so-what-is-Jekyll,-then.html")
      assert_equal dest_file, @document_with_strange_slug.destination(dest_dir)
    end
  end

  context "document with a permalink with dots & a trailing slash" do
    setup do
      @site = fixture_site({ "collections" => {
        "with.dots" => { "output" => true },
      }, })
      @site.process
      @document = @site.collections["with.dots"].docs.last
      @dest_file = dest_dir("with.dots", "permalink.with.slash.tho", "index.html")
    end

    should "yield an HTML document" do
      assert_equal @dest_file, @document.destination(dest_dir)
    end

    should "be written properly" do
      assert_exist @dest_file
    end

    should "get the right output_ext" do
      assert_equal ".html", @document.output_ext
    end
  end

  context "documents in a collection" do
    setup do
      @site = fixture_site({
        "collections" => {
          "slides" => {
            "output" => true,
          },
        },
      })
      @site.process
      @files = @site.collections["slides"].docs
    end

    context "without output overrides" do
      should "be output according to collection defaults" do
        refute_nil @files.find do |doc|
          doc.relative_path == "_slides/example-slide-4.html"
        end
        assert_exist dest_dir("slides/example-slide-4.html")
      end
    end

    context "with output overrides" do
      should "be output according its front matter" do
        assert @files.find do |doc|
          doc.relative_path == "_slides/non-outputted-slide.html"
        end
        refute_exist dest_dir("slides/non-outputted-slide.html")
      end
    end
  end

  context "a static file in a collection" do
    setup do
      @site = fixture_site({
        "collections" => {
          "slides" => {
            "output" => true,
          },
        },
      })
      @site.process
      @document = @site.collections["slides"].files.find do |doc|
        doc.relative_path == "_slides/octojekyll.png"
      end
      @dest_file = dest_dir("slides/octojekyll.png")
    end

    should "be a static file" do
      assert_equal true, @document.is_a?(StaticFile)
    end

    should "be set to write" do
      assert @document.write?
    end

    should "be in the list of docs_to_write" do
      assert @site.docs_to_write.include?(@document)
    end

    should "be output in the correct place" do
      assert File.file?(@dest_file)
    end
  end

  context "a document in a collection with non-alphabetic file name" do
    setup do
      @site = fixture_site({
        "collections" => {
          "methods" => {
            "output" => true,
          },
        },
      })
      @site.process
      @document = @site.collections["methods"].docs.find do |doc|
        doc.relative_path == "_methods/escape-+ #%20[].md"
      end
      @dest_file = dest_dir("methods/escape-+ #%20[].html")
    end

    should "produce the right URL" do
      assert_equal "/methods/escape-+%20%23%2520%5B%5D.html", @document.url
    end

    should "produce the right destination" do
      assert_equal @dest_file, @document.destination(dest_dir)
    end

    should "be output in the correct place" do
      assert_equal true, File.file?(@dest_file)
    end
  end

  context "a document in a collection with dash-separated numeric file name" do
    setup do
      @site = fixture_site({
        "collections" => {
          "methods" => {
            "output" => true,
          },
        },
      })
      @site.process
      @document = @site.collections["methods"].docs.find do |doc|
        doc.relative_path == "_methods/3940394-21-9393050-fifif1323-test.md"
      end
      @dest_file = dest_dir("methods/3940394-21-9393050-fifif1323-test.html")
    end

    should "produce the right URL" do
      assert_equal "/methods/3940394-21-9393050-fifif1323-test.html", @document.url
    end

    should "produce the right destination" do
      assert_equal @dest_file, @document.destination(dest_dir)
    end

    should "be output in the correct place" do
      assert_equal true, File.file?(@dest_file)
    end
  end

  context "a document with UTF-8 CLRF" do
    setup do
      @document = setup_encoded_document "UTF8CRLFandBOM.md"
    end

    should "not throw an error" do
      Jekyll::Renderer.new(@document.site, @document).render_document
    end
  end

  context "a document with UTF-16LE CLRF" do
    setup do
      @document = setup_encoded_document "Unicode16LECRLFandBOM.md"
    end

    should "not throw an error" do
      Jekyll::Renderer.new(@document.site, @document).render_document
    end
  end

  context "a document with a date with timezone" do
    setup do
      @document = setup_document_with_dates "time_with_timezone.md"
    end

    should "have the expected date" do
      assert_equal "2015/09/30", @document.data["date"].strftime("%Y/%m/%d")
    end
  end

  context "a document with a date with time but without timezone" do
    setup do
      @document = setup_document_with_dates "time_without_timezone.md"
    end

    should "have the expected date" do
      assert_equal "2015/10/01", @document.data["date"].strftime("%Y/%m/%d")
    end
  end

  context "a document with a date without time" do
    setup do
      @document = setup_document_with_dates "date_without_time.md"
    end

    should "have the expected date" do
      assert_equal "2015/10/01", @document.data["date"].strftime("%Y/%m/%d")
    end
  end
end