Class: Transpec::Syntax::Have

Inherits:
Transpec::Syntax show all
Includes:
Mixin::Send
Defined in:
lib/transpec/syntax/have.rb

Defined Under Namespace

Classes: DynamicInspector, 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

Attributes inherited from Transpec::Syntax

#node, #report, #runtime_data, #source_rewriter

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Send

#arg_node, #arg_nodes, #arg_range, #args_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

Methods inherited from Transpec::Syntax

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

#expectationObject (readonly)

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

Returns:

  • (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

Returns:

  • (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_accessorObject



78
79
80
81
82
83
84
# File 'lib/transpec/syntax/have.rb', line 78

def collection_accessor
  if runtime_subject_data && runtime_subject_data[:collection_accessor].result
    runtime_subject_data[:collection_accessor].result.to_sym
  else
    items_name
  end
end

#collection_accessor_is_private?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/transpec/syntax/have.rb', line 91

def collection_accessor_is_private?
  runtime_subject_data && runtime_subject_data[:collection_accessor_is_private?].result
end

#convert_to_standard_expectation!(parenthesize_matcher_arg = true) ⇒ Object



45
46
47
48
49
50
# File 'lib/transpec/syntax/have.rb', line 45

def convert_to_standard_expectation!(parenthesize_matcher_arg = true)
  return if project_requires_collection_matcher?
  replace(@expectation.subject_range, replacement_subject_source)
  replace(expression_range, replacement_matcher_source(size_source, parenthesize_matcher_arg))
  register_record
end

#default_query_methodObject



104
105
106
# File 'lib/transpec/syntax/have.rb', line 104

def default_query_method
  QUERY_METHOD_PRIORITIES.first
end

#have_method_nameObject



66
67
68
# File 'lib/transpec/syntax/have.rb', line 66

def have_method_name
  have_node.children[1]
end

#have_nodeObject



52
53
54
# File 'lib/transpec/syntax/have.rb', line 52

def have_node
  node.children.first
end

#items_method_has_arguments?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/transpec/syntax/have.rb', line 62

def items_method_has_arguments?
  items_node.children.size > 2
end

#items_nameObject



70
71
72
# File 'lib/transpec/syntax/have.rb', line 70

def items_name
  items_node.children[1]
end

#project_requires_collection_matcher?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/transpec/syntax/have.rb', line 74

def project_requires_collection_matcher?
  runtime_subject_data && runtime_subject_data[:project_requires_collection_matcher?].result
end

#query_methodObject



95
96
97
98
99
100
101
102
# File 'lib/transpec/syntax/have.rb', line 95

def query_method
  if runtime_subject_data && runtime_subject_data[:available_query_methods]
    available_query_methods = runtime_subject_data[:available_query_methods].result
    (QUERY_METHOD_PRIORITIES & available_query_methods.map(&:to_sym)).first
  else
    default_query_method
  end
end

#register_request_for_dynamic_analysis(rewriter) ⇒ Object



41
42
43
# File 'lib/transpec/syntax/have.rb', line 41

def register_request_for_dynamic_analysis(rewriter)
  DynamicInspector.register_request(self, rewriter)
end

#replacement_matcher_source(size_source, parenthesize_arg = true) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/transpec/syntax/have.rb', line 108

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_nodeObject



56
57
58
# File 'lib/transpec/syntax/have.rb', line 56

def size_node
  have_node.children[2]
end

#subject_is_owner_of_collection?Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'lib/transpec/syntax/have.rb', line 86

def subject_is_owner_of_collection?
  return true if items_method_has_arguments?
  runtime_subject_data && !runtime_subject_data[:collection_accessor].result.nil?
end