Class: RSpock::AST::Parser::Block

Inherits:
Object
  • Object
show all
Includes:
ASTTransform::TransformationHelper
Defined in:
lib/rspock/ast/parser/block.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, node) ⇒ Block

Constructs a new Block.

Parameters:

  • type (Symbol)

    The Block type.

  • node (Parser::AST::Node)

    The node associated to this Block.



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

def initialize(type, node)
  @type = type
  @node = node
  @children = []
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



23
24
25
# File 'lib/rspock/ast/parser/block.rb', line 23

def node
  @node
end

#typeObject (readonly)

Returns the value of attribute type.



23
24
25
# File 'lib/rspock/ast/parser/block.rb', line 23

def type
  @type
end

Instance Method Details

#<<(child_node) ⇒ Object

Adds the given child_node to this Block.

Parameters:

  • child_node (Parser::AST::Node)

    The node to be added.



28
29
30
# File 'lib/rspock/ast/parser/block.rb', line 28

def <<(child_node)
  @children << child_node
end

#can_end?Boolean

Whether this block can be the last block in a test method.

Returns:

  • (Boolean)


45
46
47
# File 'lib/rspock/ast/parser/block.rb', line 45

def can_end?
  false
end

#can_start?Boolean

Whether this block can be the first block in a test method.

Returns:

  • (Boolean)


40
41
42
# File 'lib/rspock/ast/parser/block.rb', line 40

def can_start?
  false
end

#childrenArray<Parser::AST::Node>

Retrieves the duped array of children AST nodes for this Block.

Returns:

  • (Array<Parser::AST::Node>)

    The children nodes.



59
60
61
# File 'lib/rspock/ast/parser/block.rb', line 59

def children
  @children.dup
end

#rangeParser::Source::Range

Retrieves the Parser::Source::Range for this Block.

Returns:

  • (Parser::Source::Range)

    The range.



35
36
37
# File 'lib/rspock/ast/parser/block.rb', line 35

def range
  node&.loc&.expression || "?"
end

#succession_error_msgString

Retrieves the error message for succession errors.

Returns:

  • (String)

    The error message.



83
84
85
# File 'lib/rspock/ast/parser/block.rb', line 83

def succession_error_msg
  "Block #{type} @ #{range} must be followed by one of these Blocks: #{successors}"
end

#successorsArray<Symbol>

Retrieves the valid successors for this Block.

Returns:

  • (Array<Symbol>)

    This Block's successors.



52
53
54
# File 'lib/rspock/ast/parser/block.rb', line 52

def successors
  @successors ||= [].freeze
end

#to_rspock_nodeParser::AST::Node

Converts this Block into an RSpock node.

Returns:

  • (Parser::AST::Node)

    A node with type :rspock_<block_type>.



66
67
68
69
# File 'lib/rspock/ast/parser/block.rb', line 66

def to_rspock_node
  rspock_type = :"rspock_#{type.downcase}"
  s(rspock_type, *@children)
end

#valid_successor?(block) ⇒ Boolean

Checks whether or not the given block is a valid successor for this Block.

Parameters:

  • block (Block)

    The candidate successor.

Returns:

  • (Boolean)

    True if the given block is a valid successor, false otherwise.



76
77
78
# File 'lib/rspock/ast/parser/block.rb', line 76

def valid_successor?(block)
  successors.include?(block.type)
end