Alias Drop#invoke_drop to Drop#[] (#6338)

Merge pull request 6338
This commit is contained in:
Ben Balter 2017-09-06 12:52:34 -04:00 committed by jekyllbot
parent 22f2724a1f
commit 1637f29d6c
2 changed files with 62 additions and 19 deletions

View File

@ -55,6 +55,7 @@ module Jekyll
fallback_data[key] fallback_data[key]
end end
end end
alias_method :invoke_drop, :[]
# Set a field in the Drop. If mutable, sets in the mutations and # 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 # returns. If not mutable, checks first if it's trying to override a
@ -103,7 +104,7 @@ module Jekyll
# #
# Returns true if the given key is present # Returns true if the given key is present
def key?(key) def key?(key)
if self.class.mutable if self.class.mutable?
@mutations.key?(key) @mutations.key?(key)
else else
!key.nil? && (respond_to?(key) || fallback_data.key?(key)) !key.nil? && (respond_to?(key) || fallback_data.key?(key))

View File

@ -2,6 +2,18 @@
require "helper" require "helper"
class DropFixture < Jekyll::Drops::Drop
mutable true
def foo
"bar"
end
def fallback_data
@fallback_data ||= {}
end
end
class TestDrop < JekyllUnitTest class TestDrop < JekyllUnitTest
context "a document drop" do context "a document drop" do
setup do setup do
@ -12,37 +24,67 @@ class TestDrop < JekyllUnitTest
@document = @site.collections["methods"].docs.detect do |d| @document = @site.collections["methods"].docs.detect do |d|
d.relative_path == "_methods/configuration.md" d.relative_path == "_methods/configuration.md"
end end
@drop = @document.to_liquid @document_drop = @document.to_liquid
@drop = DropFixture.new({})
end end
should "reject 'nil' key" do should "reject 'nil' key" do
refute @drop.key?(nil) refute @drop.key?(nil)
end 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 "fetch" do
should "raise KeyError if key is not found and no default provided" do should "raise KeyError if key is not found and no default provided" do
assert_raises KeyError do assert_raises KeyError do
@drop.fetch("not_existing_key") @document_drop.fetch("not_existing_key")
end end
end end
should "fetch value without default" do should "fetch value without default" do
assert_equal "Jekyll.configuration", @drop.fetch("title") assert_equal "Jekyll.configuration", @document_drop.fetch("title")
end end
should "fetch default if key is not found" do should "fetch default if key is not found" do
assert_equal "default", @drop.fetch("not_existing_key", "default") assert_equal "default", @document_drop.fetch("not_existing_key", "default")
end end
should "fetch default boolean value correctly" do should "fetch default boolean value correctly" do
assert_equal false, @drop.fetch("bar", false) assert_equal false, @document_drop.fetch("bar", false)
end end
should "fetch default value from block if key is not found" do should "fetch default value from block if key is not found" do
assert_equal "default bar", @drop.fetch("bar") { |el| "default #{el}" } assert_equal "default bar", @document_drop.fetch("bar") { |el| "default #{el}" }
end end
should "fetch default value from block first if both argument and block given" do should "fetch default value from block first if both argument and block given" do
assert_equal "baz", @drop.fetch("bar", "default") { "baz" } 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 end
end end