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

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 collapse

Attributes inherited from Transpec::Syntax

#node, #report, #runtime_data, #source_rewriter

Instance Method Summary collapse

Methods included from Mixin::ShouldBase

#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

#dependent_syntaxes, #expression_range, #inspect, #parent_node, 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

#initialize ⇒ OnelinerShould

Returns a new instance of OnelinerShould.



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

def initialize(*)
  super
  @current_syntax_type = :should
end

Instance Attribute Details

#current_syntax_type ⇒ Object (readonly)

Returns the value of attribute current_syntax_type.



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

def current_syntax_type
  @current_syntax_type
end

Instance Method Details

#build_description(size) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/transpec/syntax/oneliner_should.rb', line 95

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)


27
28
29
30
31
32
33
34
35
# File 'lib/transpec/syntax/oneliner_should.rb', line 27

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/transpec/syntax/oneliner_should.rb', line 58

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

  report.records << OnelinerShouldHaveRecord.new(self, have_matcher, negative_form)
end

#convert_have_items_to_standard_should! ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/transpec/syntax/oneliner_should.rb', line 47

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

  report.records << OnelinerShouldHaveRecord.new(self, have_matcher)
end

#dynamic_analysis_target? ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/transpec/syntax/oneliner_should.rb', line 23

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

#example ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/transpec/syntax/oneliner_should.rb', line 73

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

  @example = nil

  node.each_ancestor_node do |node|
    next unless node.block_type?
    send_node = 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, source_rewriter, runtime_data)
      next unless syntax.conversion_target?
      @example = syntax
    end

    break if found
  end

  @example
end

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



37
38
39
40
41
42
43
44
45
# File 'lib/transpec/syntax/oneliner_should.rb', line 37

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

  @current_syntax_type = :expect

  add_record(negative_form)
end