Class: RSpock::AST::Parser::TestMethodParser

Inherits:
Object
  • Object
show all
Includes:
NodeBuilder
Defined in:
lib/rspock/ast/parser/test_method_parser.rb

Overview

Parses a Ruby test method AST node into a self-contained RSpock AST.

Input: s(:block, s(:send, nil, :test, …), s(:args), s(:begin, …)) Output: s(:rspock_test,

s(:rspock_def, method_call_node, args_node),
s(:rspock_body, s(:rspock_given, ...), s(:rspock_when, ...), ...),
s(:rspock_where, ...))   # optional

Instance Method Summary collapse

Methods included from NodeBuilder

#s

Constructor Details

#initialize(block_registry, strict: true) ⇒ TestMethodParser

Returns a new instance of TestMethodParser.



17
18
19
20
# File 'lib/rspock/ast/parser/test_method_parser.rb', line 17

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

Instance Method Details

#parse(node) ⇒ Object

Parses a Ruby test method AST into an RSpock AST (TestNode). Returns nil when non-strict and no RSpock blocks are found.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rspock/ast/parser/test_method_parser.rb', line 24

def parse(node)
  blocks = parse_blocks(node)

  if blocks.empty?
    return nil unless @strict
    raise BlockError, "Test method @ #{node.loc&.expression || '?'} must start with one of: Given, When, Expect"
  end

  validate_blocks(blocks, node)
  build_rspock_ast(node, blocks)
end