parent
22f2724a1f
commit
1637f29d6c
|
@ -55,6 +55,7 @@ module Jekyll
|
|||
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 @@ module Jekyll
|
|||
#
|
||||
# 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))
|
||||
|
|
|
@ -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")
|
||||
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
|
||||
|
||||
should "fetch value without default" do
|
||||
assert_equal "Jekyll.configuration", @drop.fetch("title")
|
||||
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 if key is not found" do
|
||||
assert_equal "default", @drop.fetch("not_existing_key", "default")
|
||||
end
|
||||
should "fetch value without default" do
|
||||
assert_equal "Jekyll.configuration", @document_drop.fetch("title")
|
||||
end
|
||||
|
||||
should "fetch default boolean value correctly" do
|
||||
assert_equal false, @drop.fetch("bar", false)
|
||||
end
|
||||
should "fetch default if key is not found" do
|
||||
assert_equal "default", @document_drop.fetch("not_existing_key", "default")
|
||||
end
|
||||
|
||||
should "fetch default value from block if key is not found" do
|
||||
assert_equal "default bar", @drop.fetch("bar") { |el| "default #{el}" }
|
||||
end
|
||||
should "fetch default boolean value correctly" do
|
||||
assert_equal false, @document_drop.fetch("bar", false)
|
||||
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 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
|
||||
|
|
Loading…
Reference in New Issue