Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias Drop#invoke_drop to Drop#[] #6338

Merged
merged 7 commits into from Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/jekyll/drops/drop.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
76 changes: 59 additions & 17 deletions test/test_drop.rb
Expand Up @@ -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
Expand All @@ -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