File: test_drop.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 (119 lines) | stat: -rw-r--r-- 3,108 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
# frozen_string_literal: true

require "helper"

class DropFixture < Jekyll::Drops::Drop
  mutable true

  def foo
    "bar"
  end

  def fallback_data
    @fallback_data ||= { "baz" => "buzz" }
  end
end

class TestDrop < JekyllUnitTest
  context "Drops" 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
      @document_drop = @document.to_liquid
      @drop = DropFixture.new({})
    end

    should "reject 'nil' key" do
      refute @drop.key?(nil)
    end

    should "return values for #[]" do
      assert_equal "bar", @drop["foo"]
    end

    should "return values for #invoke_drop" do
      assert_equal "bar", @drop.invoke_drop("foo")
    end

    context "mutations" do
      should "return mutations for #[]" do
        @drop["foo"] = "baz"
        assert_equal "baz", @drop["foo"]
      end

      should "return mutations for #invoke_drop" do
        @drop["foo"] = "baz"
        assert_equal "baz", @drop.invoke_drop("foo")
      end
    end

    context "a document drop" do
      context "fetch" do
        should "raise KeyError if key is not found and no default provided" do
          assert_raises KeyError do
            @document_drop.fetch("not_existing_key")
          end
        end

        should "fetch value without default" do
          assert_equal "Jekyll.configuration", @document_drop.fetch("title")
        end

        should "fetch default if key is not found" do
          assert_equal "default", @document_drop.fetch("not_existing_key", "default")
        end

        should "fetch default boolean value correctly" do
          assert_equal false, @document_drop.fetch("bar", false)
        end

        should "fetch default value from block if key is not found" do
          assert_equal "default bar", @document_drop.fetch("bar") { |el| "default #{el}" }
        end

        should "fetch default value from block first if both argument and block given" do
          assert_equal "baz", @document_drop.fetch("bar", "default") { "baz" }
        end

        should "not change mutability when fetching" do
          assert @drop.class.mutable?
          @drop["foo"] = "baz"
          assert_equal "baz", @drop.fetch("foo")
          assert @drop.class.mutable?
        end
      end
    end

    context "key?" do
      context "a mutable drop" do
        should "respond true for native methods" do
          assert @drop.key? "foo"
        end

        should "respond true for mutable keys" do
          @drop["bar"] = "baz"
          assert @drop.key? "bar"
        end

        should "return true for fallback data" do
          assert @drop.key? "baz"
        end
      end

      context "a document drop" do
        should "respond true for native methods" do
          assert @document_drop.key? "collection"
        end

        should "return true for fallback data" do
          assert @document_drop.key? "title"
        end
      end
    end
  end
end