parser: add tests for named methods
This commit is contained in:
parent
da811373d3
commit
74c1010256
@ -519,7 +519,7 @@ module Homebrew
|
||||
def check_named_args(args)
|
||||
exception = if @min_named_args && args.size < @min_named_args
|
||||
if @named_args_type.present?
|
||||
types = @named_args_type.is_a?(Array) ? @named_args_type : [@named_args_type]
|
||||
types = Array(@named_args_type)
|
||||
if types.any? { |arg| arg.is_a? String }
|
||||
MinNamedArgumentsError.new(@min_named_args)
|
||||
else
|
||||
|
||||
@ -317,4 +317,128 @@ describe Homebrew::CLI::Parser do
|
||||
expect(args.named).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
describe "named_args" do
|
||||
let(:parser_none) {
|
||||
described_class.new do
|
||||
named_args :none
|
||||
end
|
||||
}
|
||||
let(:parser_number) {
|
||||
described_class.new do
|
||||
named_args number: 1
|
||||
end
|
||||
}
|
||||
|
||||
it "doesn't allow :none passed with a number" do
|
||||
expect do
|
||||
described_class.new do
|
||||
named_args :none, number: 1
|
||||
end
|
||||
end.to raise_error(ArgumentError, /Do not specify both `number`, `min` or `max` with `named_args :none`/)
|
||||
end
|
||||
|
||||
it "doesn't allow number and min" do
|
||||
expect do
|
||||
described_class.new do
|
||||
named_args number: 1, min: 1
|
||||
end
|
||||
end.to raise_error(ArgumentError, /Do not specify both `number` and `min` or `max`/)
|
||||
end
|
||||
|
||||
it "doesn't accept fewer than the passed number of arguments" do
|
||||
expect { parser_number.parse([]) }.to raise_error(Homebrew::CLI::MinNamedArgumentsError)
|
||||
end
|
||||
|
||||
it "doesn't accept more than the passed number of arguments" do
|
||||
expect { parser_number.parse(["foo", "bar"]) }.to raise_error(Homebrew::CLI::MaxNamedArgumentsError)
|
||||
end
|
||||
|
||||
it "accepts the passed number of arguments" do
|
||||
expect { parser_number.parse(["foo"]) }.not_to raise_error
|
||||
end
|
||||
|
||||
it "doesn't accept any arguments with :none" do
|
||||
expect { parser_none.parse(["foo"]) }
|
||||
.to raise_error(Homebrew::CLI::MaxNamedArgumentsError, /This command does not take named arguments/)
|
||||
end
|
||||
|
||||
it "accepts no arguments with :none" do
|
||||
expect { parser_none.parse([]) }.not_to raise_error
|
||||
end
|
||||
|
||||
it "displays the correct error message with an array of strings" do
|
||||
parser = described_class.new do
|
||||
named_args %w[on off], min: 1
|
||||
end
|
||||
expect { parser.parse([]) }.to raise_error(Homebrew::CLI::MinNamedArgumentsError)
|
||||
end
|
||||
|
||||
it "displays the correct error message with an array of symbols" do
|
||||
parser = described_class.new do
|
||||
named_args [:formula, :cask], min: 1
|
||||
end
|
||||
expect { parser.parse([]) }.to raise_error(UsageError, /this command requires a formula or cask argument/)
|
||||
end
|
||||
end
|
||||
|
||||
describe "named" do
|
||||
subject(:parser) {
|
||||
described_class.new do
|
||||
named 1
|
||||
end
|
||||
}
|
||||
|
||||
it "allows the specified number of arguments" do
|
||||
expect { parser.parse(["foo"]) }.not_to raise_error
|
||||
end
|
||||
|
||||
it "doesn't allow less than the specified number of arguments" do
|
||||
expect { parser.parse([]) }.to raise_error(Homebrew::CLI::MinNamedArgumentsError)
|
||||
end
|
||||
|
||||
it "doesn't allow more than the specified number of arguments" do
|
||||
expect { parser.parse(["foo", "bar"]) }.to raise_error(Homebrew::CLI::MaxNamedArgumentsError)
|
||||
end
|
||||
end
|
||||
|
||||
describe "min_named" do
|
||||
subject(:parser) {
|
||||
described_class.new do
|
||||
min_named 1
|
||||
end
|
||||
}
|
||||
|
||||
it "doesn't allow less than the minimum number of arguments" do
|
||||
expect { parser.parse([]) }.to raise_error(Homebrew::CLI::MinNamedArgumentsError)
|
||||
end
|
||||
|
||||
it "allows the minimum number of arguments" do
|
||||
expect { parser.parse(["foo"]) }.not_to raise_error
|
||||
end
|
||||
|
||||
it "allows more than the specified number of arguments" do
|
||||
expect { parser.parse(["foo", "bar"]) }.not_to raise_error
|
||||
end
|
||||
end
|
||||
|
||||
describe "max_named" do
|
||||
subject(:parser) {
|
||||
described_class.new do
|
||||
max_named 1
|
||||
end
|
||||
}
|
||||
|
||||
it "doesn't allow more than the minimum number of arguments" do
|
||||
expect { parser.parse(["foo", "bar"]) }.to raise_error(Homebrew::CLI::MaxNamedArgumentsError)
|
||||
end
|
||||
|
||||
it "allows the minimum number of arguments" do
|
||||
expect { parser.parse(["foo"]) }.not_to raise_error
|
||||
end
|
||||
|
||||
it "allows less than the specified number of arguments" do
|
||||
expect { parser.parse([]) }.not_to raise_error
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user