Class: Transpec::Syntax::Should

Inherits:
Transpec::Syntax show all
Includes:
Mixin::Expectizable, Mixin::HaveMatcher, Mixin::MonkeyPatch, Mixin::Send, Util
Defined in:
lib/transpec/syntax/should.rb

Instance Attribute Summary collapse

Attributes inherited from Transpec::Syntax

#ancestor_nodes, #node, #report, #runtime_data, #source_rewriter

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Send

#arg_node, #arg_nodes, #arg_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::MonkeyPatch

#check_syntax_availability, #register_request_of_syntax_availability_inspection, #subject_node, #subject_range

Methods included from Mixin::Expectizable

#wrap_subject_in_expect!

Methods included from Mixin::HaveMatcher

#have_matcher

Methods included from Util

const_name, contain_here_document?, here_document?, in_parentheses?, indentation_of_line, proc_literal?

Methods inherited from Transpec::Syntax

all_syntaxes, #expression_range, inherited, #parent_node, register_request_for_dynamic_analysis, snake_case_name, standalone?, standalone_syntaxes, #static_context_inspector, target_node?

Constructor Details

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

Returns a new instance of Should.



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

def initialize(node, ancestor_nodes, 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/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/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

#expect_available?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/transpec/syntax/should.rb', line 38

def expect_available?
  check_syntax_availability(__method__)
end

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/transpec/syntax/should.rb', line 46

def expectize!(negative_form = 'not_to', parenthesize_matcher_arg = true)
  unless expect_available?
    fail InvalidContextError.new(selector_range, "##{method_name}", '#expect')
  end

  if proc_literal?(subject_node)
    replace_proc_selector_with_expect!
  else
    wrap_subject_in_expect!
  end

  replace(should_range, positive? ? 'to' : negative_form)

  @current_syntax_type = :expect
  register_record(negative_form)

  operator_matcher.correct_operator!(parenthesize_matcher_arg) if operator_matcher
end

#matcher_nodeObject



77
78
79
# File 'lib/transpec/syntax/should.rb', line 77

def matcher_node
  arg_node || parent_node
end

#operator_matcherObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/transpec/syntax/should.rb', line 65

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

  @operator_matcher ||= begin
    if OperatorMatcher.target_node?(matcher_node, @runtime_data)
      OperatorMatcher.new(matcher_node, @source_rewriter, @runtime_data, @report)
    else
      nil
    end
  end
end

#positive?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/transpec/syntax/should.rb', line 42

def positive?
  method_name == :should
end

#register_request_for_dynamic_analysis(rewriter) ⇒ Object



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

def register_request_for_dynamic_analysis(rewriter)
  register_request_of_syntax_availability_inspection(
    rewriter,
    :expect_available?,
    [:expect]
  )

  operator_matcher.register_request_for_dynamic_analysis(rewriter) if operator_matcher
  have_matcher.register_request_for_dynamic_analysis(rewriter) if have_matcher
end