Class: Transpec::Syntax::Have

Inherits:
Transpec::Syntax show all
Includes:
Mixin::OwnedMatcher, Mixin::Send
Defined in:
lib/transpec/syntax/have.rb,
lib/transpec/syntax/have/have_record.rb,
lib/transpec/syntax/have/source_builder.rb,
lib/transpec/syntax/have/dynamic_inspector.rb

Defined Under Namespace

Classes: DynamicInspector, HaveRecord, SourceBuilder

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, #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 included from Mixin::OwnedMatcher

#initialize

Methods inherited from Transpec::Syntax

conversion_target_node?, #expression_range, #initialize, #parent_node, snake_case_name, standalone?, #static_context_inspector, target_node?

Methods included from Collection

#all_syntaxes, #inherited, #require_all, #standalone_syntaxes

Methods included from DynamicAnalysis

#register_request_for_dynamic_analysis

Instance Attribute Details

#expectationObject (readonly)

Returns the value of attribute expectation.



25
26
27
# File 'lib/transpec/syntax/have.rb', line 25

def expectation
  @expectation
end

Class Method Details

.target_method?(receiver_node, method_name) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/transpec/syntax/have.rb', line 27

def self.target_method?(receiver_node, method_name)
  receiver_node.nil? &&
    [:have, :have_exactly, :have_at_least, :have_at_most].include?(method_name)
end

Instance Method Details

#collection_accessorObject



68
69
70
71
72
73
74
# File 'lib/transpec/syntax/have.rb', line 68

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)


81
82
83
# File 'lib/transpec/syntax/have.rb', line 81

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



36
37
38
39
40
41
42
43
# File 'lib/transpec/syntax/have.rb', line 36

def convert_to_standard_expectation!(parenthesize_matcher_arg = true)
  return if project_requires_collection_matcher?

  replace(expectation.subject_range, replacement_subject_source) if explicit_subject?
  replace(matcher_range, source_builder.replacement_matcher_source(parenthesize_matcher_arg))

  register_record if explicit_subject?
end

#default_query_methodObject



94
95
96
# File 'lib/transpec/syntax/have.rb', line 94

def default_query_method
  QUERY_METHOD_PRIORITIES.first
end

#explicit_subject?Boolean

Returns:

  • (Boolean)


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

def explicit_subject?
  expectation.respond_to?(:subject_node)
end

#items_method_has_arguments?Boolean

Returns:

  • (Boolean)


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

def items_method_has_arguments?
  items_node.children.size > 2
end

#items_nameObject



60
61
62
# File 'lib/transpec/syntax/have.rb', line 60

def items_name
  items_node.children[1]
end

#project_requires_collection_matcher?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/transpec/syntax/have.rb', line 64

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

#query_methodObject



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

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

#replacement_subject_source(original_subject_source = nil) ⇒ Object



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

def replacement_subject_source(original_subject_source = nil)
  original_subject_source ||= expectation.subject_range.source
  source_builder.replacement_subject_source(original_subject_source)
end

#size_nodeObject



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

def size_node
  have_node.children[2]
end

#size_sourceObject



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

def size_source
  size_node.loc.expression.source
end

#subject_is_owner_of_collection?Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/transpec/syntax/have.rb', line 76

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