Class: MinitestToRspec::Input::Model::Call
- Defined in:
- lib/minitest_to_rspec/input/model/call.rb
Overview
Data object. Represents a ‘:call` s-expression.
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
- .refute_raise?(exp) ⇒ Boolean
- .refute_raises?(exp) ⇒ 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
- #find_call_in_receiver_chain(method_names) ⇒ 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_chain ⇒ Object
Consider the following chain of method calls:.
- #receiver_chain_include?(method_name) ⇒ Boolean
- #refute_raise? ⇒ Boolean
- #refute_raises? ⇒ 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.
12 13 14 15 16 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 12 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.
10 11 12 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 10 def original @original end |
Class Method Details
.assert_difference?(exp) ⇒ Boolean
19 20 21 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 19 def assert_difference?(exp) exp.sexp_type == :call && new(exp).assert_difference? end |
.assert_no_difference?(exp) ⇒ Boolean
23 24 25 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 23 def assert_no_difference?(exp) exp.sexp_type == :call && new(exp).assert_no_difference? end |
.assert_nothing_raised?(exp) ⇒ Boolean
27 28 29 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 27 def assert_nothing_raised?(exp) exp.sexp_type == :call && new(exp).assert_nothing_raised? end |
.assert_raise?(exp) ⇒ Boolean
31 32 33 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 31 def assert_raise?(exp) exp.sexp_type == :call && new(exp).assert_raise? end |
.assert_raises?(exp) ⇒ Boolean
35 36 37 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 35 def assert_raises?(exp) exp.sexp_type == :call && new(exp).assert_raises? end |
.method_name?(exp, name) ⇒ Boolean
47 48 49 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 47 def method_name?(exp, name) exp.sexp_type == :call && new(exp).method_name.to_s == name.to_s end |
.refute_raise?(exp) ⇒ Boolean
39 40 41 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 39 def refute_raise?(exp) exp.sexp_type == :call && new(exp).refute_raise? end |
.refute_raises?(exp) ⇒ Boolean
43 44 45 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 43 def refute_raises?(exp) exp.sexp_type == :call && new(exp).refute_raises? end |
Instance Method Details
#argument_types ⇒ Object
56 57 58 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 56 def argument_types arguments.map(&:sexp_type) end |
#arguments ⇒ Object
52 53 54 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 52 def arguments @exp[3..-1] || [] end |
#assert_difference? ⇒ Boolean
60 61 62 63 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 60 def assert_difference? return false unless method_name == :assert_difference [[:str], %i[str lit]].include?(argument_types) end |
#assert_no_difference? ⇒ Boolean
65 66 67 68 69 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 65 def assert_no_difference? method_name == :assert_no_difference && arguments.length == 1 && arguments[0].sexp_type == :str end |
#assert_nothing_raised? ⇒ Boolean
71 72 73 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 71 def assert_nothing_raised? method_name == :assert_nothing_raised && arguments.empty? end |
#assert_raise? ⇒ Boolean
75 76 77 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 75 def assert_raise? method_name == :assert_raise && raise_error_args? end |
#assert_raises? ⇒ Boolean
79 80 81 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 79 def assert_raises? method_name == :assert_raises && raise_error_args? end |
#calls_in_receiver_chain ⇒ Object
91 92 93 94 95 96 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 91 def calls_in_receiver_chain receiver_chain.each_with_object([]) do |e, a| next unless sexp_type?(:call, e) a << self.class.new(e) end end |
#find_call_in_receiver_chain(method_names) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 98 def find_call_in_receiver_chain(method_names) name_array = [method_names].flatten calls_in_receiver_chain.find { |i| name_array.include?(i.method_name) } end |
#method_name ⇒ Object
105 106 107 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 105 def method_name @exp[2] end |
#num_arguments ⇒ Object
109 110 111 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 109 def num_arguments arguments.length end |
#one_string_argument? ⇒ Boolean
113 114 115 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 113 def one_string_argument? arguments.length == 1 && string?(arguments[0]) end |
#question_mark_method? ⇒ Boolean
173 174 175 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 173 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.
120 121 122 123 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 120 def raise_error_args? arg_types = arguments.map(&:sexp_type) [[], [:str], [:const], %i[const str], [:colon2]].include?(arg_types) end |
#receiver ⇒ Object
125 126 127 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 125 def receiver @exp[1] 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`.
151 152 153 154 155 156 157 158 159 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 151 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
161 162 163 164 165 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 161 def receiver_chain_include?(method_name) receiver_chain.compact.any? { |r| self.class.method_name?(r, method_name) } end |
#refute_raise? ⇒ Boolean
83 84 85 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 83 def refute_raise? method_name == :refute_raise && raise_error_args? end |
#refute_raises? ⇒ Boolean
87 88 89 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 87 def refute_raises? method_name == :refute_raises && raise_error_args? end |
#require_test_helper? ⇒ Boolean
167 168 169 170 171 |
# File 'lib/minitest_to_rspec/input/model/call.rb', line 167 def require_test_helper? method_name == :require && one_string_argument? && arguments[0][1] == 'test_helper' end |