Class: Transpec::Syntax::OnelinerShould

Inherits:
Transpec::Syntax show all
Includes:
RSpecDSL, Mixin::ShouldBase, Util
Defined in:
lib/transpec/syntax/oneliner_should.rb

Defined Under Namespace

Classes: ExpectRecordBuilder, HaveRecordBuilder

Constant Summary

Constants included from RSpecDSL

RSpecDSL::EXAMPLE_GROUP_METHODS, RSpecDSL::EXAMPLE_METHODS, RSpecDSL::HELPER_METHODS, RSpecDSL::HOOK_METHODS

Constants included from Util

Util::LITERAL_TYPES, Util::WHITESPACES

Instance Attribute Summary

Attributes inherited from Transpec::Syntax

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

Instance Method Summary collapse

Methods included from Mixin::ShouldBase

#current_syntax_type, #matcher_node, #positive?, #should_range

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::MatcherOwner

#dependent_syntaxes

Methods included from Util

beginning_of_line_range, block_node_taken_by_method, chainable_source, const_name, contain_here_document?, each_backward_chained_node, each_forward_chained_node, each_line_range, expand_range_to_adjacent_whitespaces, find_consecutive_whitespace_position, first_block_arg_name, here_document?, in_explicit_parentheses?, indentation_of_line, line_range, literal?, proc_literal?, range_from_arg

Methods inherited from Transpec::Syntax

#add_record, #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

Methods included from DynamicAnalysis

#register_dynamic_analysis_request

Constructor Details

This class inherits a constructor from Transpec::Syntax

Instance Method Details

#build_description(size) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/transpec/syntax/oneliner_should.rb', line 88

def build_description(size)
  description = positive? ? 'has ' : 'does not have '

  case have_matcher.method_name
  when :have_at_least then description << 'at least '
  when :have_at_most  then description << 'at most '
  end

  items = have_matcher.items_name

  if positive? && size == '0'
    size = 'no'
  elsif size == '1'
    items = ActiveSupport::Inflector.singularize(have_matcher.items_name)
  end

  description << "#{size} #{items}"
end

#conversion_target?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
# File 'lib/transpec/syntax/oneliner_should.rb', line 21

def conversion_target?
  return false unless dynamic_analysis_target?
  return true unless runtime_data.run?(send_analysis_target_node)
  return false unless defined_in_rspec_source?
  # #should inside of #its is dynamically defined in MemoizedHelper,
  # so it cannot be differentiated from user-defined methods by the dynamic analysis in Send.
  # https://github.com/rspec/rspec-core/blob/v2.14.8/lib/rspec/core/memoized_helpers.rb#L439
  !example_method_defined_by_user? || in_its?
end

#convert_have_items_to_standard_expect!(negative_form = 'not_to') ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/transpec/syntax/oneliner_should.rb', line 52

def convert_have_items_to_standard_expect!(negative_form = 'not_to')
  return unless have_matcher.conversion_target?

  insert_example_description!

  subject_source = have_matcher.replacement_subject_source('subject')
  expect_to_source = "expect(#{subject_source})."
  expect_to_source << (positive? ? 'to' : negative_form)
  replace(should_range, expect_to_source)

  @current_syntax_type = :expect

  add_record(HaveRecordBuilder.build(self, have_matcher, negative_form))
end

#convert_have_items_to_standard_should!Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/transpec/syntax/oneliner_should.rb', line 41

def convert_have_items_to_standard_should!
  return unless have_matcher.conversion_target?

  insert_example_description!

  subject_source = have_matcher.replacement_subject_source('subject')
  insert_before(expression_range, "#{subject_source}.")

  add_record(HaveRecordBuilder.build(self, have_matcher))
end

#dynamic_analysis_target?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/transpec/syntax/oneliner_should.rb', line 17

def dynamic_analysis_target?
  super && receiver_node.nil? && [:should, :should_not].include?(method_name)
end

#exampleObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/transpec/syntax/oneliner_should.rb', line 67

def example
  return @example if instance_variable_defined?(:@example)

  @example = nil

  node.each_ancestor(:block) do |block_node|
    send_node = block_node.children[0]

    found = Syntax.all_syntaxes.find do |syntax_class|
      next unless syntax_class.ancestors.include?(Mixin::Examplish)
      syntax = syntax_class.new(send_node, runtime_data, project, source_rewriter)
      next unless syntax.conversion_target?
      @example = syntax
    end

    break if found
  end

  @example
end

#expectize!(negative_form = 'not_to') ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/transpec/syntax/oneliner_should.rb', line 31

def expectize!(negative_form = 'not_to')
  replacement = 'is_expected.'
  replacement << (positive? ? 'to' : negative_form)
  replace(should_range, replacement)

  @current_syntax_type = :expect

  add_record(ExpectRecordBuilder.build(self, negative_form))
end