Sha256: 852fba665689832f434457849668ddc222f61404797a1f97e2ba57d25973f9d7

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2018-2022, by Samuel Williams.

require_relative 'wrapper'

require 'coverage'

module Covered
	class Capture < Wrapper
		def initialize(output)
			super(output)
			
			begin
				@trace = TracePoint.new(:line, :call, :c_call) do |trace|
					if trace.event == :call
						# Ruby doesn't always mark call-sites in sub-expressions, so we use this approach to compute a call site and mark it:
						if location = caller_locations(2, 1).first and path = location.path
							@output.mark(path, location.lineno, 1)
						end
					end
					
					if path = trace.path
						@output.mark(path, trace.lineno, 1)
					end
				end
			rescue
				warn "Line coverage disabled: #{$!}"
				@trace = nil
			end
		end
		
		def enable
			super
			
			@trace&.enable
		end
		
		def disable
			@trace&.disable
			
			super
		end
		
		def execute(path, source, binding: TOPLEVEL_BINDING)
			enable
			
			eval(source, binding, path)
		ensure
			disable
		end
	end
	
	# class Capture < Wrapper
	# 	def enable
	# 		super
	# 
	# 		::Coverage.start
	# 	end
	# 
	# 	def disable
	# 		result = ::Coverage.result
	# 
	# 		puts result.inspect
	# 
	# 		result.each do |path, lines|
	# 			lines.each_with_index do |lineno, count|
	# 				@output.mark(path, lineno, count)
	# 			end
	# 		end
	# 
	# 		super
	# 	end
	# end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
covered-0.16.9 lib/covered/capture.rb
covered-0.16.8 lib/covered/capture.rb