Class: Pbt::Check::RSpecAdapter::PredicateBlockInspector

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/pbt/check/rspec_adapter/predicate_block_inspector.rb

Overview

This class is used to get user-defined block code. If a user defines code like below:

Pbt.property(Pbt.integer, Pbt.integer) do |x, y|
  x > 0 && y > 0
end

inspector.method_params #=> "x, y"
inspector.method_body   #=> "x > 0 && y > 0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ PredicateBlockInspector

Returns a new instance of PredicateBlockInspector.



30
31
32
33
34
# File 'lib/pbt/check/rspec_adapter/predicate_block_inspector.rb', line 30

def initialize(line)
  @line = line
  @method_body = nil
  super()
end

Instance Attribute Details

#method_bodyObject (readonly)

This class is used to get user-defined block code. If a user defines code like below:

Pbt.property(Pbt.integer, Pbt.integer) do |x, y|
  x > 0 && y > 0
end

inspector.method_params #=> "x, y"
inspector.method_body   #=> "x > 0 && y > 0"


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pbt/check/rspec_adapter/predicate_block_inspector.rb', line 27

class PredicateBlockInspector < Prism::Visitor
  attr_reader :method_body, :method_params

  def initialize(line)
    @line = line
    @method_body = nil
    super()
  end

  def visit_call_node(node)
    if node.name == :property && node.block.opening_loc.start_line == @line
      @method_params = node.block.parameters.parameters.slice
      @method_body = node.block.body.slice
    end

    super
  end
end

#method_paramsObject (readonly)

This class is used to get user-defined block code. If a user defines code like below:

Pbt.property(Pbt.integer, Pbt.integer) do |x, y|
  x > 0 && y > 0
end

inspector.method_params #=> "x, y"
inspector.method_body   #=> "x > 0 && y > 0"


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pbt/check/rspec_adapter/predicate_block_inspector.rb', line 27

class PredicateBlockInspector < Prism::Visitor
  attr_reader :method_body, :method_params

  def initialize(line)
    @line = line
    @method_body = nil
    super()
  end

  def visit_call_node(node)
    if node.name == :property && node.block.opening_loc.start_line == @line
      @method_params = node.block.parameters.parameters.slice
      @method_body = node.block.body.slice
    end

    super
  end
end

Instance Method Details

#visit_call_node(node) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/pbt/check/rspec_adapter/predicate_block_inspector.rb', line 36

def visit_call_node(node)
  if node.name == :property && node.block.opening_loc.start_line == @line
    @method_params = node.block.parameters.parameters.slice
    @method_body = node.block.body.slice
  end

  super
end