class Covered::Source

The source map, loads the source file, parses the AST to generate which lines contain executable code.

Attributes

paths[R]

Public Class Methods

new(output) click to toggle source
Calls superclass method
# File lib/covered/source.rb, line 29
def initialize(output)
        super(output)
        
        @paths = {}
        @mutex = Mutex.new
        
        @annotations = {}
        
        begin
                @trace = TracePoint.new(:script_compiled) do |event|
                        if path = event.instruction_sequence.path and source = event.eval_script
                                @mutex.synchronize do
                                        @paths[path] = source
                                end
                        end
                end
        rescue
                warn "Script coverage disabled: #{$!}"
                @trace = nil
        end
end

Public Instance Methods

disable() click to toggle source
Calls superclass method
# File lib/covered/source.rb, line 57
def disable
        @trace&.disable
        
        super
end
each() { |freeze| ... } click to toggle source
# File lib/covered/source.rb, line 108
def each(&block)
        @output.each do |coverage|
                if top = parse(coverage.path)
                        self.expand(top, coverage)
                end
                
                yield coverage.freeze
        end
end
enable() click to toggle source
Calls superclass method
# File lib/covered/source.rb, line 51
def enable
        super
        
        @trace&.enable
end
executable?(node) click to toggle source
# File lib/covered/source.rb, line 65
def executable?(node)
        node.type == :send
end
expand(node, coverage, level = 0) click to toggle source
# File lib/covered/source.rb, line 73
def expand(node, coverage, level = 0)
        if node.is_a? Parser::AST::Node
                if ignore?(node)
                        coverage.annotate(node.location.line, "ignoring #{node.type}")
                else
                        if executable?(node)
                                # coverage.annotate(node.first_lineno, "executable #{node.type}")
                                coverage.counts[node.location.line] ||= 0
                        else
                                # coverage.annotate(node.first_lineno, "not executable #{node.type}")
                        end
                        
                        expand(node.children, coverage, level + 1)
                end
        elsif node.is_a? Array
                node.each do |child|
                        expand(child, coverage, level)
                end
        else
                return false
        end
end
ignore?(node) click to toggle source
# File lib/covered/source.rb, line 69
def ignore?(node)
        node.nil? or node.type == :arg
end
parse(path) click to toggle source
# File lib/covered/source.rb, line 96
def parse(path)
        if source = @paths[path]
                Parser::CurrentRuby.parse(source)
        elsif File.exist?(path)
                Parser::CurrentRuby.parse_file(path)
        else
                warn "Couldn't parse #{path}, file doesn't exist?"
        end
rescue
        warn "Couldn't parse #{path}: #{$!}"
end