Class: Silhouette::SourceFile

Inherits:
Object
  • Object
show all
Defined in:
lib/silhouette/coverage.rb

Constant Summary collapse

NOOP_PATTERNS =
[
  /^\s*begin/,
  /^\s*else/,
  /^\s*#/,
  /^\s*ensure/,
  /^\s*end/,
  /^\s*rescue/,
  /^\s*[}\)\]]/,
  /^\scase/        # You can have a case with no condition.
]
GREEN =
"#2bc339"
RED =
"#fd3333"
DGRAY =
"#d4d4d4"
LGRAY =
"#f4f4f4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, covered_lines) ⇒ SourceFile

Returns a new instance of SourceFile.



8
9
10
11
12
13
# File 'lib/silhouette/coverage.rb', line 8

def initialize(path, covered_lines)
  @path = path
  @coverage = covered_lines
  @in_rdoc_comment = false
  @lines = []
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



15
16
17
# File 'lib/silhouette/coverage.rb', line 15

def path
  @path
end

Instance Method Details

#calculate_hotness(count) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/silhouette/coverage.rb', line 250

def calculate_hotness(count)
  return DGRAY if @largest == 0
  count = 0 if Symbol === count
  return "white" unless count
  prec = (count / @largest.to_f)
  r = [2 * (1 - prec), 1].min * 255
  g = [2 * prec, 1].min * 255
  b = 0
  
  "#%02X%02X%02X" % [g, r, b]
end

#html_header(b) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/silhouette/coverage.rb', line 227

def html_header(b)
  color = "#a8b1f9"
  b.table(:width => "100%") do
    b.tr do
      b.td do
        b.text! @path.to_s
        b.a("[MAIN]", :href => "index.html")
      end
      b.td("Total Lines: #{@lines.size}", :bgcolor => color)
      if misses == 0
        ok = "green"
      else
        ok = "#fd3333"
      end
      b.td("Missed Lines: #{misses}", :bgcolor => ok)
      b.td("Non-Code Lines: #{non_executable}", :bgcolor => color)
    end
  end
end

#html_method_summary(b) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/silhouette/coverage.rb', line 150

def html_method_summary(b)
  b.table do
    b.tr do
      b.th("Method")
      b.th("LOC")
      b.th("Misses")
      b.th("Coverage", :colspan => 2)
    end
    
    color = DGRAY
    
    @methods.each do |mp|
      color = (color == DGRAY ? LGRAY: DGRAY)
      b.tr(:bgcolor => color) do
        klass = mp.klass
        klass = "Object" if klass.empty?
        b.td(:align => "left") do
          html = @path.to_s.gsub("/","--") + ".html"
          b.a(klass + "#" + mp.name, :href => "#{html}#line#{mp.first_line}")
        end
        b.td(mp.loc)
        b.td(mp.misses)
        if mp.loc == 0
          prec = 100
        else
          prec = (((mp.loc - mp.misses) / mp.loc.to_f) * 100).to_i
        end
        b.td do
          b.text! "#{prec}%"
        end
        b.td do
          percentage_bar(prec, b)
        end
      end  
    end
  end
end

#html_summary(b) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/silhouette/coverage.rb', line 188

def html_summary(b)
  b.td(total, :align => "right")
  b.td(:align => "right") do
    if misses == 0
      color = GREEN
    else
      color = RED
    end
    b.p(misses, :style => "color:#{color}")
  end
  b.td(:align => "right") do
    meth_misses = @missed_methods.size
    if meth_misses == 0
      color = GREEN
    else
      color = RED
    end
    b.p(meth_misses, :style => "color:#{color}")
  end
  loc_percentage = (((loc - misses) / loc.to_f) * 100).to_i
  b.td(loc, :align => "right")
  b.td { percentage_bar(loc_percentage, b) }
  b.td("#{percent}%", :align => "right")
  b.td { percentage_bar(percent, b) }
end

#locObject



131
132
133
# File 'lib/silhouette/coverage.rb', line 131

def loc
  loc = total - non_executable
end

#loc_percentageObject



135
136
137
# File 'lib/silhouette/coverage.rb', line 135

def loc_percentage
  (((loc - misses) / loc.to_f) * 100).to_i
end

#missesObject



113
114
115
# File 'lib/silhouette/coverage.rb', line 113

def misses
  miss = @lines.size - @coverage.compact.size      
end

#non_executableObject



117
118
119
# File 'lib/silhouette/coverage.rb', line 117

def non_executable
  @coverage.find_all { |i| i.to_i < 0 }.size
end

#noop_line(line) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/silhouette/coverage.rb', line 71

def noop_line(line)
  if /^=begin/.match(line)
    @in_rdoc_comment = true
    return -1
  elsif /^=end/.match(line)
    @in_rdoc_comment = false
    return -1
  elsif @in_rdoc_comment
    return -1
  elsif /^\s*$/.match(line)
    return -2
  else
    return -1 if NOOP_PATTERNS.any? { |m| m.match(line) }  
  end
end

#percentObject



143
144
145
# File 'lib/silhouette/coverage.rb', line 143

def percent
  (((total - misses) / total.to_f) * 100).to_i
end

#percentage_bar(percent, b) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/silhouette/coverage.rb', line 214

def percentage_bar(percent, b)
  b.table(:width => 100, :height => 15, :cellspacing => 0, 
      :cellpadding => 0, :bgcolor => RED) do
    b.tr do
      b.td do
        b.table(:width => "#{percent}%", :height => 15, :bgcolor => GREEN) do
          b.tr { b.td { } }
        end
      end
    end
  end
end

#to_asciiObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/silhouette/coverage.rb', line 87

def to_ascii
  output = ""
  @lines.each_with_index do |line, i|
    if count = @coverage[i+1]
      output << "* "
    else
      output << "  "              
    end
    output << line
  end
  return output
end

#to_ascii_compactObject



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/silhouette/coverage.rb', line 100

def to_ascii_compact
  output = ""
  @lines.each_with_index do |line, idx|
    lineno = idx + 1
    count = @coverage[lineno]
    unless count
      output << ("%4s " % [lineno])
      output << line
    end
  end
  return output
end

#to_html(b) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/silhouette/coverage.rb', line 262

def to_html(b)
  html_header(b)
  b.table(:width => "800", :class => "code",
        :cellspacing => 0, :cellpadding => 2) do
    i = 0
    missed_until = nil
    good_until = nil
    
    @syn_lines.each do |line|
      i += 1
      b.tr do
        count = @coverage[i]
        
        klass = "sourceLine"
        lcClass = "lineCount"
        ccClass = "coverageCount"
        
        tooltip = ""
        
        # mp = @missed_methods_start[i]
        if cmp = @method_starts[i]
          if cmp.misses == 0
            gmp = cmp
          elsif cmp.misses == cmp.loc
            mp = cmp
          end
        end
        
        if mp
          ccClass = "coverageMissing"
          klass = "sourceLineHighlight"
          missed_until = mp.last_line
          count = ""
          tooltip = "Method was never entered."
        elsif missed_until
          ccClass = "coverageMissing"
          missed_until = nil if missed_until == i
          count = ""
        elsif gmp
          ccClass = "coverageMethod"
          klass = "sourceLineGoodHighlight"
          good_until = gmp.last_line
          tooltip = "Method was completely covered."
          count = ""
        elsif good_until
          ccClass = "coverageMethod"
          good_until = nil if good_until == i
        elsif cmp
          ccClass = "coverageHit"
          klass = "sourceLinePartialHighlight"
          tooltip = "Method has #{cmp.coverage}% coverage."
          count = ""
        elsif count == -1 or count == -2
          lcClass = ccClass = "lineNonCode"
          count = ""
        elsif count
          lcClass = ccClass = "coverageHit"
        else
          klass = "sourceLineHighlight"
          ccClass = "coverageMissed"
          tooltip = "This line was never run."
        end
        
        b.td(:class => lcClass, :align => "right", :width => 20) do
          b.a(i, :name => "line#{i}")
        end
        
        # Don't show less than 0 counts (they are special)
        count = "" if count and Numeric === count and count < 0
        
        b.td(count, :class => ccClass, :align => "right", :width => 20)
        
        b.td(:class => klass) do
          b.a(:title => tooltip) do         
            b.pre(:class => klass) do
              b << line.rstrip
            end
          end
        end
=begin
        if color == RED
          color = DGRAY
          b.td(:bgcolor => color, :style => "border: medium solid red") do
            b << "<pre>#{line.rstrip}</pre>"
          end
          hotness = "white"
        else
          # Calculate the HOTNESS of the line.
          
          b.td(:bgcolor => color) do
            b << "<pre>#{line.rstrip}</pre>"
          end
          hotness = calculate_hotness count
        end
        if count and count > 0
          b.td(count, :bgcolor => hotness, :width => 65)
        end
=end
      end
    end
  end
end

#to_xml(b) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/silhouette/coverage.rb', line 121

def to_xml(b)
  b.file(:path => @path, :total => @lines.size, :missed => misses) do
    @coverage.each_with_index do |count, idx|
      next if idx == 0
      count = 0 unless count
      b.line(:times => count, :number => idx)
    end
  end
end

#totalObject



139
140
141
# File 'lib/silhouette/coverage.rb', line 139

def total
  @lines.size
end

#update_coverageObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/silhouette/coverage.rb', line 17

def update_coverage
  @lines = File.readlines @path
  i = 0
  @lines.each do |line|
    i += 1
    next if @coverage[i]
    if code = noop_line(line)
      @coverage[i] = code
    end
  end
  
  @largest = @coverage.compact.max do |a,b|
    a.to_i <=> b.to_i
  end
  
  @convertor = Silhouette::MethodFinder.for_syntax "ruby"
  @syn_lines = @convertor.convert @lines.join(""), false
  
  @methods = @convertor.methods
  @missed_methods = []
  @missed_methods_start = []
  
  @method_starts = []
  
  # Calculate the LOC and missed lines per method.

  @methods.each do |mp|
    next unless mp.first_line and mp.last_line
    (mp.first_line + 1).upto(mp.last_line - 1) do |line|
      code = @coverage[line]
      mp.loc += 1 unless code and code < 0
      mp.misses += 1 unless code
    end
    
    @method_starts[mp.first_line] = mp
    
    if mp.loc > 0 and mp.misses == mp.loc
      @missed_methods << mp
      @missed_methods_start[mp.first_line] = mp
    end
  end
end