Class: Transpec::Syntax::Have

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

Defined Under Namespace

Modules: DynamicAnalysis Classes: RecordBuilder, 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

Attributes inherited from Transpec::Syntax

#node, #project, #report, #runtime_data, #source_rewriter

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 included from DynamicAnalysis

#register_dynamic_analysis_request

Methods inherited from Transpec::Syntax

#dependent_syntaxes, #expression_range, #initialize, #inspect, #parent_node, #rspec_version, snake_case_name, standalone?, #static_context_inspector

Methods included from Collection

#all_syntaxes, #inherited, #mixins, #require_all, #standalone_syntaxes

Instance Method Details

#accurate_conversion?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/transpec/syntax/have.rb', line 113

def accurate_conversion?
  runtime_subject_data
end

#active_model_errors_on?Boolean

Returns:

  • (Boolean)


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

def active_model_errors_on?
  return false unless runtime_subject_data(:subject_includes_active_model_validations?)
  [:errors_on, :error_on].include?(items_name)
end

#collection_accessorObject



63
64
65
66
67
68
69
# File 'lib/transpec/syntax/have.rb', line 63

def collection_accessor
  if runtime_subject_data(:collection_accessor)
    runtime_subject_data(:collection_accessor).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(:collection_accessor_is_private?)
end

#conversion_target?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/transpec/syntax/have.rb', line 32

def conversion_target?
  return false unless super
  return false if runtime_subject_data(:project_requires_collection_matcher?)
  !active_model_errors_on?
end

#convert_to_standard_expectation!(parenthesize_matcher_arg = true) ⇒ Object



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

def convert_to_standard_expectation!(parenthesize_matcher_arg = true)
  replace(expectation.subject_range, replacement_subject_source) if explicit_subject?
  replace(matcher_range, source_builder.replacement_matcher_source(parenthesize_matcher_arg))
  add_record if explicit_subject?
end

#default_query_methodObject



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

def default_query_method
  QUERY_METHOD_PRIORITIES.first
end

#dynamic_analysis_target?Boolean

Returns:

  • (Boolean)


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

def dynamic_analysis_target?
  super &&
    receiver_node.nil? &&
    [:have, :have_exactly, :have_at_least, :have_at_most].include?(method_name) &&
    node.sibling_index == 0
end

#explicit_subject?Boolean

Returns:

  • (Boolean)


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

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

#items_method_has_arguments?Boolean

Returns:

  • (Boolean)


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

def items_method_has_arguments?
  items_node.children.size > 2
end

#items_nameObject



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

def items_name
  items_node.children[1]
end

#matcher_rangeObject



117
118
119
# File 'lib/transpec/syntax/have.rb', line 117

def matcher_range
  expression_range.join(items_node.loc.expression)
end

#query_methodObject



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

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

#replacement_subject_source(base_subject = nil) ⇒ Object



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

def replacement_subject_source(base_subject = nil)
  base_subject ||= expectation.subject_node
  source_builder.replacement_subject_source(base_subject)
end

#size_nodeObject



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

def size_node
  have_node.children[2]
end

#size_sourceObject



103
104
105
106
107
108
109
110
111
# File 'lib/transpec/syntax/have.rb', line 103

def size_source
  if size_node.sym_type? && size_node.children.first == :no
    0
  elsif size_node.str_type?
    size_node.children.first.to_i
  else
    size_node.loc.expression.source
  end
end

#subject_is_owner_of_collection?Boolean

Returns:

  • (Boolean)


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

def subject_is_owner_of_collection?
  return true if items_method_has_arguments?
  runtime_subject_data(:collection_accessor)
end