Class: RSpock::AST::Transformation

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(block_registry: DEFAULT_BLOCK_REGISTRY, strict: true) ⇒ Transformation

Returns a new instance of Transformation.



23
24
25
26
27
28
29
30
# File 'lib/rspock/ast/transformation.rb', line 23

def initialize(
  block_registry: DEFAULT_BLOCK_REGISTRY,
  strict: true
)
  super()
  @block_registry = block_registry
  @strict = strict
end

Instance Method Details

#on_block(node) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/rspock/ast/transformation.rb', line 83

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

  TestMethodTransformation.new(
    @block_registry,
    strict: @strict
  ).run(node)
end

#on_casgn(node) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/rspock/ast/transformation.rb', line 50

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



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rspock/ast/transformation.rb', line 36

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rspock/ast/transformation.rb', line 61

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



78
79
80
81
# File 'lib/rspock/ast/transformation.rb', line 78

def process_rspock(node)
  processed = process_all(node).compact
  node.updated(nil, [EXTEND_RSPOCK_DECLARATIVE, *processed])
end