Add fetch method to Drops
This commit is contained in:
parent
51a80b8225
commit
72d49490d2
|
@ -199,6 +199,10 @@ module Jekyll
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
def fetch(*args, &block)
|
||||
to_h.fetch(*args, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
require "helper"
|
||||
|
||||
class TestDrop < JekyllUnitTest
|
||||
context "a document drop" 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
|
||||
@drop = @document.to_liquid
|
||||
end
|
||||
|
||||
should "raise KeyError if key is not found and no default provided" do
|
||||
assert_raises KeyError do
|
||||
@drop.fetch("not_existing_key")
|
||||
end
|
||||
end
|
||||
|
||||
should "fetch value without default" do
|
||||
assert_equal "Jekyll.configuration", @drop.fetch("title")
|
||||
end
|
||||
|
||||
should "fetch default if key is not found" do
|
||||
assert_equal "default", @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
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue