Add indexer methods for captured arguments

This commit is contained in:
Michael Miller 2022-07-10 17:38:17 -06:00
parent 4aaed186c3
commit 29389f1dbf
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
2 changed files with 30 additions and 0 deletions

View file

@ -24,6 +24,26 @@ Spectator.describe Spectator::Arguments do
end
end
describe "#[]" do
context "with an index" do
it "returns a positional argument" do
aggregate_failures do
expect(arguments[0]).to eq(42)
expect(arguments[1]).to eq("foo")
end
end
end
context "with a symbol" do
it "returns a named argument" do
aggregate_failures do
expect(arguments[:bar]).to eq("baz")
expect(arguments[:qux]).to eq(123)
end
end
end
end
describe "#to_s" do
subject { arguments.to_s }

View file

@ -27,6 +27,16 @@ module Spectator
{{@type.name(generic_args: false)}}.capture
end
# Returns the positional argument at the specified index.
def [](index : Int)
@args[index]
end
# Returns the specified named argument.
def [](arg : Symbol)
@kwargs[arg]
end
# Constructs a string representation of the arguments.
def to_s(io : IO) : Nil
return io << "(no args)" if args.empty? && kwargs.empty?