Class: Transpec::Syntax::OnelinerShould

Inherits:
Transpec::Syntax show all
Includes:
RSpecDSL, Mixin::HaveMatcherOwner, Mixin::Send, 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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ShouldBase

included, #matcher_node, #operator_matcher, #positive?, #should_range

Methods included from Mixin::Send

#arg_node, #arg_nodes, #arg_range, #args_range, included, #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::HaveMatcherOwner

#have_matcher, included

Methods included from Util

const_name, contain_here_document?, expand_range_to_adjacent_whitespaces, find_consecutive_whitespace_position, here_document?, in_explicit_parentheses?, indentation_of_line, literal?, proc_literal?, taking_block?

Methods inherited from Transpec::Syntax

conversion_target_node?, #expression_range, #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

Constructor Details

#initialize(node, source_rewriter = nil, runtime_data = nil, report = nil) ⇒ OnelinerShould

Returns a new instance of OnelinerShould.



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

def initialize(node, source_rewriter = nil, runtime_data = nil, report = nil)
  super
  @current_syntax_type = :should
end

Instance Attribute Details

#current_syntax_typeObject (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

Class Method Details

.target_method?(receiver_node, method_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#build_description(size) ⇒ Object



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

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

  case have_matcher.have_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

#convert_have_items_to_standard_expect!(negative_form = 'not_to', parenthesize_matcher_arg = true) ⇒ Object

rubocop:disable LineLength



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

def convert_have_items_to_standard_expect!(negative_form = 'not_to', parenthesize_matcher_arg = true)
  # rubocop:enable LineLength
  return if have_matcher.project_requires_collection_matcher?

  insert_example_description!

  subject_source = have_matcher.build_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
  have_matcher.convert_to_standard_expectation!

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

#convert_have_items_to_standard_should!Object



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

def convert_have_items_to_standard_should!
  return if have_matcher.project_requires_collection_matcher?

  insert_example_description!

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

  have_matcher.convert_to_standard_expectation!

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

#example_has_description?Boolean

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/transpec/syntax/oneliner_should.rb', line 69

def example_has_description?
  send_node = example_block_node.children.first
  !send_node.children[2].nil?
end

#expectize!(negative_form = 'not_to', parenthesize_matcher_arg = true) ⇒ Object



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

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

  @current_syntax_type = :expect
  operator_matcher.convert_operator!(parenthesize_matcher_arg) if operator_matcher

  register_record(negative_form)
end