Class: Lemon::Generator
- Inherits:
-
Object
- Object
- Lemon::Generator
- Defined in:
- lib/lemon/coverage/generator.rb
Overview
Test Scaffold Generator.
Instance Attribute Summary collapse
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#group ⇒ Object
readonly
Group tests by ‘:case` or `:file`.
-
#namespaces ⇒ Object
readonly
List of class and module namespaces to limit scaffolding.
-
#tests ⇒ Object
readonly
Returns the value of attribute tests.
Instance Method Summary collapse
-
#analyzer ⇒ Object
Returns CoverageAnalyzer instance.
-
#filter(units) ⇒ Object
Filter targets to include only specified namespaces.
-
#generate ⇒ Object
Generate test template(s).
-
#grouped_units ⇒ Object
Units in groups, by file or by case.
-
#initialize(options = {}) ⇒ Generator
constructor
New Scaffold Generator.
-
#private? ⇒ Boolean
Include private and protected methods.
-
#render(units) ⇒ Object
Generate code template.
- #term_case ⇒ Object
- #term_class_method ⇒ Object
- #term_method ⇒ Object
- #units_by_case ⇒ Object
- #units_by_file ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Generator
New Scaffold Generator.
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(={}) @files = [:files] || [] @tests = [:tests] || [] @group = [:group] || :case @namespaces = [:namespaces] #@coverage = options[:coverage] @private = [:private] @caps = [:caps] #if @namespaces # @coverage ||= :all #else # @coverage ||= :uncovered #end #@snapshot = Snapshot.capture @analyzer = CoverageAnalyzer.new(files + tests, ) @suite = @analyzer.suite end |
Instance Attribute Details
#files ⇒ Object (readonly)
Returns the value of attribute files.
52 53 54 |
# File 'lib/lemon/coverage/generator.rb', line 52 def files @files end |
#group ⇒ Object (readonly)
Group tests by ‘:case` or `:file`.
58 59 60 |
# File 'lib/lemon/coverage/generator.rb', line 58 def group @group end |
#namespaces ⇒ Object (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 |
#tests ⇒ Object (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
#analyzer ⇒ Object
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 |
#generate ⇒ Object
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_units ⇒ Object
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.
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_case ⇒ Object
180 181 182 |
# File 'lib/lemon/coverage/generator.rb', line 180 def term_case @caps ? 'TestCase' : 'test_case' end |
#term_class_method ⇒ Object
185 186 187 |
# File 'lib/lemon/coverage/generator.rb', line 185 def term_class_method @caps ? 'ClassMethod' : 'class_method' end |
#term_method ⇒ Object
190 191 192 |
# File 'lib/lemon/coverage/generator.rb', line 190 def term_method @caps ? 'Method' : 'method' end |
#units_by_case ⇒ Object
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_file ⇒ Object
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 |