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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ShouldBase

#matcher_node, #operator_matcher, #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::HaveMatcherOwner

#have_matcher

Methods included from Util

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

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.



20
21
22
23
# File 'lib/transpec/syntax/oneliner_should.rb', line 20

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.



14
15
16
# File 'lib/transpec/syntax/oneliner_should.rb', line 14

def current_syntax_type
  @current_syntax_type
end

Class Method Details

.target_method?(receiver_node, method_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

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

rubocop:disable LineLength



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

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



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

def convert_have_items_to_standard_should!
  return if have_matcher.project_requires_collection_matcher?

  insert_example_description!

  subject_source = have_matcher.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)


67
68
69
70
# File 'lib/transpec/syntax/oneliner_should.rb', line 67

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



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

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