Class: MockSuey::MockContract
- Inherits:
-
Object
- Object
- MockSuey::MockContract
- Defined in:
- lib/mock_suey/mock_contract.rb
Defined Under Namespace
Classes: Error, NoMatchingMethodCalls, NoMatchingReturnType, NoMethodCalls
Constant Summary collapse
- ANYTHING =
Object.new.freeze
Instance Attribute Summary collapse
-
#args_pattern ⇒ Object
readonly
Returns the value of attribute args_pattern.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#receiver_class ⇒ Object
readonly
Returns the value of attribute receiver_class.
-
#return_type ⇒ Object
readonly
Returns the value of attribute return_type.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(receiver_class:, method_name:, args_pattern:, return_type:) ⇒ MockContract
constructor
A new instance of MockContract.
- #inspect ⇒ Object
- #method_desc ⇒ Object
- #noop? ⇒ Boolean
- #verify!(calls) ⇒ Object
Constructor Details
#initialize(receiver_class:, method_name:, args_pattern:, return_type:) ⇒ MockContract
Returns a new instance of MockContract.
99 100 101 102 103 104 |
# File 'lib/mock_suey/mock_contract.rb', line 99 def initialize(receiver_class:, method_name:, args_pattern:, return_type:) @receiver_class = receiver_class @method_name = method_name @args_pattern = args_pattern @return_type = return_type end |
Instance Attribute Details
#args_pattern ⇒ Object (readonly)
Returns the value of attribute args_pattern.
96 97 98 |
# File 'lib/mock_suey/mock_contract.rb', line 96 def args_pattern @args_pattern end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
96 97 98 |
# File 'lib/mock_suey/mock_contract.rb', line 96 def method_name @method_name end |
#receiver_class ⇒ Object (readonly)
Returns the value of attribute receiver_class.
96 97 98 |
# File 'lib/mock_suey/mock_contract.rb', line 96 def receiver_class @receiver_class end |
#return_type ⇒ Object (readonly)
Returns the value of attribute return_type.
96 97 98 |
# File 'lib/mock_suey/mock_contract.rb', line 96 def return_type @return_type end |
Class Method Details
.contractable_arg?(val) ⇒ Boolean
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/mock_suey/mock_contract.rb', line 83 def self.contractable_arg?(val) case val when TrueClass, FalseClass, Numeric, String, Regexp, NilClass true when Array val.all? { |v| contractable_arg?(v) } when Hash contractable_arg?(val.values) else false end end |
.from_stub(call_obj) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mock_suey/mock_contract.rb', line 68 def self.from_stub(call_obj) call_obj => {receiver_class:, method_name:, return_value:} args_pattern = call_obj.arguments.map do contractable_arg?(_1) ? _1 : ANYTHING end new( receiver_class:, method_name:, args_pattern:, return_type: return_value.class ) end |
Instance Method Details
#inspect ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/mock_suey/mock_contract.rb', line 129 def inspect args_pattern.map do (_1 == ANYTHING) ? "_" : _1.inspect end.join(", ").then do |args_desc| "#{method_desc}: (#{args_desc}) -> #{return_type}" end end |
#method_desc ⇒ Object
123 124 125 126 127 |
# File 'lib/mock_suey/mock_contract.rb', line 123 def method_desc delimeter = receiver_class.singleton_class? ? "." : "#" "#{receiver_class.instance_class_name}#{delimeter}#{method_name}" end |
#noop? ⇒ Boolean
121 |
# File 'lib/mock_suey/mock_contract.rb', line 121 def noop? = args_pattern.all? { _1 == ANYTHING } |
#verify!(calls) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/mock_suey/mock_contract.rb', line 106 def verify!(calls) return if noop? raise NoMethodCalls.new(self) if calls.nil? || calls.empty? matching_input_calls = calls.select { matching_args?(_1) } raise NoMatchingMethodCalls.new(self, calls) if matching_input_calls.empty? matching_input_calls.each do return if _1.return_value.class <= return_type end raise NoMatchingReturnType.new(self, matching_input_calls) end |