mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Rename attributes
This commit is contained in:
parent
163f94287e
commit
a728a037d4
2 changed files with 53 additions and 58 deletions
|
@ -5,9 +5,9 @@ Spectator.describe Spectator::Arguments do
|
||||||
|
|
||||||
it "stores the arguments" do
|
it "stores the arguments" do
|
||||||
expect(arguments).to have_attributes(
|
expect(arguments).to have_attributes(
|
||||||
positional: {42, "foo"},
|
args: {42, "foo"},
|
||||||
splat_name: :splat,
|
splat_name: :splat,
|
||||||
extra: {:x, :y, :z},
|
splat: {:x, :y, :z},
|
||||||
kwargs: {bar: "baz", qux: 123}
|
kwargs: {bar: "baz", qux: 123}
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
@ -16,7 +16,7 @@ Spectator.describe Spectator::Arguments do
|
||||||
subject { Spectator::Arguments.capture(42, "foo", bar: "baz", qux: 123) }
|
subject { Spectator::Arguments.capture(42, "foo", bar: "baz", qux: 123) }
|
||||||
|
|
||||||
it "stores the arguments and keyword arguments" do
|
it "stores the arguments and keyword arguments" do
|
||||||
is_expected.to have_attributes(positional: {42, "foo"}, kwargs: {bar: "baz", qux: 123})
|
is_expected.to have_attributes(args: {42, "foo"}, kwargs: {bar: "baz", qux: 123})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ Spectator.describe Spectator::Arguments do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with the same kwargs in a different order" do
|
context "with the same kwargs in a different order" do
|
||||||
let(other) { Spectator::Arguments.new(arguments.positional, nil, nil, {qux: 123, bar: "baz"}) }
|
let(other) { Spectator::Arguments.new(arguments.args, nil, nil, {qux: 123, bar: "baz"}) }
|
||||||
|
|
||||||
it "returns true" do
|
it "returns true" do
|
||||||
is_expected.to be_true
|
is_expected.to be_true
|
||||||
|
@ -84,7 +84,7 @@ Spectator.describe Spectator::Arguments do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with a missing kwarg" do
|
context "with a missing kwarg" do
|
||||||
let(other) { Spectator::Arguments.new(arguments.positional, nil, nil, {bar: "baz"}) }
|
let(other) { Spectator::Arguments.new(arguments.args, nil, nil, {bar: "baz"}) }
|
||||||
|
|
||||||
it "returns false" do
|
it "returns false" do
|
||||||
is_expected.to be_false
|
is_expected.to be_false
|
||||||
|
@ -112,7 +112,7 @@ Spectator.describe Spectator::Arguments do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with the same kwargs in a different order" do
|
context "with the same kwargs in a different order" do
|
||||||
let(pattern) { Spectator::Arguments.new(arguments.positional, nil, nil, {qux: 123, bar: "baz"}) }
|
let(pattern) { Spectator::Arguments.new(arguments.args, nil, nil, {qux: 123, bar: "baz"}) }
|
||||||
|
|
||||||
it "returns true" do
|
it "returns true" do
|
||||||
is_expected.to be_true
|
is_expected.to be_true
|
||||||
|
@ -120,7 +120,7 @@ Spectator.describe Spectator::Arguments do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with a missing kwarg" do
|
context "with a missing kwarg" do
|
||||||
let(pattern) { Spectator::Arguments.new(arguments.positional, nil, nil, {bar: "baz"}) }
|
let(pattern) { Spectator::Arguments.new(arguments.args, nil, nil, {bar: "baz"}) }
|
||||||
|
|
||||||
it "returns false" do
|
it "returns false" do
|
||||||
is_expected.to be_false
|
is_expected.to be_false
|
||||||
|
|
|
@ -4,15 +4,15 @@ module Spectator
|
||||||
# Arguments used in a method call.
|
# Arguments used in a method call.
|
||||||
#
|
#
|
||||||
# Can also be used to match arguments.
|
# Can also be used to match arguments.
|
||||||
# *Positional* must be a `Tuple` or `NamedTuple` type representing the standard arguments.
|
# *Args* must be a `Tuple` or `NamedTuple` type representing the standard arguments.
|
||||||
# *Splat* must be a `Tuple` type representing the extra positional arguments.
|
# *Splat* must be a `Tuple` type representing the extra positional arguments.
|
||||||
# *DoubleSplat* must be a `NamedTuple` type representing extra keyword arguments.
|
# *DoubleSplat* must be a `NamedTuple` type representing extra keyword arguments.
|
||||||
class Arguments(Positional, Splat, DoubleSplat) < AbstractArguments
|
class Arguments(Args, Splat, DoubleSplat) < AbstractArguments
|
||||||
# Positional arguments.
|
# Positional arguments.
|
||||||
getter positional : Positional
|
getter args : Args
|
||||||
|
|
||||||
# Additional positional arguments.
|
# Additional positional arguments.
|
||||||
getter extra : Splat
|
getter splat : Splat
|
||||||
|
|
||||||
# Keyword arguments.
|
# Keyword arguments.
|
||||||
getter kwargs : DoubleSplat
|
getter kwargs : DoubleSplat
|
||||||
|
@ -21,12 +21,12 @@ module Spectator
|
||||||
getter splat_name : Symbol?
|
getter splat_name : Symbol?
|
||||||
|
|
||||||
# Creates arguments used in a method call.
|
# Creates arguments used in a method call.
|
||||||
def initialize(@positional : Positional, @splat_name : Symbol?, @extra : Splat, @kwargs : DoubleSplat)
|
def initialize(@args : Args, @splat_name : Symbol?, @splat : Splat, @kwargs : DoubleSplat)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Creates arguments used in a method call.
|
# Creates arguments used in a method call.
|
||||||
def self.new(positional : Positional, kwargs : DoubleSplat)
|
def self.new(args : Args, kwargs : DoubleSplat)
|
||||||
new(positional, nil, nil, kwargs)
|
new(args, nil, nil, kwargs)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Instance of empty arguments.
|
# Instance of empty arguments.
|
||||||
|
@ -38,13 +38,13 @@ module Spectator
|
||||||
end
|
end
|
||||||
|
|
||||||
# Captures arguments passed to a call.
|
# Captures arguments passed to a call.
|
||||||
def self.build(positional = Tuple.new, kwargs = NamedTuple.new)
|
def self.build(args = Tuple.new, kwargs = NamedTuple.new)
|
||||||
new(positional, nil, nil, kwargs)
|
new(args, nil, nil, kwargs)
|
||||||
end
|
end
|
||||||
|
|
||||||
# :ditto:
|
# :ditto:
|
||||||
def self.build(positional : NamedTuple, splat_name : Symbol, extra : Tuple, kwargs = NamedTuple.new)
|
def self.build(args : NamedTuple, splat_name : Symbol, splat : Tuple, kwargs = NamedTuple.new)
|
||||||
new(positional, splat_name, extra, kwargs)
|
new(args, splat_name, splat, kwargs)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Friendlier constructor for capturing arguments.
|
# Friendlier constructor for capturing arguments.
|
||||||
|
@ -54,49 +54,54 @@ module Spectator
|
||||||
|
|
||||||
# Returns the positional argument at the specified index.
|
# Returns the positional argument at the specified index.
|
||||||
def [](index : Int)
|
def [](index : Int)
|
||||||
{% if Positional < NamedTuple %}
|
positional[index]
|
||||||
@positional.values[index]
|
|
||||||
{% else %}
|
|
||||||
@positional[index]
|
|
||||||
{% end %}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the specified named argument.
|
# Returns the specified named argument.
|
||||||
def [](arg : Symbol)
|
def [](arg : Symbol)
|
||||||
{% if Positional < NamedTuple %}
|
{% if Args < NamedTuple %}
|
||||||
return @positional[arg] if @positional.has_key?(arg)
|
return @args[arg] if @args.has_key?(arg)
|
||||||
{% end %}
|
{% end %}
|
||||||
@kwargs[arg]
|
@kwargs[arg]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns all arguments and splatted arguments as a tuple.
|
||||||
|
def positional : Tuple
|
||||||
|
if (splat = @splat)
|
||||||
|
{% if Args < NamedTuple %}args.values{% else %}args{% end %} + splat
|
||||||
|
else
|
||||||
|
{% if Args < NamedTuple %}args.values{% else %}args{% end %}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Constructs a string representation of the arguments.
|
# Constructs a string representation of the arguments.
|
||||||
def to_s(io : IO) : Nil
|
def to_s(io : IO) : Nil
|
||||||
return io << "(no args)" if positional.empty? && ((extra = @extra).nil? || extra.empty?) && kwargs.empty?
|
return io << "(no args)" if args.empty? && ((splat = @splat).nil? || splat.empty?) && kwargs.empty?
|
||||||
|
|
||||||
io << '('
|
io << '('
|
||||||
|
|
||||||
# Add the positional arguments.
|
# Add the positional arguments.
|
||||||
{% if Positional < NamedTuple %}
|
{% if Args < NamedTuple %}
|
||||||
# Include argument names.
|
# Include argument names.
|
||||||
positional.each_with_index do |name, value, i|
|
args.each_with_index do |name, value, i|
|
||||||
io << ", " if i > 0
|
io << ", " if i > 0
|
||||||
io << name << ": "
|
io << name << ": "
|
||||||
value.inspect(io)
|
value.inspect(io)
|
||||||
end
|
end
|
||||||
{% else %}
|
{% else %}
|
||||||
positional.each_with_index do |arg, i|
|
args.each_with_index do |arg, i|
|
||||||
io << ", " if i > 0
|
io << ", " if i > 0
|
||||||
arg.inspect(io)
|
arg.inspect(io)
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
# Add the splat arguments.
|
# Add the splat arguments.
|
||||||
if (extra = @extra) && !extra.empty?
|
if (splat = @splat) && !splat.empty?
|
||||||
io << ", " unless positional.empty?
|
io << ", " unless args.empty?
|
||||||
if splat = @splat_name
|
if name = @splat_name
|
||||||
io << '*' << splat << ": {"
|
io << '*' << name << ": {"
|
||||||
end
|
end
|
||||||
extra.each_with_index do |arg, i|
|
splat.each_with_index do |arg, i|
|
||||||
io << ", " if i > 0
|
io << ", " if i > 0
|
||||||
arg.inspect(io)
|
arg.inspect(io)
|
||||||
end
|
end
|
||||||
|
@ -104,8 +109,8 @@ module Spectator
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add the keyword arguments.
|
# Add the keyword arguments.
|
||||||
offset = positional.size
|
offset = args.size
|
||||||
offset += extra.size if (extra = @extra)
|
offset += splat.size if (splat = @splat)
|
||||||
kwargs.each_with_index(offset) do |name, value, i|
|
kwargs.each_with_index(offset) do |name, value, i|
|
||||||
io << ", " if i > 0
|
io << ", " if i > 0
|
||||||
io << name << ": "
|
io << name << ": "
|
||||||
|
@ -117,31 +122,29 @@ module Spectator
|
||||||
|
|
||||||
# Checks if this set of arguments and another are equal.
|
# Checks if this set of arguments and another are equal.
|
||||||
def ==(other : Arguments)
|
def ==(other : Arguments)
|
||||||
ordered = simplify_positional
|
positional == other.positional && kwargs == other.kwargs
|
||||||
other_ordered = other.simplify_positional
|
|
||||||
ordered == other_ordered && kwargs == other.kwargs
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Checks if another set of arguments matches this set of arguments.
|
# Checks if another set of arguments matches this set of arguments.
|
||||||
def ===(other : Arguments)
|
def ===(other : Arguments)
|
||||||
{% if Positional < NamedTuple %}
|
{% if Args < NamedTuple %}
|
||||||
if (other_positional = other.positional).is_a?(NamedTuple)
|
if (other_args = other.args).is_a?(NamedTuple)
|
||||||
positional.each do |k, v|
|
args.each do |k, v|
|
||||||
return false unless other_positional.has_key?(k)
|
return false unless other_args.has_key?(k)
|
||||||
return false unless v === other_positional[k]
|
return false unless v === other_args[k]
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
return false if positional.size != other_positional
|
return false if args.size != other_args
|
||||||
positional.each_with_index do |k, v, i|
|
args.each_with_index do |k, v, i|
|
||||||
return false unless v === other_positional.unsafe_fetch(i)
|
return false unless v === other_args.unsafe_fetch(i)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
{% else %}
|
{% else %}
|
||||||
return false unless positional === other.simplify_positional
|
return false unless args === other.positional
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
if extra = @extra
|
if splat = @splat
|
||||||
return false unless extra === other.extra
|
return false unless splat === other.splat
|
||||||
end
|
end
|
||||||
|
|
||||||
kwargs.each do |k, v|
|
kwargs.each do |k, v|
|
||||||
|
@ -151,13 +154,5 @@ module Spectator
|
||||||
|
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
protected def simplify_positional
|
|
||||||
if (extra = @extra)
|
|
||||||
{% if Positional < NamedTuple %}positional.values{% else %}positional{% end %} + extra
|
|
||||||
else
|
|
||||||
{% if Positional < NamedTuple %}positional.values{% else %}positional{% end %}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue