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

Fix PrepareDefault#callable! default check with false #101

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/dry/initializer/dispatchers/prepare_default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def call(default: nil, optional: nil, **options)
private

def callable!(default)
return unless default
return if default.nil? # handles false case better than `unless default`
return default if default.respond_to?(:call)
return callable(default.to_proc) if default.respond_to?(:to_proc)

Expand All @@ -38,7 +38,7 @@ def check_arity!(default)

def invalid!(default)
raise TypeError, "The #{default.inspect} should be" \
" either convertable to proc with no arguments," \
" either convertible to proc with no arguments," \
" or respond to #call without arguments."
end
end
Expand Down
34 changes: 32 additions & 2 deletions spec/invalid_default_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# frozen_string_literal: true

RSpec.describe "invalid default value assignment" do
shared_examples "it has a TypeError" do
it "raises TypeError" do
expect { subject }.to raise_error TypeError
end
end

subject do
class Test::Foo
extend Dry::Initializer
Expand All @@ -9,7 +15,31 @@ class Test::Foo
end
end

it "raises TypeError" do
expect { subject }.to raise_error TypeError
it_behaves_like "it has a TypeError"

context "when default is false" do
subject do
class Test::Foo
extend Dry::Initializer

param :foo, default: false
end
end

it_behaves_like "it has a TypeError"
end

context "when default is a lambda returning false" do
subject do
class Test::Foo
extend Dry::Initializer

param :foo, default: -> { false }
end
end

it "does not raise TypeError" do
expect { subject }.not_to raise_error
end
end
end