Class: Silhouette::CoverageProcessor

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

Constant Summary collapse

WONLY =
/^\s*(end)?\s*$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Processor

#process_end, #process_return, #process_start, #run

Constructor Details

#initializeCoverageProcessor

Returns a new instance of CoverageProcessor.



367
368
369
370
371
372
373
374
375
376
377
# File 'lib/silhouette/coverage.rb', line 367

def initialize
  @methods = Hash.new
  @files = Hash.new
  @coverage = Hash.new { |h,k| h[k] = [] }
  @process_all = false
  @total_lines = 0
  @total_missed = 0
  @match_files = nil
  @css = "default.css"
  @method_hits = Hash.new { |h,k| h[k] = 0 }
end

Instance Attribute Details

#coverageObject (readonly)

Returns the value of attribute coverage.



424
425
426
# File 'lib/silhouette/coverage.rb', line 424

def coverage
  @coverage
end

#cssObject

Returns the value of attribute css.



379
380
381
# File 'lib/silhouette/coverage.rb', line 379

def css
  @css
end

#filesObject (readonly)

Returns the value of attribute files.



424
425
426
# File 'lib/silhouette/coverage.rb', line 424

def files
  @files
end

#match_filesObject

Returns the value of attribute match_files.



379
380
381
# File 'lib/silhouette/coverage.rb', line 379

def match_files
  @match_files
end

#process_allObject

Returns the value of attribute process_all.



379
380
381
# File 'lib/silhouette/coverage.rb', line 379

def process_all
  @process_all
end

Instance Method Details

#add_line(file, line) ⇒ Object



396
397
398
399
400
401
402
403
# File 'lib/silhouette/coverage.rb', line 396

def add_line(file, line)
  fc = @coverage[file]
  if fc[line]
    fc[line] += 1
  else
    fc[line] = 1
  end
end

#each_fileObject



459
460
461
462
463
464
465
466
467
# File 'lib/silhouette/coverage.rb', line 459

def each_file
  processed_files do |file, hits|
    sf = SourceFile.new(file, hits)
    sf.update_coverage
    @total_lines += sf.loc
    @total_missed += sf.misses
    yield sf
  end
end

#find_in_paths(file) ⇒ Object



426
427
428
429
430
431
432
433
# File 'lib/silhouette/coverage.rb', line 426

def find_in_paths(file)
  $:.each do |path|
    cp = File.join(path, file)
    return cp if File.exists? cp
  end
  
  return file
end

#num_filesObject



453
454
455
# File 'lib/silhouette/coverage.rb', line 453

def num_files
  @coverage.find_all { |i,h| @files[i] }.size
end

#overall_percentObject



510
511
512
# File 'lib/silhouette/coverage.rb', line 510

def overall_percent
  ((@total_lines - @total_missed) / @total_lines.to_f) * 100
end

#process?(file) ⇒ Boolean

Returns:

  • (Boolean)


381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/silhouette/coverage.rb', line 381

def process?(file)
  return true if @process_all
  return false if file == "(eval)"
  
  if @match_files
    return @match_files.match(file.to_s)
  end
  
  if file[0] == ?/
    return false
  end
  
  return true
end

#process_call(thread, meth, file, line, clock) ⇒ Object



405
406
407
408
# File 'lib/silhouette/coverage.rb', line 405

def process_call(thread, meth, file, line, clock)
  return unless @files.keys.include? file
  @method_hits[meth] += 1
end

#process_file(idx, file) ⇒ Object



414
415
416
417
# File 'lib/silhouette/coverage.rb', line 414

def process_file(idx, file)
  return unless process? file
  @files[idx] = file
end

#process_line(thread, meth, file, line, clock) ⇒ Object



419
420
421
422
# File 'lib/silhouette/coverage.rb', line 419

def process_line(thread, meth, file, line, clock)
  return unless @files.keys.include? file
  add_line file, line      
end

#process_method(idx, klass, kind, meth) ⇒ Object



410
411
412
# File 'lib/silhouette/coverage.rb', line 410

def process_method(idx, klass, kind, meth)
  @methods[idx] = [klass, kind, meth].to_s
end

#processed_filesObject



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/silhouette/coverage.rb', line 435

def processed_files
  indexs = @coverage.keys.sort do |a,b|
    @files[a].to_s <=> @files[b].to_s
  end
  
  indexs.each do |idx|
    hits = @coverage[idx]
    file = @files[idx]
    if file
      unless File.exists? file
        file = find_in_paths(file)
      end
      path = Pathname.new(file)
      yield(path.cleanpath, hits)
    end
  end
end

#statsObject



484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/silhouette/coverage.rb', line 484

def stats
  output = ""
  output << "Total files: #{num_files}\n"
  output << "\n"
  total_lines = 0
  total_missed = 0
  each_file do |sf|
    output << "#{sf.path}: #{sf.total}, #{sf.loc}, #{sf.misses}, #{sf.loc_percentage}%\n"
  end
  output << "\nTotal LOC: #{total_lines}\n"
  output << "Total Missed: #{total_missed}\n"
  output << "Overall Coverage: #{overall_percent.to_i}%\n"
end

#to_ascii(compact = false) ⇒ Object



469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/silhouette/coverage.rb', line 469

def to_ascii(compact=false)
  output = ""
  processed_files do |file, hits|
    sf = SourceFile.new(file, hits)
    sf.update_coverage
    output << "================ #{file} (#{sf.total} / #{sf.loc} / #{sf.misses} / #{sf.loc_percentage}%)\n"
    if compact
      output << sf.to_ascii_compact
    else
      output << sf.to_ascii
    end
  end
  return output
end

#to_html(dir) ⇒ Object



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'lib/silhouette/coverage.rb', line 514

def to_html(dir)
  dir = Pathname.new(dir)
  paths = []
  STDOUT.sync = true
  print "Writing out html (#{num_files} total):     "
  i = 1
  each_file do |sf|
    print "\b\b\b\b\b #{"%3d" % [(i / num_files.to_f) * 100]}%"
    path = dir + "#{sf.path.to_s.gsub("/","--")}.html"
    sum = dir + "#{sf.path.to_s.gsub("/","--")}-summary.html"
    
    paths << [path, sum, sf]
    path.open("w") do |fd|
      xm = Builder::XmlMarkup.new(:target=>fd, :indent=>2)
      xm.html do
        xm.head do
          xm.link :rel => "stylesheet", :type => "text/css", :href => "default.css"
        end
        xm.body do
          sf.to_html xm
        end
      end
    end
    sum.open("w") do |fd|
      xm = Builder::XmlMarkup.new(:target => fd, :indent => 2)
      xm.html do
        xm.body do
          sf.html_method_summary xm
        end
      end
    end
    i += 1
  end
  
  css = dir + "default.css"
  unless css.exist?
    css.open("w") do |fd|
      fd << File.read(File.join(File.dirname(__FILE__), @css))
    end
  end
  
  color2 = "#dedede"
  color1 = "#a8b1f9"
  
  cur_color = color2
  
  index = dir + "index.html"
  index.open("w") do |fd|
    xm = Builder::XmlMarkup.new(:target => fd, :indent => 2)
    xm.html do
      xm.body do
        xm.h3 "Code Coverage Information"
        xm.h5 do
          xm.text!("Total Coverage: ")
          xm.b("#{overall_percent.to_i}%")
        end
        xm.h5 do
          xm.text!("Number of Files: ")
          xm.b(num_files)
        end
        
        xm.table(:cellpadding => 3, :cellspacing => 1) do
          xm.tr do
            xm.th("File")
            xm.th("Total")
            xm.th("Missed")
            xm.th("Methods Missed")
            xm.th("LOC")
            xm.th("LOC %")
            xm.th("Coverage")
            xm.th("Coverage %")
          end
          paths.each do |path, sum, sf|
            cur_color = (cur_color == color1 ? color2 : color1)
            xm.tr(:bgcolor => cur_color) do
              xm.td do
                xm.a(sf.path, :href => path.basename)
                xm.a("[M]", :href => sum.basename)
              end
              sf.html_summary(xm)
            end
          end
        end
      end
    end
  end
  
  puts "\b\b\b\b\b done."
end

#to_xmlObject



498
499
500
501
502
503
504
505
506
507
508
# File 'lib/silhouette/coverage.rb', line 498

def to_xml
  output = ""
  xm = Builder::XmlMarkup.new(:target=>output, :indent=>2)
  xm.coverage(:pwd => Dir.getwd, :time => Time.now.to_i) do
    each_file do |sf|
      sf.to_xml xm
    end
  end
  
  return output
end