123456789_123456789_123456789_123456789_123456789_

Class: PowerAssert::Parser

Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: lib/power_assert/parser.rb

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(line, path, lineno, binding, assertion_method_name = nil, assertion_proc = nil) ⇒ Parser

[ GitHub ]

  
# File 'lib/power_assert/parser.rb', line 10

def initialize(line, path, lineno, binding, assertion_method_name = nil, assertion_proc = nil)
  @line = line
  @line_for_parsing = (valid_syntax?(line) ? line : slice_expression(line)).b
  @path = path
  @lineno = lineno
  @binding = binding
  @proc_local_variables = binding.eval('local_variables').map(&:to_s)
  @assertion_method_name = assertion_method_name
  @assertion_proc = assertion_proc
end

Instance Attribute Details

#binding (readonly)

[ GitHub ]

  
# File 'lib/power_assert/parser.rb', line 8

attr_reader :line, :path, :lineno, :binding

#line (readonly)

[ GitHub ]

  
# File 'lib/power_assert/parser.rb', line 8

attr_reader :line, :path, :lineno, :binding

#lineno (readonly)

[ GitHub ]

  
# File 'lib/power_assert/parser.rb', line 8

attr_reader :line, :path, :lineno, :binding

#path (readonly)

[ GitHub ]

  
# File 'lib/power_assert/parser.rb', line 8

attr_reader :line, :path, :lineno, :binding

Instance Method Details

#call_paths

[ GitHub ]

  
# File 'lib/power_assert/parser.rb', line 25

def call_paths
  collect_paths(idents).uniq
end

#collect_paths(idents, prefixes = [[]], index = 0)

[ GitHub ]

  
# File 'lib/power_assert/parser.rb', line 250

def collect_paths(idents, prefixes = [[]], index = 0)
  if index < idents.length
    node = idents[index]
    if node.kind_of?(Branch)
      prefixes = node.flat_map {|n| collect_paths(n, prefixes, 0) }
    else
      prefixes = prefixes.map {|prefix| prefix + [node] }
    end
    collect_paths(idents, prefixes, index + 1)
  else
    prefixes
  end
end

#idents

[ GitHub ]

  
# File 'lib/power_assert/parser.rb', line 21

def idents
  @idents ||= []
end

#method_id_set

[ GitHub ]

  
# File 'lib/power_assert/parser.rb', line 29

def method_id_set
  methods = idents.flatten.find_all {|i| i.type == :method }
  @method_id_set ||= methods.map(&:name).map(&:to_sym).each_with_object({}) {|i, h| h[i] = true }
end

#slice_expression(str) (private)

[ GitHub ]

  
# File 'lib/power_assert/parser.rb', line 46

def slice_expression(str)
  str = str.chomp
  str.sub!(/\A\s*(?:if|unless|elsif|case|while|until) /) {|i| ' ' * i.length }
  str.sub!(/\A\s*(?:\}|\]|end)?\./) {|i| ' ' * i.length }
  str.sub!(/[\{\.\\]\z/, '')
  str.sub!(/(?:&&|\|\|)\z/, '')
  str.sub!(/ (?:do|and|or)\z/, '')
  str
end

#valid_syntax?(str) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/power_assert/parser.rb', line 36

def valid_syntax?(str)
  verbose, $VERBOSE = $VERBOSE, nil
  RubyVM::InstructionSequence.compile(str)
  true
rescue SyntaxError
  false
ensure
  $VERBOSE = verbose
end