2022-03-05 00:16:45 +00:00
|
|
|
require "../../spec_helper"
|
|
|
|
|
|
|
|
Spectator.describe Spectator::Arguments do
|
2022-10-23 21:22:50 +00:00
|
|
|
subject(arguments) { Spectator::Arguments.new({42, "foo"}, :splat, {:x, :y, :z}, {bar: "baz", qux: 123}) }
|
2022-03-05 00:16:45 +00:00
|
|
|
|
|
|
|
it "stores the arguments" do
|
2022-10-23 21:22:50 +00:00
|
|
|
expect(arguments).to have_attributes(
|
|
|
|
positional: {42, "foo"},
|
|
|
|
splat_name: :splat,
|
|
|
|
extra: {:x, :y, :z},
|
|
|
|
kwargs: {bar: "baz", qux: 123}
|
|
|
|
)
|
2022-03-05 00:16:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".capture" do
|
|
|
|
subject { Spectator::Arguments.capture(42, "foo", bar: "baz", qux: 123) }
|
|
|
|
|
|
|
|
it "stores the arguments and keyword arguments" do
|
2022-10-23 21:22:50 +00:00
|
|
|
is_expected.to have_attributes(positional: {42, "foo"}, kwargs: {bar: "baz", qux: 123})
|
2022-03-05 00:16:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-10 23:38:17 +00:00
|
|
|
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
|
|
|
|
|
2022-03-05 00:16:45 +00:00
|
|
|
describe "#to_s" do
|
|
|
|
subject { arguments.to_s }
|
|
|
|
|
|
|
|
it "formats the arguments" do
|
|
|
|
is_expected.to eq("(42, \"foo\", bar: \"baz\", qux: 123)")
|
|
|
|
end
|
2022-03-13 00:49:07 +00:00
|
|
|
|
|
|
|
context "when empty" do
|
2022-07-13 00:59:23 +00:00
|
|
|
let(arguments) { Spectator::Arguments.none }
|
2022-03-13 00:49:07 +00:00
|
|
|
|
|
|
|
it "returns (no args)" do
|
|
|
|
is_expected.to eq("(no args)")
|
|
|
|
end
|
|
|
|
end
|
2022-03-05 00:16:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#==" do
|
|
|
|
subject { arguments == other }
|
|
|
|
|
|
|
|
context "with equal arguments" do
|
|
|
|
let(other) { arguments }
|
|
|
|
|
|
|
|
it "returns true" do
|
|
|
|
is_expected.to be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with different arguments" do
|
2022-10-23 21:22:50 +00:00
|
|
|
let(other) { Spectator::Arguments.new({123, :foo, "bar"}, nil, nil, {opt: "foobar"}) }
|
2022-03-05 00:16:45 +00:00
|
|
|
|
|
|
|
it "returns false" do
|
|
|
|
is_expected.to be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with the same kwargs in a different order" do
|
2022-10-23 21:22:50 +00:00
|
|
|
let(other) { Spectator::Arguments.new(arguments.positional, nil, nil, {qux: 123, bar: "baz"}) }
|
2022-03-05 00:16:45 +00:00
|
|
|
|
|
|
|
it "returns true" do
|
|
|
|
is_expected.to be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a missing kwarg" do
|
2022-10-23 21:22:50 +00:00
|
|
|
let(other) { Spectator::Arguments.new(arguments.positional, nil, nil, {bar: "baz"}) }
|
2022-03-05 00:16:45 +00:00
|
|
|
|
|
|
|
it "returns false" do
|
|
|
|
is_expected.to be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#===" do
|
|
|
|
subject { pattern === arguments }
|
|
|
|
|
|
|
|
context "with equal arguments" do
|
|
|
|
let(pattern) { arguments }
|
|
|
|
|
|
|
|
it "returns true" do
|
|
|
|
is_expected.to be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with different arguments" do
|
2022-10-23 21:22:50 +00:00
|
|
|
let(pattern) { Spectator::Arguments.new({123, :foo, "bar"}, nil, nil, {opt: "foobar"}) }
|
2022-03-05 00:16:45 +00:00
|
|
|
|
|
|
|
it "returns false" do
|
|
|
|
is_expected.to be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with the same kwargs in a different order" do
|
2022-10-23 21:22:50 +00:00
|
|
|
let(pattern) { Spectator::Arguments.new(arguments.positional, nil, nil, {qux: 123, bar: "baz"}) }
|
2022-03-05 00:16:45 +00:00
|
|
|
|
|
|
|
it "returns true" do
|
|
|
|
is_expected.to be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with a missing kwarg" do
|
2022-10-23 21:22:50 +00:00
|
|
|
let(pattern) { Spectator::Arguments.new(arguments.positional, nil, nil, {bar: "baz"}) }
|
2022-03-05 00:16:45 +00:00
|
|
|
|
|
|
|
it "returns false" do
|
|
|
|
is_expected.to be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with matching types and regex" do
|
2022-10-23 21:22:50 +00:00
|
|
|
let(pattern) { Spectator::Arguments.new({Int32, /foo/}, nil, nil, {bar: String, qux: 123}) }
|
2022-03-05 00:16:45 +00:00
|
|
|
|
|
|
|
it "returns true" do
|
|
|
|
is_expected.to be_true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with different types and regex" do
|
2022-10-23 21:22:50 +00:00
|
|
|
let(pattern) { Spectator::Arguments.new({Symbol, /bar/}, nil, nil, {bar: String, qux: 42}) }
|
2022-03-05 00:16:45 +00:00
|
|
|
|
|
|
|
it "returns false" do
|
|
|
|
is_expected.to be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|