Class: Lemon::CoverageAnalyzer

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

Constant Summary collapse

DEFAULT_REPORTER =
'compact'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, options = {}) ⇒ CoverageAnalyzer

New Coverage object.

CoverageAnalyzer.new(suite, :MyApp, :public => true)


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
59
60
61
62
63
64
65
# 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

Instance Attribute Details

#filesObject (readonly)

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.



97
98
99
# File 'lib/lemon/coverage/analyzer.rb', line 97

def files
  @files
end

#formatObject (readonly)

Report format.



102
103
104
# File 'lib/lemon/coverage/analyzer.rb', line 102

def format
  @format
end

#namespacesObject (readonly)



107
108
109
# File 'lib/lemon/coverage/analyzer.rb', line 107

def namespaces
  @namespaces
end

Instance Method Details

#calculateObject

Trigger a full set of calculations.



164
165
166
# File 'lib/lemon/coverage/analyzer.rb', line 164

def calculate
  uncovered_cases # that should be all it takes
end

#canonicalObject



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

def canonical
  @canonical #= Snapshot.capture
end

#canonical_casesObject



272
273
274
# File 'lib/lemon/coverage/analyzer.rb', line 272

def canonical_cases
  @canonical_cases ||= canonical.units.map{ |u| u.namespace }.uniq
end

#covered_namespacesObject



204
205
206
# 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) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# 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_unitsObject Also known as: covered



171
172
173
174
175
176
177
178
179
# 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

#currentObject

Current system snapshot.



225
226
227
# File 'lib/lemon/coverage/analyzer.rb', line 225

def current
  @current ||= Snapshot.capture
end

#each(&block) ⇒ Object

Iterate over covered units.



299
300
301
# File 'lib/lemon/coverage/analyzer.rb', line 299

def each(&block)
  covered_units.each(&block)
end

#filter(ns) ⇒ Object (private)

Filter namespaces.



365
366
367
368
369
370
371
372
373
# 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) ⇒ Object (private)



378
379
380
381
382
383
384
385
386
387
388
389
# 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

#initialize_prerequisites(options) ⇒ Object

Load in prerequisites



70
71
72
73
74
75
76
# 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?Boolean

Returns:

  • (Boolean)


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

def private?
  @private
end

#public_only?Boolean

Only use public methods for coverage.

Returns:

  • (Boolean)


132
133
134
# File 'lib/lemon/coverage/analyzer.rb', line 132

def public_only?
  !@private
end

#renderObject



396
397
398
# File 'lib/lemon/coverage/analyzer.rb', line 396

def render
  reporter.render
end

#reporterObject

All output is handled by a reporter.



403
404
405
# File 'lib/lemon/coverage/analyzer.rb', line 403

def reporter
  @reporter ||= reporter_find(format)
end

#reporter_find(format) ⇒ Object (private)



414
415
416
417
418
419
420
421
422
423
# 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_listObject (private)



428
429
430
431
432
# 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

#reset!Object

Reset coverage data for recalcuation.



286
287
288
289
290
291
292
293
294
# 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_suiteObject



81
82
83
# File 'lib/lemon/coverage/analyzer.rb', line 81

def reset_suite
  $TEST_SUITE = []
end

#sizeObject

Number of covered units.



306
307
308
# File 'lib/lemon/coverage/analyzer.rb', line 306

def size
  covered_units.size
end

#suiteObject



88
89
90
# File 'lib/lemon/coverage/analyzer.rb', line 88

def suite
  @suite
end

#suite=(suite) ⇒ Object

Raises:

  • (ArgumentError)


124
125
126
127
# File 'lib/lemon/coverage/analyzer.rb', line 124

def suite=(suite)
  raise ArgumentError unless TestSuite === suite
  @suite = suite
end

#system(*namespaces) ⇒ Object (private)



352
353
354
355
# File 'lib/lemon/coverage/analyzer.rb', line 352

def system(*namespaces)
  namespaces = nil if namespaces.empty?
  Snapshot.capture(namespaces)
end

#targetObject

Target system snapshot.



218
219
220
# File 'lib/lemon/coverage/analyzer.rb', line 218

def target
  @target ||= Snapshot.capture(target_namespaces)
end

#target_namespacesObject



211
212
213
# File 'lib/lemon/coverage/analyzer.rb', line 211

def target_namespaces
  @target_namespaces ||= filter(covered_namespaces)
end

#uncovered_casesObject

List of modules/classes not covered.



254
255
256
257
258
259
260
# 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_systemObject



265
266
267
# File 'lib/lemon/coverage/analyzer.rb', line 265

def uncovered_system
  @uncovered_system ||= Snapshot.capture(uncovered_cases)
end

#uncovered_unitsObject Also known as: uncovered



232
233
234
235
236
237
238
239
240
241
242
# 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

#undefined_unitsObject Also known as: undefined



247
248
249
# File 'lib/lemon/coverage/analyzer.rb', line 247

def undefined_units
  @undefined_units ||= covered_units - target.units
end

#zealous?Boolean

Include methods of uncovered cases in uncovered units.

Returns:

  • (Boolean)


146
147
148
# File 'lib/lemon/coverage/analyzer.rb', line 146

def zealous?
  @zealous
end