Class: Covered::Summary

Inherits:
Object
  • Object
show all
Defined in:
lib/covered/summary.rb

Direct Known Subclasses

BriefSummary, FullSummary, PartialSummary

Instance Method Summary collapse

Constructor Details

#initialize(threshold: 1.0) ⇒ Summary

Returns a new instance of Summary.



11
12
13
# File 'lib/covered/summary.rb', line 11

def initialize(threshold: 1.0)
	@threshold = threshold
end

Instance Method Details

#call(wrapper, output = $stdout, **options) ⇒ Object

A coverage array gives, for each line, the number of line execution by the interpreter. A nil value means coverage is finishd for this line (lines like else and end).



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/covered/summary.rb', line 111

def call(wrapper, output = $stdout, **options)
	terminal = self.terminal(output)
	
	statistics = self.each(wrapper) do |coverage|
		path = wrapper.relative_path(coverage.path)
		terminal.puts ""
		terminal.puts path, style: :path
		
		begin
			print_coverage(terminal, coverage, **options)
		rescue => error
			print_error(terminal, error)
		end
		
		coverage.print(output)
	end
	
	terminal.puts
	statistics.print(output)
end

#each(wrapper) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/covered/summary.rb', line 36

def each(wrapper)
	statistics = Statistics.new
	
	wrapper.each do |coverage|
		statistics << coverage
		
		if @threshold.nil? or coverage.ratio < @threshold
			yield coverage
		end
	end
	
	return statistics
end


50
51
52
53
54
55
56
57
58
# File 'lib/covered/summary.rb', line 50

def print_annotations(terminal, coverage, line, line_offset)
	if annotations = coverage.annotations[line_offset]
		prefix = "#{line_offset}|".rjust(8) + "*|".rjust(8)
		terminal.write prefix, style: :ignored_prefix
		
		terminal.write line.match(/^\s+/)
		terminal.puts "\# #{annotations.join(", ")}", style: :annotations
	end
end


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/covered/summary.rb', line 86

def print_coverage(terminal, coverage)
	line_offset = 1
	counts = coverage.counts
	
	coverage.read do |file|
		print_line_header(terminal)
		
		file.each_line do |line|
			count = counts[line_offset]
			
			print_annotations(terminal, coverage, line, line_offset)
			
			print_line(terminal, line, line_offset, count)
			
			line_offset += 1
		end
	end
end


105
106
107
108
# File 'lib/covered/summary.rb', line 105

def print_error(terminal, error)
	terminal.puts "Error: #{error.message}", style: :error
	terminal.puts error.backtrace
end


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/covered/summary.rb', line 66

def print_line(terminal, line, line_offset, count)
	prefix = "#{line_offset}|".rjust(8) + "#{count}|".rjust(8)
	
	if count == nil
		terminal.write prefix, style: :ignored_prefix
		terminal.write line, style: :ignored_code
	elsif count == 0
		terminal.write prefix, style: :uncovered_prefix
		terminal.write line, style: :uncovered_code
	else
		terminal.write prefix, style: :covered_prefix
		terminal.write line, style: :covered_code
	end
	
	# If there was no newline at end of file, we add one:
	unless line.end_with? $/
		terminal.puts
	end
end


60
61
62
63
64
# File 'lib/covered/summary.rb', line 60

def print_line_header(terminal)
	prefix = "Line|".rjust(8) + "Hits|".rjust(8)
	
	terminal.puts prefix, style: :header_prefix
end

#terminal(output) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/covered/summary.rb', line 15

def terminal(output)
	require 'console/terminal'
	
	Console::Terminal.for(output).tap do |terminal|
		terminal[:path] ||= terminal.style(nil, nil, :bold, :underline)
		terminal[:brief_path] ||= terminal.style(:yellow)
		
		terminal[:uncovered_prefix] ||= terminal.style(:red)
		terminal[:covered_prefix] ||= terminal.style(:green)
		terminal[:ignored_prefix] ||= terminal.style(nil, nil, :faint)
		terminal[:header_prefix] ||= terminal.style(nil, nil, :faint)
		
		terminal[:uncovered_code] ||= terminal.style(:red)
		terminal[:covered_code] ||= terminal.style(:green)
		terminal[:ignored_code] ||= terminal.style(nil, nil, :faint)
		
		terminal[:annotations] ||= terminal.style(:blue)
		terminal[:error] ||= terminal.style(:red)
	end
end