class Lemon::CoverageAnalyzer

Constants

DEFAULT_REPORTER

Attributes

files[R]

Paths of lemon tests and/or ruby scripts to be compared and covered. This can include directories too, in which case all .rb scripts below then directory will be included.

format[R]

Report format.

namespaces[R]

Public Class Methods

new(files, options={}) click to toggle source

New Coverage object.

CoverageAnalyzer.new(suite, :MyApp, :public => true)
# File lib/lemon/coverage/analyzer.rb, line 32
def initialize(files, options={})
  @files = files

  @namespaces = [options[:namespaces]].flatten.compact
  @private    = options[:private]
  @format     = options[:format]
  @zealous    = options[:zealous]

  @reporter   = reporter_find(@format)

  reset_suite

  initialize_prerequisites(options)

  @canonical = Snapshot.capture #system #@suite.canonical

  #@suite = Lemon.suite
  #@suite      = Lemon::TestSuite.new(files, :cover=>true)  #@suite = suite
  #Lemon.suite = @suite

  files = files.map{ |f| Dir[f] }.flatten
  files = files.map{ |f| 
    if File.directory?(f)
      Dir[File.join(f, '**/*.rb')]
    else
      f 
    end
  }.flatten.uniq
  files = files.map{ |f| File.expand_path(f) }

  files.each{ |s| load s } #require s }

  @suite = $TEST_SUITE.dup
end

Public Instance Methods

calculate() click to toggle source

Trigger a full set of calculations.

# File lib/lemon/coverage/analyzer.rb, line 164
def calculate
  uncovered_cases # that should be all it takes
end
canonical() click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 117
def canonical
  @canonical #= Snapshot.capture
end
canonical_cases() click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 272
def canonical_cases
  @canonical_cases ||= canonical.units.map{ |u| u.namespace }.uniq
end
covered()
Alias for: covered_units
covered_namespaces() click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 204
def covered_namespaces
  @covered_namespaces ||= covered_units.map{ |u| u.namespace }.uniq
end
covered_unit(test, list) click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 184
def covered_unit(test, list)
  case test
  when Lemon::TestModule
    test.each do |t|
      covered_unit(t, list)
    end
  when Lemon::TestMethod
    list << Snapshot::Unit.new(
      test.context.target,
      test.target,
      :singleton=>test.class_method?
    )
  else
    # ignore
  end
end
covered_units() click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 171
def covered_units
  @covered_units ||= (
    list = []
    suite.each do |test|
      covered_unit(test, list)
    end
    list.uniq
  )
end
Also aliased as: covered
current() click to toggle source

Current system snapshot.

# File lib/lemon/coverage/analyzer.rb, line 225
def current
  @current ||= Snapshot.capture
end
each(&block) click to toggle source

Iterate over covered units.

# File lib/lemon/coverage/analyzer.rb, line 299
def each(&block)
  covered_units.each(&block)
end
initialize_prerequisites(options) click to toggle source

Load in prerequisites

# File lib/lemon/coverage/analyzer.rb, line 70
def initialize_prerequisites(options)
  loadpath = [options[:loadpath] || []].compact.flatten
  requires = [options[:requires] || []].compact.flatten

  loadpath.each{ |path| $LOAD_PATH.unshift(path) }
  requires.each{ |path| require(path) }
end
private?() click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 139
def private?
  @private
end
public_only?() click to toggle source

Only use public methods for coverage.

# File lib/lemon/coverage/analyzer.rb, line 132
def public_only?
  !@private
end
render() click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 396
def render
  reporter.render
end
reporter() click to toggle source

All output is handled by a reporter.

# File lib/lemon/coverage/analyzer.rb, line 403
def reporter
  @reporter ||= reporter_find(format)
end
reset!() click to toggle source

Reset coverage data for recalcuation.

# File lib/lemon/coverage/analyzer.rb, line 286
def reset!
  @covered_units      = nil
  @covered_namespaces = nil
  @target_namespaces  = nil
  @uncovered_units    = nil
  @undefined_units    = nil
  @target             = nil
  @current            = nil
end
reset_suite() click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 81
def reset_suite
  $TEST_SUITE = []
end
size() click to toggle source

Number of covered units.

# File lib/lemon/coverage/analyzer.rb, line 306
def size
  covered_units.size
end
suite() click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 88
def suite
  @suite
end
suite=(suite) click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 124
def suite=(suite)
  raise ArgumentError unless TestSuite === suite
  @suite = suite
end
target() click to toggle source

Target system snapshot.

# File lib/lemon/coverage/analyzer.rb, line 218
def target
  @target ||= Snapshot.capture(target_namespaces)
end
target_namespaces() click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 211
def target_namespaces
  @target_namespaces ||= filter(covered_namespaces)
end
uncovered()
Alias for: uncovered_units
uncovered_cases() click to toggle source

List of modules/classes not covered.

# File lib/lemon/coverage/analyzer.rb, line 254
def uncovered_cases
  @uncovered_cases ||= (
    list = current.units - (target.units + canonical.units)
    list = list.map{ |u| u.namespace }.uniq
    list - canonical_cases
  )
end
uncovered_system() click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 265
def uncovered_system
  @uncovered_system ||= Snapshot.capture(uncovered_cases)
end
uncovered_units() click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 232
def uncovered_units
  @uncovered_units ||= (
    units = target.units
    if public_only?
      units = units.select{ |u| u.public? }
    end
    units -= (covered_units + canonical.units)
    units += uncovered_system.units if zealous?
    units
  )
end
Also aliased as: uncovered
undefined()
Alias for: undefined_units
undefined_units() click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 247
def undefined_units
  @undefined_units ||= covered_units - target.units
end
Also aliased as: undefined
zealous?() click to toggle source

Include methods of uncovered cases in uncovered units.

# File lib/lemon/coverage/analyzer.rb, line 146
def zealous?
  @zealous
end

Private Instance Methods

filter(ns) click to toggle source

Filter namespaces.

# File lib/lemon/coverage/analyzer.rb, line 365
def filter(ns)
  return ns if namespaces.nil? or namespaces.empty?
  #units = units.reject do |u|
  #  /^Lemon::/ =~ u.namespace.to_s
  #end
  ns.select do |u|
    namespaces.any?{ |ns| /^#{ns}(::|$)/ =~ u.namespace.to_s }
  end
end
filter_units(units) click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 378
def filter_units(units)
  return units if namespaces.nil? or namespaces.empty?
  #units = units.reject do |u|
  #  /^Lemon::/ =~ u.namespace.to_s
  #end
  units = units.select do |u|
    namespaces.any? do |ns|
      /^#{ns}/ =~ u.namespace.to_s
    end
  end
  units
end
reporter_find(format) click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 414
def reporter_find(format)
  format = format ? format.to_s.downcase : DEFAULT_REPORTER
  format = reporter_list.find do |name|
    /^#{format}/ =~ name
  end
  raise "unsupported format" unless format
  require "lemon/coverage/formats/#{format}"
  reporter = Lemon::CoverReports.const_get(format.capitalize)
  reporter.new(self)
end
reporter_list() click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 428
def reporter_list
  Dir[File.dirname(__FILE__) + '/formats/*.rb'].map do |rb|
    File.basename(rb).chomp('.rb')
  end
end
system(*namespaces) click to toggle source
# File lib/lemon/coverage/analyzer.rb, line 352
def system(*namespaces)
  namespaces = nil if namespaces.empty?
  Snapshot.capture(namespaces)
end