1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
# frozen_string_literal: true
RSpec.shared_context "with context for instance methods" do
# an instance of a class with instance methods setup to test memoization
let(:instance) { class_with_memo.new }
# a class with instance methods setup to test memoization
let(:class_with_memo) do
Class.new do
prepend MemoWise
DefineMethodsForTestingMemoWise.define_methods_for_testing_memo_wise(
target: self,
via: :instance
)
# Counter for calls to a protected method
def protected_memowise_method_counter
@protected_memowise_method_counter || 0
end
# A memoized protected method - only makes sense as an instance method
def protected_memowise_method
@protected_memowise_method_counter = protected_memowise_method_counter + 1
"protected_memowise_method"
end
protected :protected_memowise_method
memo_wise :protected_memowise_method
# Counter for calls to class method '.no_args', see below.
def self.class_no_args_counter
@class_no_args_counter || 0
end
# See: "with non-memoized method with same name as memoized method"
#
# Used by that spec to verify that `memo_wise :no_args` memoizes only the
# instance method, and not this class method sharing the same name.
def self.no_args
@class_no_args_counter = class_no_args_counter + 1
"class_no_args"
end
# Counter for calls to class method '.with_one_positional_arg', see below.
def self.class_one_positional_arg_counter
@class_one_positional_arg_counter || 0
end
# See: "with non-memoized method with same name as memoized method"
#
# Used by that spec to verify that `memo_wise :with_one_positional_arg`
# memoizes only the instance method, and not this class method sharing
# the same name.
def self.with_one_positional_arg(a) # rubocop:disable Naming/MethodParameterName
@class_one_positional_arg_counter = class_one_positional_arg_counter + 1
"class_with_one_positional_arg: a=#{a}"
end
# Counter for calls to class method '.with_positional_args', see below.
def self.class_positional_args_counter
@class_positional_args_counter || 0
end
# See: "with non-memoized method with same name as memoized method"
#
# Used by that spec to verify that `memo_wise :with_positional_args`
# memoizes only the instance method, and not this class method sharing
# the same name.
def self.with_positional_args(a, b) # rubocop:disable Naming/MethodParameterName
@class_positional_args_counter = class_positional_args_counter + 1
"class_with_positional_args: a=#{a}, b=#{b}"
end
end
end
end
|