Class: Lemon::CoverageAnalyzer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

New Coverage object.

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


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
59
60
61
62
63
64
# File 'lib/lemon/coverage/analyzer.rb', line 31

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.



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

def files
  @files
end

#formatObject (readonly)

Report format.



94
95
96
# File 'lib/lemon/coverage/analyzer.rb', line 94

def format
  @format
end

#namespacesObject (readonly)

Returns the value of attribute namespaces.



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

def namespaces
  @namespaces
end

Instance Method Details

#calculateObject

Trigger a full set of calculations.



137
138
139
# File 'lib/lemon/coverage/analyzer.rb', line 137

def calculate
  uncovered_cases # that should be all it takes
end

#canonicalObject



100
101
102
# File 'lib/lemon/coverage/analyzer.rb', line 100

def canonical
  @canonical #= Snapshot.capture
end

#canonical_casesObject



223
224
225
# File 'lib/lemon/coverage/analyzer.rb', line 223

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

#covered_namespacesObject



171
172
173
# File 'lib/lemon/coverage/analyzer.rb', line 171

def covered_namespaces
  @covered_namespaces ||= covered_units.map{ |u| u.namespace }.uniq
end

#covered_unit(test, list) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/lemon/coverage/analyzer.rb', line 153

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,
      :function=>test.function?
    )      
  else
    # ignore
  end
end

#covered_unitsObject Also known as: covered



142
143
144
145
146
147
148
149
150
# File 'lib/lemon/coverage/analyzer.rb', line 142

def covered_units
  @covered_units ||= (
    list = []
    suite.each do |test|
      covered_unit(test, list)
    end
    list.uniq
  )
end

#currentObject

Current system snapshot.



186
187
188
# File 'lib/lemon/coverage/analyzer.rb', line 186

def current
  @current ||= Snapshot.capture
end

#each(&block) ⇒ Object

Iterate over covered units.



244
245
246
# File 'lib/lemon/coverage/analyzer.rb', line 244

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

#initialize_prerequisites(options) ⇒ Object

Load in prerequisites



67
68
69
70
71
72
73
# File 'lib/lemon/coverage/analyzer.rb', line 67

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)


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

def private?
  @private
end

#public_only?Boolean

Only use public methods for coverage.

Returns:

  • (Boolean)


111
112
113
# File 'lib/lemon/coverage/analyzer.rb', line 111

def public_only?
  !@private
end

#renderObject



331
332
333
# File 'lib/lemon/coverage/analyzer.rb', line 331

def render
  reporter.render
end

#reporterObject

All output is handled by a reporter.



336
337
338
# File 'lib/lemon/coverage/analyzer.rb', line 336

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

#reset!Object

Reset coverage data for recalcuation.



233
234
235
236
237
238
239
240
241
# File 'lib/lemon/coverage/analyzer.rb', line 233

def reset!
  @covered_units      = nil
  @covered_namespaces = nil
  @target_namespaces  = nil
  @uncovered_units    = nil
  @undefined_units    = nil
  @target             = nil
  @current            = nil
end

#reset_suiteObject



76
77
78
# File 'lib/lemon/coverage/analyzer.rb', line 76

def reset_suite
  $TEST_SUITE = []
end

#sizeObject

Number of covered units.



249
250
251
# File 'lib/lemon/coverage/analyzer.rb', line 249

def size
  covered_units.size
end

#suiteObject



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

def suite
  @suite
end

#suite=(suite) ⇒ Object

Raises:

  • (ArgumentError)


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

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

#targetObject

Target system snapshot.



181
182
183
# File 'lib/lemon/coverage/analyzer.rb', line 181

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

#target_namespacesObject



176
177
178
# File 'lib/lemon/coverage/analyzer.rb', line 176

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

#uncovered_casesObject

List of modules/classes not covered.



209
210
211
212
213
214
215
# File 'lib/lemon/coverage/analyzer.rb', line 209

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



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

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

#uncovered_unitsObject Also known as: uncovered



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/lemon/coverage/analyzer.rb', line 191

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



204
205
206
# File 'lib/lemon/coverage/analyzer.rb', line 204

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

#zealous?Boolean

Include methods of uncovered cases in uncovered units.

Returns:

  • (Boolean)


121
122
123
# File 'lib/lemon/coverage/analyzer.rb', line 121

def zealous?
  @zealous
end