Class: MinitestToRspec::Model::Call
- Defined in:
- lib/minitest_to_rspec/model/call.rb
Overview
Data object. Represents a ‘:call` s-expression.
Direct Known Subclasses
MinitestToRspec::Model::Calls::Once, MinitestToRspec::Model::Calls::Returns, MinitestToRspec::Model::Calls::Twice
Instance Attribute Summary collapse
-
#original ⇒ Object
readonly
Returns the value of attribute original.
Class Method Summary collapse
- .assert_difference?(exp) ⇒ Boolean
- .assert_no_difference?(exp) ⇒ Boolean
- .assert_nothing_raised?(exp) ⇒ Boolean
- .assert_raise?(exp) ⇒ Boolean
- .assert_raises?(exp) ⇒ Boolean
- .method_name?(exp, name) ⇒ Boolean
Instance Method Summary collapse
- #argument_types ⇒ Object
- #arguments ⇒ Object
- #assert_difference? ⇒ Boolean
- #assert_no_difference? ⇒ Boolean
- #assert_nothing_raised? ⇒ Boolean
- #assert_raise? ⇒ Boolean
- #assert_raises? ⇒ Boolean
- #calls_in_receiver_chain ⇒ Object
-
#initialize(exp) ⇒ Call
constructor
A new instance of Call.
- #method_name ⇒ Object
- #num_arguments ⇒ Object
- #one_string_argument? ⇒ Boolean
- #question_mark_method? ⇒ Boolean
-
#raise_error_args? ⇒ Boolean
Returns true if arguments can be processed into RSpec’s ‘raise_error` matcher.
- #receiver ⇒ Object
-
#receiver_call ⇒ Object
While ‘#receiver` returns a `Sexp`, `#receiver_call` returns a `Model::Call`.
-
#receiver_chain ⇒ Object
Consider the following chain of method calls:.
- #receiver_chain_include?(method_name) ⇒ Boolean
- #require_test_helper? ⇒ Boolean
Methods included from SexpAssertions
#assert_sexp_type, #assert_sexp_type_array, #sexp_type?
Constructor Details
#initialize(exp) ⇒ Call
Returns a new instance of Call.
11 12 13 14 15 |
# File 'lib/minitest_to_rspec/model/call.rb', line 11 def initialize(exp) assert_sexp_type(:call, exp) @exp = exp.dup @original = exp.dup end |
Instance Attribute Details
#original ⇒ Object (readonly)
Returns the value of attribute original.
9 10 11 |
# File 'lib/minitest_to_rspec/model/call.rb', line 9 def original @original end |
Class Method Details
.assert_difference?(exp) ⇒ Boolean
18 19 20 |
# File 'lib/minitest_to_rspec/model/call.rb', line 18 def assert_difference?(exp) exp.sexp_type == :call && new(exp).assert_difference? end |
.assert_no_difference?(exp) ⇒ Boolean
22 23 24 |
# File 'lib/minitest_to_rspec/model/call.rb', line 22 def assert_no_difference?(exp) exp.sexp_type == :call && new(exp).assert_no_difference? end |
.assert_nothing_raised?(exp) ⇒ Boolean
26 27 28 |
# File 'lib/minitest_to_rspec/model/call.rb', line 26 def assert_nothing_raised?(exp) exp.sexp_type == :call && new(exp).assert_nothing_raised? end |
.assert_raise?(exp) ⇒ Boolean
30 31 32 |
# File 'lib/minitest_to_rspec/model/call.rb', line 30 def assert_raise?(exp) exp.sexp_type == :call && new(exp).assert_raise? end |
.assert_raises?(exp) ⇒ Boolean
34 35 36 |
# File 'lib/minitest_to_rspec/model/call.rb', line 34 def assert_raises?(exp) exp.sexp_type == :call && new(exp).assert_raises? end |
.method_name?(exp, name) ⇒ Boolean
38 39 40 |
# File 'lib/minitest_to_rspec/model/call.rb', line 38 def method_name?(exp, name) exp.sexp_type == :call && new(exp).method_name.to_s == name.to_s end |
Instance Method Details
#argument_types ⇒ Object
47 48 49 |
# File 'lib/minitest_to_rspec/model/call.rb', line 47 def argument_types arguments.map(&:sexp_type) end |
#arguments ⇒ Object
43 44 45 |
# File 'lib/minitest_to_rspec/model/call.rb', line 43 def arguments @exp[3..-1] || [] end |
#assert_difference? ⇒ Boolean
51 52 53 54 |
# File 'lib/minitest_to_rspec/model/call.rb', line 51 def assert_difference? return false unless method_name == :assert_difference [[:str], [:str, :lit]].include?(argument_types) end |
#assert_no_difference? ⇒ Boolean
56 57 58 59 60 |
# File 'lib/minitest_to_rspec/model/call.rb', line 56 def assert_no_difference? method_name == :assert_no_difference && arguments.length == 1 && arguments[0].sexp_type == :str end |
#assert_nothing_raised? ⇒ Boolean
62 63 64 |
# File 'lib/minitest_to_rspec/model/call.rb', line 62 def assert_nothing_raised? method_name == :assert_nothing_raised && arguments.empty? end |
#assert_raise? ⇒ Boolean
66 67 68 |
# File 'lib/minitest_to_rspec/model/call.rb', line 66 def assert_raise? method_name == :assert_raise && raise_error_args? end |
#assert_raises? ⇒ Boolean
70 71 72 |
# File 'lib/minitest_to_rspec/model/call.rb', line 70 def assert_raises? method_name == :assert_raises && raise_error_args? end |
#calls_in_receiver_chain ⇒ Object
74 75 76 77 78 79 |
# File 'lib/minitest_to_rspec/model/call.rb', line 74 def calls_in_receiver_chain receiver_chain .compact .select { |r| sexp_type?(:call, r) } .map { |r| Call.new(r) } end |
#method_name ⇒ Object
81 82 83 |
# File 'lib/minitest_to_rspec/model/call.rb', line 81 def method_name @exp[2] end |
#num_arguments ⇒ Object
85 86 87 |
# File 'lib/minitest_to_rspec/model/call.rb', line 85 def num_arguments arguments.length end |
#one_string_argument? ⇒ Boolean
89 90 91 |
# File 'lib/minitest_to_rspec/model/call.rb', line 89 def one_string_argument? arguments.length == 1 && string?(arguments[0]) end |
#question_mark_method? ⇒ Boolean
164 165 166 |
# File 'lib/minitest_to_rspec/model/call.rb', line 164 def question_mark_method? method_name.to_s.end_with?("?") end |
#raise_error_args? ⇒ Boolean
Returns true if arguments can be processed into RSpec’s ‘raise_error` matcher. When the last argument is a string, it represents the assertion failure message, which will be discarded later.
96 97 98 99 |
# File 'lib/minitest_to_rspec/model/call.rb', line 96 def raise_error_args? arg_types = arguments.map(&:sexp_type) [[], [:str], [:const], [:const, :str]].include?(arg_types) end |
#receiver ⇒ Object
101 102 103 |
# File 'lib/minitest_to_rspec/model/call.rb', line 101 def receiver @exp[1] end |
#receiver_call ⇒ Object
While ‘#receiver` returns a `Sexp`, `#receiver_call` returns a `Model::Call`.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/minitest_to_rspec/model/call.rb', line 107 def receiver_call if sexp_type?(:call, receiver) rvc = Model::Call.new(receiver) # TODO: Seems like a factory pattern if rvc.method_name == :returns Model::Calls::Returns.new(receiver) else rvc end else raise TypeError end end |
#receiver_chain ⇒ Object
Consider the following chain of method calls:
@a.b.c
whose S-expression is
s(:call, s(:call, s(:call, nil, :a), :b), :c)
the “receiver chain” is
[
s(:call, s(:call, nil, :a), :b),
s(:call, nil, :a),
nil
]
The order of the returned array matches the order in which messages are received, i.e. the order of execution.
Note that the final receiver ‘nil` is included. This `nil` represents the implicit receiver, e.g. `self` or `main`.
144 145 146 147 148 149 150 151 152 |
# File 'lib/minitest_to_rspec/model/call.rb', line 144 def receiver_chain receivers = [] ptr = @exp while sexp_type?(:call, ptr) receivers << ptr[1] ptr = ptr[1] end receivers end |
#receiver_chain_include?(method_name) ⇒ Boolean
154 155 156 |
# File 'lib/minitest_to_rspec/model/call.rb', line 154 def receiver_chain_include?(method_name) receiver_chain.compact.any? { |r| Call.method_name?(r, method_name) } end |
#require_test_helper? ⇒ Boolean
158 159 160 161 162 |
# File 'lib/minitest_to_rspec/model/call.rb', line 158 def require_test_helper? method_name == :require && one_string_argument? && arguments[0][1] == "test_helper" end |