From 74c101025616c98bc5774a9a84a7c34311c886d1 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Thu, 14 Jan 2021 15:14:10 -0500 Subject: [PATCH] parser: add tests for named methods --- Library/Homebrew/cli/parser.rb | 2 +- Library/Homebrew/test/cli/parser_spec.rb | 124 +++++++++++++++++++++++ 2 files changed, 125 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index d2454f0bc5..5ebd993613 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -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 diff --git a/Library/Homebrew/test/cli/parser_spec.rb b/Library/Homebrew/test/cli/parser_spec.rb index f737b4b8ce..0a4ca16436 100644 --- a/Library/Homebrew/test/cli/parser_spec.rb +++ b/Library/Homebrew/test/cli/parser_spec.rb @@ -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