class Transpec::Syntax::Have

Constants

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).

Public Instance Methods

accurate_conversion?() click to toggle source
# File lib/transpec/syntax/have.rb, line 113
def accurate_conversion?
  runtime_subject_data
end
active_model_errors_on?() click to toggle source
# 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_accessor() click to toggle source
# 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?() click to toggle source
# File lib/transpec/syntax/have.rb, line 81
def collection_accessor_is_private?
  runtime_subject_data(:collection_accessor_is_private?)
end
conversion_target?() click to toggle source
Calls superclass method Transpec::Syntax#conversion_target?
# 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) click to toggle source
# 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_method() click to toggle source
# File lib/transpec/syntax/have.rb, line 93
def default_query_method
  QUERY_METHOD_PRIORITIES.first
end
dynamic_analysis_target?() click to toggle source
# 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.zero?
end
explicit_subject?() click to toggle source
# File lib/transpec/syntax/have.rb, line 44
def explicit_subject?
  expectation.respond_to?(:subject_node)
end
items_method_has_arguments?() click to toggle source
# File lib/transpec/syntax/have.rb, line 55
def items_method_has_arguments?
  items_node.children.size > 2
end
items_name() click to toggle source
# File lib/transpec/syntax/have.rb, line 59
def items_name
  items_node.children[1]
end
matcher_range() click to toggle source
# File lib/transpec/syntax/have.rb, line 117
def matcher_range
  expression_range.join(items_node.loc.expression)
end
query_method() click to toggle source
# 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) click to toggle source
# 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_node() click to toggle source
# File lib/transpec/syntax/have.rb, line 51
def size_node
  have_node.children[2]
end
size_source() click to toggle source

github.com/rspec/rspec-expectations/blob/v2.14.5/lib/rspec/matchers/built_in/have.rb#L8-L12

# 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?() click to toggle source
# 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

Private Instance Methods

add_record() click to toggle source
Calls superclass method Transpec::Syntax#add_record
# File lib/transpec/syntax/have.rb, line 142
def add_record
  super(Have::RecordBuilder.build(self))
end
runtime_subject_data(key = nil) click to toggle source
# File lib/transpec/syntax/have.rb, line 127
def runtime_subject_data(key = nil)
  unless instance_variable_defined?(:@runtime_subject_data)
    node = explicit_subject? ? expectation.subject_node : expectation.node
    @runtime_subject_data = runtime_data[node]
  end

  return @runtime_subject_data unless key

  if @runtime_subject_data
    @runtime_subject_data[key]
  else
    nil
  end
end
source_builder() click to toggle source
# File lib/transpec/syntax/have.rb, line 123
def source_builder
  @source_builder ||= SourceBuilder.new(self, size_source)
end