Skip to content

Commit

Permalink
Add test for positional and keyword args and appease RuboCop
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobEvelyn committed Sep 22, 2020
1 parent fd97d19 commit 4765542
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/memo_wise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ def initialize(*values)
super
end

def self.has_arg?(method)
def self.has_arg?(method) # rubocop:disable Naming/PredicateName
method.parameters.any? do |(param, _)|
param == :req || param == :opt || param == :rest # rubocop:disable Style/MultipleComparison
end
end

def self.has_kwarg?(method)
def self.has_kwarg?(method) # rubocop:disable Naming/PredicateName
method.parameters.any? do |(param, _)|
param == :keyreq || param == :key || param == :keyrest # rubocop:disable Style/MultipleComparison
end
Expand Down Expand Up @@ -94,7 +94,7 @@ def #{method_name}#{args_str}
end
end

def reset_memo_wise(method_name, *args, **kwargs) # rubocop:disable Metrics/PerceivedComplexity
def reset_memo_wise(method_name, *args, **kwargs)
unless method_name.is_a?(Symbol)
raise ArgumentError, "#{method_name.inspect} must be a Symbol"
end
Expand Down
121 changes: 121 additions & 0 deletions spec/memo_wise_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
def initialize
@no_args_counter = 0
@with_positional_args_counter = 0
@with_positional_and_splat_args_counter = 0
@with_keyword_args_counter = 0
@with_keyword_and_double_splat_args_counter = 0
@with_positional_and_keyword_args_counter = 0
@with_positional_splat_keyword_and_double_splat_args_counter = 0
@special_chars_counter = 0
@false_method_counter = 0
@nil_method_counter = 0
Expand All @@ -19,7 +23,11 @@ def initialize

attr_reader :no_args_counter,
:with_positional_args_counter,
:with_positional_and_splat_args_counter,
:with_keyword_args_counter,
:with_keyword_and_double_splat_args_counter,
:with_positional_and_keyword_args_counter,
:with_positional_splat_keyword_and_double_splat_args_counter,
:special_chars_counter,
:false_method_counter,
:nil_method_counter,
Expand All @@ -39,12 +47,42 @@ def with_positional_args(a, b) # rubocop:disable Naming/MethodParameterName
end
memo_wise :with_positional_args

def with_positional_and_splat_args(a, *args) # rubocop:disable Naming/MethodParameterName
@with_positional_and_splat_args_counter += 1
"with_positional_and_splat_args: a=#{a}, args=#{args}"
end
memo_wise :with_positional_and_splat_args

def with_keyword_args(a:, b:) # rubocop:disable Naming/MethodParameterName
@with_keyword_args_counter += 1
"with_keyword_args: a=#{a}, b=#{b}"
end
memo_wise :with_keyword_args

def with_keyword_and_double_splat_args(a:, **kwargs) # rubocop:disable Naming/MethodParameterName
@with_keyword_and_double_splat_args_counter += 1
"with_keyword_and_double_splat_args: a=#{a}, kwargs=#{kwargs}"
end
memo_wise :with_keyword_and_double_splat_args

def with_positional_and_keyword_args(a, b:) # rubocop:disable Naming/MethodParameterName
@with_positional_and_keyword_args_counter += 1
"with_positional_and_keyword_args: a=#{a}, b=#{b}"
end
memo_wise :with_positional_and_keyword_args

def with_positional_splat_keyword_and_double_splat_args(
a, # rubocop:disable Naming/MethodParameterName
*args,
b:, # rubocop:disable Naming/MethodParameterName
**kwargs
)
@with_positional_splat_keyword_and_double_splat_args_counter += 1
"with_positional_splat_keyword_and_double_splat_args: "\
"a=#{a}, args=#{args} b=#{b} kwargs=#{kwargs}"
end
memo_wise :with_positional_splat_keyword_and_double_splat_args

def special_chars?
@special_chars_counter += 1
"special_chars?"
Expand Down Expand Up @@ -105,6 +143,17 @@ def public_memowise_method
expect(instance.with_positional_args_counter).to eq(2)
end

it "memoizes methods with positional and splat arguments" do
expect(Array.new(4) { instance.with_positional_and_splat_args(1, 2, 3) }).
to all eq("with_positional_and_splat_args: a=1, args=[2, 3]")

expect(Array.new(4) { instance.with_positional_and_splat_args(1, 3, 4) }).
to all eq("with_positional_and_splat_args: a=1, args=[3, 4]")

# This should be executed once for each set of arguments passed
expect(instance.with_positional_and_splat_args_counter).to eq(2)
end

it "memoizes methods with keyword arguments" do
expect(Array.new(4) { instance.with_keyword_args(a: 1, b: 2) }).
to all eq("with_keyword_args: a=1, b=2")
Expand All @@ -116,6 +165,78 @@ def public_memowise_method
expect(instance.with_keyword_args_counter).to eq(2)
end

it "memoizes methods with keyword and double-splat arguments" do
expect(
Array.new(4) do
instance.with_keyword_and_double_splat_args(a: 1, b: 2, c: 3)
end
).to all eq(
"with_keyword_and_double_splat_args: a=1, kwargs={:b=>2, :c=>3}"
)

expect(
Array.new(4) do
instance.with_keyword_and_double_splat_args(a: 1, b: 2, c: 4)
end
).to all eq(
"with_keyword_and_double_splat_args: a=1, kwargs={:b=>2, :c=>4}"
)

# This should be executed once for each set of arguments passed
expect(instance.with_keyword_and_double_splat_args_counter).to eq(2)
end

it "memoizes methods with positional and keyword arguments" do
expect(
Array.new(4) { instance.with_positional_and_keyword_args(1, b: 2) }
).to all eq("with_positional_and_keyword_args: a=1, b=2")

expect(
Array.new(4) { instance.with_positional_and_keyword_args(2, b: 3) }
).to all eq("with_positional_and_keyword_args: a=2, b=3")

# This should be executed once for each set of arguments passed
expect(instance.with_positional_and_keyword_args_counter).to eq(2)
end

it "memoizes methods with positional, splat, keyword, and double-splat "\
"arguments" do
expect(
Array.new(4) do
instance.with_positional_splat_keyword_and_double_splat_args(
1,
2,
3,
b: 4,
c: 5,
d: 6
)
end
).to all eq(
"with_positional_splat_keyword_and_double_splat_args: "\
"a=1, args=[2, 3] b=4 kwargs={:c=>5, :d=>6}"
)

expect(
Array.new(4) do
instance.with_positional_splat_keyword_and_double_splat_args(
1,
2,
b: 4,
c: 5
)
end
).to all eq(
"with_positional_splat_keyword_and_double_splat_args: "\
"a=1, args=[2] b=4 kwargs={:c=>5}"
)

# This should be executed once for each set of arguments passed
expect(
instance.with_positional_splat_keyword_and_double_splat_args_counter
).to eq(2)
end

it "memoizes methods with special characters in the name" do
expect(Array.new(4) { instance.special_chars? }).
to all eq("special_chars?")
Expand Down

0 comments on commit 4765542

Please sign in to comment.