Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dduugg committed Mar 7, 2024
1 parent ce11300 commit 3b207dd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Library/Homebrew/abstract_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def cmd_args(&block)
sig { returns(CLI::Args) }
attr_reader :args

sig { void }
def initialize
@args = T.let(CLI::Parser.new(&self.class.parser_block).parse, CLI::Args)
sig { params(argv: T::Array[String]).void }
def initialize(argv = ARGV.freeze)
@args = T.let(CLI::Parser.new(&self.class.parser_block).parse(argv), CLI::Args)
end

sig { abstract.void }
Expand Down
56 changes: 56 additions & 0 deletions Library/Homebrew/test/abstract_command_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require "abstract_command"

RSpec.describe Homebrew::AbstractCommand do
describe "subclasses" do
before do
cat = Class.new(described_class) do
cmd_args do
switch "--foo"
flag "--bar="
end
def run; end
end
stub_const("Cat", cat)
end

describe "parsing args" do
it "parses valid args" do
expect { Cat.new(["--foo"]).run }.not_to raise_error
end

it "allows access to args" do
expect(Cat.new(["--bar", "baz"]).args[:bar]).to eq("baz")
end

it "raises on invalid args" do
expect { Cat.new(["--bat"]) }.to raise_error(OptionParser::InvalidOption)
end
end

describe "command names" do
it "has a default command name" do
expect(Cat.command_name).to eq("cat")
end

it "can lookup command" do
expect(described_class.command("cat")).to be(Cat)
end

describe "when command name is overridden" do
before do
tac = Class.new(described_class) do
def self.command_name = "t-a-c"
def run; end
end
stub_const("Tac", tac)
end

it "can be looked up by command name" do
expect(described_class.command("t-a-c")).to be(Tac)
end
end
end
end
end
4 changes: 4 additions & 0 deletions Library/Homebrew/test/abstract_command_spec.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# typed: strict

class Cat < Homebrew::AbstractCommand; end
class Tac < Homebrew::AbstractCommand; end

0 comments on commit 3b207dd

Please sign in to comment.