Skip to content

Check the type of the default #435

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

Merged
merged 1 commit into from
Aug 27, 2014
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/thor/parser/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ def #{type}?

def validate!
fail ArgumentError, "An option cannot be boolean and required." if boolean? && required?
validate_default_type!
end

def validate_default_type!
default_type = case @default
when nil
return
when TrueClass, FalseClass
:boolean
when Numeric
:numeric
when Hash, Array, String
@default.class.name.downcase.to_sym
end
fail ArgumentError, "An option's default must match its type." unless default_type == @type
end

def dasherized?
Expand Down
6 changes: 6 additions & 0 deletions spec/parser/option_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ def option(name, options = {})
expect(option).to be_required
end

it "raises an error if default is inconsistent with type" do
expect do
option = option("foo", :type => :numeric, :default => "bar")
end.to raise_error(ArgumentError, "An option's default must match its type.")
end

it "boolean options cannot be required" do
expect do
option("foo", :required => true, :type => :boolean)
Expand Down