Class: RSpock::AST::Transformation

Inherits:
ASTTransform::AbstractTransformation
  • Object
show all
Defined in:
lib/rspock/ast/transformation.rb

Constant Summary collapse

DefaultSourceMap =
{
  Given: RSpock::AST::GivenBlock,
  When: RSpock::AST::WhenBlock,
  Then: RSpock::AST::ThenBlock,
  Expect: RSpock::AST::ExpectBlock,
  Cleanup: RSpock::AST::CleanupBlock,
  Where: RSpock::AST::WhereBlock,
}.freeze
EXTEND_RSPOCK_DECLARATIVE =
s(:send, nil, :extend,
                                     s(:const,
s(:const, nil, :RSpock), :Declarative))

Instance Method Summary collapse

Constructor Details

#initialize(start_block_class: StartBlock, end_block_class: EndBlock, source_map: DefaultSourceMap, strict: true) ⇒ Transformation

Returns a new instance of Transformation.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rspock/ast/transformation.rb', line 25

def initialize(
  start_block_class: StartBlock,
  end_block_class: EndBlock,
  source_map: DefaultSourceMap,
  strict: true
)
  super()
  @start_block_class = start_block_class
  @source_map = source_map
  @end_block_class = end_block_class
  @strict = strict
end

Instance Method Details

#on_block(node) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rspock/ast/transformation.rb', line 89

def on_block(node)
  if node.children[0]&.children[1] != :test
    return node.updated(nil, process_all(node))
  end

  TestMethodTransformation.new(
    @source_map,
    @start_block_class,
    @end_block_class,
    strict: @strict
  ).run(node)
end

#on_casgn(node) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/rspock/ast/transformation.rb', line 56

def on_casgn(node)
  if node.children[2]&.type == :block
    children = node.children.dup
    children[2] = process_casgn_block(children[2])

    node.updated(nil, children)
  else
    super
  end
end

#on_class(node) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rspock/ast/transformation.rb', line 42

def on_class(node)
  if node.children[2]&.type == :begin
    children = node.children.dup
    children[2] = process_rspock(children[2])

    node.updated(nil, children)
  else
    children = node.children.dup
    children[2] = process_rspock(s(:begin, node.children[2]))

    node.updated(nil, children)
  end
end

#process_casgn_block(node) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rspock/ast/transformation.rb', line 67

def process_casgn_block(node)
  if node.children[2]&.type == :begin
    children = node.children.dup
    children[2] = process_rspock(children[2])

    # Optimization to remove empty :begin node
    children.slice!(2) if children[2].children.empty?

    node.updated(nil, children)
  else
    children = node.children.dup
    children[2] = process_rspock(s(:begin, node.children[2]))

    node.updated(nil, children)
  end
end

#process_rspock(node) ⇒ Object



84
85
86
87
# File 'lib/rspock/ast/transformation.rb', line 84

def process_rspock(node)
  children = [source_map_rescue_wrapper(s(:begin, *[EXTEND_RSPOCK_DECLARATIVE, *process_all(node)]))]
  node.updated(nil, children)
end

#source_map_rescue_wrapper(node) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rspock/ast/transformation.rb', line 102

def source_map_rescue_wrapper(node)
  s(:kwbegin,
    s(:rescue,
      node,
      s(:resbody,
        s(:array,
          s(:const, nil, :StandardError)
        ),
        s(:lvasgn, :e),
        s(:begin,
          s(:send,
            s(:send,
              s(:const,
                s(:const,
                  s(:cbase), :RSpock), :BacktraceFilter), :new), :filter_exception,
            s(:lvar, :e)
          ),
          s(:send, nil, :raise)
        )
      ),
      nil
    )
  )
end