Class: Lemon::Generator

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

Overview

Test Scaffold Generator.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Generator

New Scaffold Generator.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :files (Array)

    Ruby scripts for which to generate tests.

  • :tests (Array)

    Test files that already exist.

  • :namespaces (Array)

    List of class/module names to limit scaffolding.

  • :private (Boolean)

    Include private methods in scaffolding.

  • :group (Symbol)

    Group by ‘:case` or by `:file`.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/lemon/coverage/generator.rb', line 29

def initialize(options={})
  @files      = options[:files] || []
  @tests      = options[:tests] || []
  @group      = options[:group] || :case

  @namespaces = options[:namespaces]
  #@coverage   = options[:coverage]
  @private    = options[:private]
  @caps       = options[:caps]

  #if @namespaces
  #  @coverage ||= :all
  #else
  #  @coverage ||= :uncovered
  #end

  #@snapshot   = Snapshot.capture

  @analyzer   = CoverageAnalyzer.new(files + tests, options)
  @suite      = @analyzer.suite
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



52
53
54
# File 'lib/lemon/coverage/generator.rb', line 52

def files
  @files
end

#groupObject (readonly)

Group tests by ‘:case` or `:file`.



58
59
60
# File 'lib/lemon/coverage/generator.rb', line 58

def group
  @group
end

#namespacesObject (readonly)

List of class and module namespaces to limit scaffolding.



61
62
63
# File 'lib/lemon/coverage/generator.rb', line 61

def namespaces
  @namespaces
end

#testsObject (readonly)

Returns the value of attribute tests.



55
56
57
# File 'lib/lemon/coverage/generator.rb', line 55

def tests
  @tests
end

Instance Method Details

#analyzerObject

Returns CoverageAnalyzer instance.



74
75
76
# File 'lib/lemon/coverage/generator.rb', line 74

def analyzer
  @analyzer
end

#filter(units) ⇒ Object

Filter targets to include only specified namespaces.



145
146
147
148
149
150
151
152
# File 'lib/lemon/coverage/generator.rb', line 145

def filter(units)
  return units if namespaces.nil? or namespaces.empty?
  units.select do |u|
    namespaces.any? do |ns|
      /^#{ns}/ =~ u.namespace.to_s
    end
  end
end

#generateObject

Generate test template(s).



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/lemon/coverage/generator.rb', line 91

def generate
  render_map = {}

  if tests.empty?
    grouped_units.each do |group, units|
      units = filter(units)
      render_map[group] = render(units)
    end
  else
    uncovered_units = analyzer.uncovered
    grouped_units.each do |group, units|
      units = filter(units)
      units = units & uncovered_units
      render_map[group] = render(units)
    end
  #when :covered
  #  covered_units = analyzer.target.units
  #  grouped_units.each do |group, units|
  #    units = filter(units)
  #    units = units & covered_units
  #    map[group] = render(units)
  #  end
  #else
  #  #units = Snapshot.capture(namespaces).units
  #  #units = (units - @snapshot.units)
  end

  render_map
end

#grouped_unitsObject

Units in groups, by file or by case.



79
80
81
82
83
84
85
86
87
88
# File 'lib/lemon/coverage/generator.rb', line 79

def grouped_units
  case group
  when :case
    units_by_case
  when :file
    units_by_file
  else
    units_by_case # default ?
  end
end

#private?Boolean

Include private and protected methods.

Returns:

  • (Boolean)


69
70
71
# File 'lib/lemon/coverage/generator.rb', line 69

def private?
  @private
end

#render(units) ⇒ Object

Generate code template.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/lemon/coverage/generator.rb', line 155

def render(units)
  code = []
  mods = units.group_by{ |u| u.namespace }
  mods.each do |mod, units|
    code << "#{term_case} #{mod} do"
    units.each do |unit|
      next unless private? or unit.public?
      if unit.singleton?
        code << "\n  #{term_class_method} :#{unit.method} do"
        code << "\n    test '' do"
        code << "\n    end"
        code << "\n  end"
      else
        code << "\n  #{term_method} :#{unit.method} do"
        code << "\n    test '' do"
        code << "\n    end"
        code << "\n  end"
      end
    end
    code << "\nend\n"
  end
  code.join("\n")
end

#term_caseObject



180
181
182
# File 'lib/lemon/coverage/generator.rb', line 180

def term_case
  @caps ? 'TestCase' : 'test_case'
end

#term_class_methodObject



185
186
187
# File 'lib/lemon/coverage/generator.rb', line 185

def term_class_method
  @caps ? 'ClassMethod' : 'class_method'
end

#term_methodObject



190
191
192
# File 'lib/lemon/coverage/generator.rb', line 190

def term_method
  @caps ? 'Method' : 'method'
end

#units_by_caseObject



126
127
128
129
130
131
132
# File 'lib/lemon/coverage/generator.rb', line 126

def units_by_case
  units = []
  files.each do |file|
    units.concat SourceParser.parse_units(File.read(file))
  end
  units.group_by{ |u| u.namespace }
end

#units_by_fileObject



135
136
137
138
139
140
141
142
# File 'lib/lemon/coverage/generator.rb', line 135

def units_by_file
  map = {}
  files.each do |file|
    units = SourceParser.parse_units(File.read(file))
    map[file] = units
  end
  map
end