From 1637f29d6c37ebcc1149d11384cbac0a842ade99 Mon Sep 17 00:00:00 2001 From: Ben Balter Date: Wed, 6 Sep 2017 12:52:34 -0400 Subject: [PATCH] Alias Drop#invoke_drop to Drop#[] (#6338) Merge pull request 6338 --- lib/jekyll/drops/drop.rb | 3 +- test/test_drop.rb | 76 +++++++++++++++++++++++++++++++--------- 2 files changed, 61 insertions(+), 18 deletions(-) diff --git a/lib/jekyll/drops/drop.rb b/lib/jekyll/drops/drop.rb index 09623ef9ac0..600e68a00e0 100644 --- a/lib/jekyll/drops/drop.rb +++ b/lib/jekyll/drops/drop.rb @@ -55,6 +55,7 @@ def [](key) fallback_data[key] end end + alias_method :invoke_drop, :[] # Set a field in the Drop. If mutable, sets in the mutations and # returns. If not mutable, checks first if it's trying to override a @@ -103,7 +104,7 @@ def content_methods # # Returns true if the given key is present def key?(key) - if self.class.mutable + if self.class.mutable? @mutations.key?(key) else !key.nil? && (respond_to?(key) || fallback_data.key?(key)) diff --git a/test/test_drop.rb b/test/test_drop.rb index eb23feb0f20..7bcd39714f1 100644 --- a/test/test_drop.rb +++ b/test/test_drop.rb @@ -2,6 +2,18 @@ require "helper" +class DropFixture < Jekyll::Drops::Drop + mutable true + + def foo + "bar" + end + + def fallback_data + @fallback_data ||= {} + end +end + class TestDrop < JekyllUnitTest context "a document drop" do setup do @@ -12,37 +24,67 @@ class TestDrop < JekyllUnitTest @document = @site.collections["methods"].docs.detect do |d| d.relative_path == "_methods/configuration.md" end - @drop = @document.to_liquid + @document_drop = @document.to_liquid + @drop = DropFixture.new({}) end should "reject 'nil' key" do refute @drop.key?(nil) end - should "raise KeyError if key is not found and no default provided" do - assert_raises KeyError do - @drop.fetch("not_existing_key") - end + should "return values for #[]" do + assert_equal "bar", @drop["foo"] end - should "fetch value without default" do - assert_equal "Jekyll.configuration", @drop.fetch("title") + should "return values for #invoke_drop" do + assert_equal "bar", @drop.invoke_drop("foo") end - should "fetch default if key is not found" do - assert_equal "default", @drop.fetch("not_existing_key", "default") - end + context "mutations" do + should "return mutations for #[]" do + @drop["foo"] = "baz" + assert_equal "baz", @drop["foo"] + end - should "fetch default boolean value correctly" do - assert_equal false, @drop.fetch("bar", false) + should "return mutations for #invoke_drop" do + @drop["foo"] = "baz" + assert_equal "baz", @drop.invoke_drop("foo") + end end - should "fetch default value from block if key is not found" do - assert_equal "default bar", @drop.fetch("bar") { |el| "default #{el}" } - end + 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 default value from block first if both argument and block given" do - assert_equal "baz", @drop.fetch("bar", "default") { "baz" } + 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 end