Convert ARGV test to spec.

This commit is contained in:
Markus Reiter 2017-02-11 03:07:23 +01:00
parent cdcf362fc0
commit 46a1e2f22d
2 changed files with 149 additions and 79 deletions

View File

@ -0,0 +1,149 @@
require "extend/ARGV"
describe HomebrewArgvExtension do
subject { argv.extend(HomebrewArgvExtension) }
let(:argv) { ["mxcl"] }
describe "#formulae" do
it "raises an error when a Formula is unavailable" do
expect { subject.formulae }.to raise_error FormulaUnavailableError
end
context "when there are no Formulae" do
let(:argv) { [] }
it "returns an empty array" do
expect(subject.formulae).to be_empty
end
end
end
describe "#casks" do
it "returns an empty array if there is no match" do
expect(subject.casks).to eq []
end
end
describe "#kegs" do
context "when there are matching Kegs" do
before(:each) do
keg = HOMEBREW_CELLAR + "mxcl/10.0"
keg.mkpath
end
it "returns an array of Kegs" do
expect(subject.kegs.length).to eq 1
end
end
context "when there are no matching Kegs" do
let(:argv) { [] }
it "returns an empty array" do
expect(subject.kegs).to be_empty
end
end
end
describe "#named" do
let(:argv) { ["foo", "--debug", "-v"] }
it "returns an array of non-option arguments" do
expect(subject.named).to eq ["foo"]
end
context "when there are no named arguments" do
let(:argv) { [] }
it "returns an empty array" do
expect(subject.named).to be_empty
end
end
end
describe "#options_only" do
let(:argv) { ["--foo", "-vds", "a", "b", "cdefg"] }
it "returns an array of option arguments" do
expect(subject.options_only).to eq ["--foo", "-vds"]
end
end
describe "#flags_only" do
let(:argv) { ["--foo", "-vds", "a", "b", "cdefg"] }
it "returns an array of flags" do
expect(subject.flags_only).to eq ["--foo"]
end
end
describe "#empty?" do
let(:argv) { [] }
it "returns true if it is empty" do
expect(subject).to be_empty
end
end
describe "#switch?" do
let(:argv) { ["-ns", "-i", "--bar", "-a-bad-arg"] }
it "returns true if the given string is a switch" do
%w[n s i].each do |s|
expect(subject.switch?(s)).to be true
end
end
it "returns false if the given string is not a switch" do
%w[b ns bar --bar -n a bad arg].each do |s|
expect(subject.switch?(s)).to be false
end
end
end
describe "#flag?" do
let(:argv) { ["--foo", "-bq", "--bar"] }
it "returns true if the given string is a flag" do
expect(subject.flag?("--foo")).to eq true
expect(subject.flag?("--bar")).to eq true
end
it "returns true if there is a switch with the same initial character" do
expect(subject.flag?("--baz")).to eq true
expect(subject.flag?("--qux")).to eq true
end
it "returns false if there is no matching flag" do
expect(subject.flag?("--frotz")).to eq false
expect(subject.flag?("--debug")).to eq false
end
end
describe "#value" do
let(:argv) { ["--foo=", "--bar=ab"] }
it "returns the value for a given string" do
expect(subject.value("foo")).to eq ""
expect(subject.value("bar")).to eq "ab"
end
it "returns nil if there is no matching argument" do
expect(subject.value("baz")).to be nil
end
end
describe "#values" do
let(:argv) { ["--foo=", "--bar=a", "--baz=b,c"] }
it "returns the value for a given argument" do
expect(subject.values("foo")).to eq []
expect(subject.values("bar")).to eq ["a"]
expect(subject.values("baz")).to eq ["b", "c"]
end
it "returns nil if there is no matching argument" do
expect(subject.values("qux")).to be nil
end
end
end

View File

@ -1,79 +0,0 @@
require "testing_env"
require "extend/ARGV"
class ArgvExtensionTests < Homebrew::TestCase
def setup
super
@argv = [].extend(HomebrewArgvExtension)
end
def test_argv_formulae
@argv.unshift "mxcl"
assert_raises(FormulaUnavailableError) { @argv.formulae }
end
def test_argv_casks
@argv.unshift "mxcl"
assert_equal [], @argv.casks
end
def test_argv_kegs
keg = HOMEBREW_CELLAR + "mxcl/10.0"
keg.mkpath
@argv << "mxcl"
assert_equal 1, @argv.kegs.length
end
def test_argv_named
@argv << "foo" << "--debug" << "-v"
assert_equal %w[foo], @argv.named
end
def test_options_only
@argv << "--foo" << "-vds" << "a" << "b" << "cdefg"
assert_equal %w[--foo -vds], @argv.options_only
end
def test_flags_only
@argv << "--foo" << "-vds" << "a" << "b" << "cdefg"
assert_equal %w[--foo], @argv.flags_only
end
def test_empty_argv
assert_empty @argv.named
assert_empty @argv.kegs
assert_empty @argv.formulae
assert_empty @argv
end
def test_switch?
@argv << "-ns" << "-i" << "--bar" << "-a-bad-arg"
%w[n s i].each { |s| assert @argv.switch?(s) }
%w[b ns bar --bar -n a bad arg].each { |s| assert !@argv.switch?(s) }
end
def test_flag?
@argv << "--foo" << "-bq" << "--bar"
assert @argv.flag?("--foo")
assert @argv.flag?("--bar")
assert @argv.flag?("--baz")
assert @argv.flag?("--qux")
assert !@argv.flag?("--frotz")
assert !@argv.flag?("--debug")
end
def test_value
@argv << "--foo=" << "--bar=ab"
assert_equal "", @argv.value("foo")
assert_equal "ab", @argv.value("bar")
assert_nil @argv.value("baz")
end
def test_values
@argv << "--foo=" << "--bar=a" << "--baz=b,c"
assert_equal [], @argv.values("foo")
assert_equal ["a"], @argv.values("bar")
assert_equal ["b", "c"], @argv.values("baz")
assert_nil @argv.values("qux")
end
end