Class: Transpec::Syntax::Have
Defined Under Namespace
Classes: HaveRecord
Constant Summary
collapse
- QUERY_METHOD_PRIORITIES =
String#count is not query method, and there’s no way to determine whether a method is query method. Method#arity and Method#parameters return same results for Array#count (0+ args) and String#count (1+ args).
So I make #size a priority over #count so that #count won’t be chosen for String (String responds to #size).
[:size, :count, :length].freeze
Instance Attribute Summary collapse
#node, #report, #runtime_data, #source_rewriter
Class Method Summary
collapse
Instance Method Summary
collapse
#arg_node, #arg_nodes, #arg_range, included, #method_name, #parentheses_range, #range_after_arg, #range_in_between_receiver_and_selector, #range_in_between_selector_and_arg, #receiver_node, #receiver_range, #selector_range
all_syntaxes, #expression_range, inherited, #parent_node, register_request_for_dynamic_analysis, snake_case_name, standalone_syntaxes, #static_context_inspector, target_node?
Constructor Details
#initialize(node, expectation, source_rewriter = nil, runtime_data = nil, report = nil) ⇒ Have
Returns a new instance of Have.
33
34
35
36
37
38
39
|
# File 'lib/transpec/syntax/have.rb', line 33
def initialize(node, expectation, source_rewriter = nil, runtime_data = nil, report = nil)
@node = node
@expectation = expectation
@source_rewriter = source_rewriter
@runtime_data = runtime_data
@report = report || Report.new
end
|
Instance Attribute Details
#expectation ⇒ Object
Returns the value of attribute expectation.
20
21
22
|
# File 'lib/transpec/syntax/have.rb', line 20
def expectation
@expectation
end
|
Class Method Details
.standalone? ⇒ Boolean
22
23
24
|
# File 'lib/transpec/syntax/have.rb', line 22
def self.standalone?
false
end
|
.target_method?(have_node, items_method_name) ⇒ Boolean
26
27
28
29
30
31
|
# File 'lib/transpec/syntax/have.rb', line 26
def self.target_method?(have_node, items_method_name)
return false unless have_node
have_receiver_node, have_method_name, *_ = *have_node
return false if have_receiver_node
[:have, :have_exactly, :have_at_least, :have_at_most].include?(have_method_name)
end
|
Instance Method Details
#collection_accessor_is_private? ⇒ Boolean
94
95
96
97
|
# File 'lib/transpec/syntax/have.rb', line 94
def collection_accessor_is_private?
node_data = runtime_node_data(@expectation.subject_node)
node_data && node_data[:collection_accessor_is_private?].result
end
|
#convert_to_standard_expectation!(parenthesize_matcher_arg = true) ⇒ Object
65
66
67
68
69
|
# File 'lib/transpec/syntax/have.rb', line 65
def convert_to_standard_expectation!(parenthesize_matcher_arg = true)
replace(@expectation.subject_range, replacement_subject_source)
replace(expression_range, replacement_matcher_source(size_source, parenthesize_matcher_arg))
register_record
end
|
#default_query_method ⇒ Object
108
109
110
|
# File 'lib/transpec/syntax/have.rb', line 108
def default_query_method
QUERY_METHOD_PRIORITIES.first
end
|
#have_method_name ⇒ Object
81
82
83
|
# File 'lib/transpec/syntax/have.rb', line 81
def have_method_name
have_node.children[1]
end
|
#have_node ⇒ Object
71
72
73
|
# File 'lib/transpec/syntax/have.rb', line 71
def have_node
node.children.first
end
|
#items_name ⇒ Object
85
86
87
|
# File 'lib/transpec/syntax/have.rb', line 85
def items_name
items_node.children[1]
end
|
#query_method ⇒ Object
99
100
101
102
103
104
105
106
|
# File 'lib/transpec/syntax/have.rb', line 99
def query_method
node_data = runtime_node_data(@expectation.subject_node)
if node_data
(QUERY_METHOD_PRIORITIES & node_data[:available_query_methods].result).first
else
default_query_method
end
end
|
#register_request_for_dynamic_analysis(rewriter) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/transpec/syntax/have.rb', line 41
def register_request_for_dynamic_analysis(rewriter)
node = @expectation.subject_node
key = :subject_is_owner_of_collection?
code = "respond_to?(#{items_name.inspect}) || " +
"(methods & #{QUERY_METHOD_PRIORITIES.inspect}).empty?"
rewriter.register_request(node, key, code)
key = :available_query_methods
code = "target = #{code} ? #{items_name} : self; " +
"target.methods & #{QUERY_METHOD_PRIORITIES.inspect}"
rewriter.register_request(node, key, code)
key = :collection_accessor_is_private?
code = "private_methods.include?(#{items_name.inspect})"
rewriter.register_request(node, key, code)
end
|
#replacement_matcher_source(size_source, parenthesize_arg = true) ⇒ Object
112
113
114
115
116
117
118
119
|
# File 'lib/transpec/syntax/have.rb', line 112
def replacement_matcher_source(size_source, parenthesize_arg = true)
case @expectation.current_syntax_type
when :should
replacement_matcher_source_for_should(size_source)
when :expect
replacement_matcher_source_for_expect(size_source, parenthesize_arg)
end
end
|
#size_node ⇒ Object
75
76
77
|
# File 'lib/transpec/syntax/have.rb', line 75
def size_node
have_node.children[2]
end
|
#subject_is_owner_of_collection? ⇒ Boolean
89
90
91
92
|
# File 'lib/transpec/syntax/have.rb', line 89
def subject_is_owner_of_collection?
node_data = runtime_node_data(@expectation.subject_node)
node_data && node_data[:subject_is_owner_of_collection?].result
end
|