File: test_drop.rb

package info (click to toggle)
jekyll 4.3.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,356 kB
  • sloc: ruby: 16,765; javascript: 1,455; sh: 214; xml: 29; makefile: 9
file content (154 lines) | stat: -rw-r--r-- 4,468 bytes parent folder | download | duplicates (3)
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
# frozen_string_literal: true

require "helper"

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

  attr_accessor :lipsum

  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

    should "return array of strings for .getter_methods" do
      assert(@drop.class.getter_method_names.all? { |entry| entry.is_a?(String) })
    end

    should "return array of only getter method name strings for .getter_methods" do
      [:lipsum, :lipsum=].each { |id| assert_includes(@drop.class.instance_methods, id) }

      assert_includes @drop.class.getter_method_names, "lipsum"
      refute_includes @drop.class.getter_method_names, "lipsum="
    end

    should "not munge results for another Jekyll::Drops::Drop subclass" do
      fixture_ids = [:lipsum, :lipsum=, :foo]
      fixture_getter_names = %w(lipsum foo)
      fixture_ids.each { |id| assert_includes(@drop.class.instance_methods, id) }

      fixture_getter_names.each do |name|
        assert_includes @drop.class.getter_method_names, name
        refute_includes @document_drop.class.getter_method_names, name
      end
    end

    should "return only getter method names for #content_methods" do
      drop_base_class_method_names = Jekyll::Drops::Drop.instance_methods.map(&:to_s)
      sample_method_names = ["lipsum=", "fallback_data", "collapse_document"]

      (sample_method_names + drop_base_class_method_names).each do |entry|
        refute_includes @drop.content_methods, entry
      end

      assert_equal %w(foo lipsum), @drop.content_methods.sort
    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
          refute @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