Class: SpecGenerator
- Inherits:
-
Object
- Object
- SpecGenerator
- Defined in:
- lib/tools/spec_generator.rb
Instance Method Summary collapse
- #attribute_arrays_and_hashes(mhash) ⇒ Object
- #attribute_name_type_pairs(mhash) ⇒ Object
- #categorize_members(cls) ⇒ Object
- #generate_tests ⇒ Object
- #get_classes ⇒ Object
-
#initialize(rootpath) ⇒ SpecGenerator
constructor
A new instance of SpecGenerator.
- #output(string) ⇒ Object
- #output_source(method) ⇒ Object
- #process_attributes(cls, scope, attributes) ⇒ Object
- #process_class(cls) ⇒ Object
- #process_constructor(cls, mhash) ⇒ Object
- #process_methods(cls, scope, mhash) ⇒ Object
- #value_for(name, return_type) ⇒ Object
Constructor Details
#initialize(rootpath) ⇒ SpecGenerator
Returns a new instance of SpecGenerator.
25 26 27 28 |
# File 'lib/tools/spec_generator.rb', line 25 def initialize(rootpath) @rootpath = rootpath @ios = $stdout end |
Instance Method Details
#attribute_arrays_and_hashes(mhash) ⇒ Object
378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/tools/spec_generator.rb', line 378 def attribute_arrays_and_hashes(mhash) arrays_and_hashes = OrderedHash.new attribute_name_type_pairs(mhash).each do |name, type| if type.include?('Array') arrays_and_hashes[name] = 'Array' elsif type.include?('Hash') arrays_and_hashes[name] = 'Hash' end end arrays_and_hashes end |
#attribute_name_type_pairs(mhash) ⇒ Object
390 391 392 393 394 395 396 397 398 |
# File 'lib/tools/spec_generator.rb', line 390 def attribute_name_type_pairs(mhash) pairs = OrderedHash.new mhash[:instance_attributes].values.each do |attribute| read = attribute[:read] return_tag = read.docstring.tag(:return) pairs[read.name] =return_tag.types[0] end pairs end |
#categorize_members(cls) ⇒ Object
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 |
# File 'lib/tools/spec_generator.rb', line 91 def categorize_members(cls) mhash = { :class_attributes => OrderedHash.new, :instance_attributes => OrderedHash.new, :class_methods => Array.new, :instance_methods => Array.new } cls.children.each do |member| attr_symbol = member.name.to_s.gsub(/=$/, '').to_sym if member.name == :initialize mhash[:constructor] = member elsif cls.class_attributes[attr_symbol] mhash[:class_attributes][attr_symbol] = cls.class_attributes[attr_symbol] elsif member.name.to_s[0..1] == '@@' #mhash[:class_attributes][attr_symbol] = cls.class_attributes[attr_symbol] elsif cls.instance_attributes[attr_symbol] mhash[:instance_attributes][attr_symbol] = cls.instance_attributes[attr_symbol] elsif not member.respond_to?(:scope) #puts 'huh?' elsif member.scope == :class mhash[:class_methods] << member elsif member.scope == :instance mhash[:instance_methods] << member end end mhash end |
#generate_tests ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/tools/spec_generator.rb', line 30 def generate_tests spec_pathname = Pathname(@rootpath).join('spec', 'temp', "temp_unit_tests") classes = get_classes classes.each do |cls| test_pathname = spec_pathname.join(cls.path.snake_case + "_spec.rb") test_pathname.parent.mkpath #puts test_pathname.to_s unless test_pathname.exist? begin @constructor_params = Array.new @indent = 0 @ios = test_pathname.open("w") process_class(cls) ensure @ios.close end end end end |
#get_classes ⇒ Object
50 51 52 53 54 |
# File 'lib/tools/spec_generator.rb', line 50 def get_classes() yardoc = File.join(@rootpath, '.yardoc') Registry.load!(yardoc) # loads all objects into memory Registry.all(:class) # Array end |
#output(string) ⇒ Object
57 58 59 |
# File 'lib/tools/spec_generator.rb', line 57 def output(string) @ios.puts " "*@indent + string end |
#output_source(method) ⇒ Object
400 401 402 403 404 |
# File 'lib/tools/spec_generator.rb', line 400 def output_source(method) output " " lines = method.source.split(/\n/) lines.each { |line| output "# #{line}" } end |
#process_attributes(cls, scope, attributes) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/tools/spec_generator.rb', line 193 def process_attributes(cls, scope, attributes) output "" output "describe '=========================== #{scope.to_s.upcase} ATTRIBUTES ===========================' do" @indent += 1 if scope == :instance output "" output "before(:all) do" @indent += 1 @constructor_params.each do |p| if p.name == 'opts' output "opts = {}" else output "#{p.name} = #{value_for(p.name, p.types[0])} " end end cls_instance = cls.name.snake_case param_list = (@constructor_params.collect { |p| p.name }).join(', ') output "@#{cls_instance} = #{cls.name}.new(#{param_list})" @indent -= 1 output "end" end attributes.values.each do |attribute| if attribute.nil? puts "huh?" end write = attribute[:write] read = attribute[:read] return_tag = read.docstring.tag(:return) return_type = return_tag.types[0] output "" output "# Unit test for attribute: {#{read.path}}" output "# Which stores: [#{return_type}] #{return_tag.text.gsub(/\n/, ' ')}" output "specify '#{read.path}' do" @indent += 1 output "value = #{value_for(read.name, return_type)}" case scope when :class output "#{write.path} value" output "#{read.path}.should == value" when :instance output "@#{cls.name.snake_case}.#{write.name} value" output "@#{cls.name.snake_case}.#{read.name}.should == value" end output_source(write) output_source(read) @indent -= 1 output "end" end @indent -= 1 output "" output "end" end |
#process_class(cls) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/tools/spec_generator.rb', line 62 def process_class(cls) output %q{require File.join('..', '..','spec_helper')} output "" output "# Unit tests for class {#{cls.path}}" output "describe '#{cls.path}' do" @indent += 1 mhash = categorize_members(cls) if mhash[:class_attributes].size > 0 process_attributes(cls, :class, mhash[:class_attributes]) end if mhash[:class_methods].size > 0 process_methods(cls, :class, mhash) end process_constructor(cls, mhash) if mhash[:instance_attributes].size > 0 process_attributes(cls, :instance, mhash[:instance_attributes]) end if mhash[:instance_methods].size > 0 process_methods(cls, :instance, mhash) end @indent -= 1 output "" output "end" end |
#process_constructor(cls, mhash) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/tools/spec_generator.rb', line 119 def process_constructor(cls, mhash) output "" output "describe '=========================== CONSTRUCTOR ===========================' do" @indent += 1 method = mhash[:constructor] if method.respond_to?(:docstring) @constructor_params = method.docstring.(:param) output "" output "# Unit test for constructor: {#{method.path}}" output "# Which returns an instance of: [#{cls.path}]" output "# For input parameters:" @constructor_params.each do |p| output "# * #{p.name} [#{p.types.join(', ')}] = #{p.text.gsub(/\n/, ' ')} " end output "specify '#{method.path}' do" @indent += 1 cls_instance = cls.name.snake_case param_list = (@constructor_params.collect { |p| p.name }).join(', ') output " " output "# test initialization with required parameters (if any)" @constructor_params.each do |p| if p.name == 'opts' output "opts = {}" else output "#{p.name} = #{value_for(p.name, p.types[0])} " end end output "#{cls_instance} = #{cls.name}.new(#{param_list})" output "#{cls_instance}.should be_instance_of(#{cls.name})" @constructor_params.each do |p| unless p.name == 'opts' output "#{cls_instance}.#{p.name}.should == #{p.name}" end end arrays_and_hashes = attribute_arrays_and_hashes(mhash) if arrays_and_hashes.size > 0 output " " output "# test initialization of arrays and hashes" arrays_and_hashes.each do |attr_name, type| output "#{cls_instance}.#{attr_name}.should be_kind_of(#{type})" end end @constructor_params.each do |p| if p.name == 'opts' output " " output "# test initialization with options hash" output "opts = OrderedHash.new" attribute_name_type_pairs(mhash).each do |name, type| output "opts[:#{name}] = #{value_for(name, type)}" end output "#{cls_instance} = #{cls.name}.new(#{param_list})" attribute_name_type_pairs(mhash).each do |name, type| output "#{cls_instance}.#{name}.should == opts[:#{name}]" end end end output_source(method) @indent -= 1 output "end" end @indent -= 1 output "" output "end" end |
#process_methods(cls, scope, mhash) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/tools/spec_generator.rb', line 252 def process_methods(cls, scope, mhash) output "" output "describe '=========================== #{scope.to_s.upcase} METHODS ===========================' do" @indent += 1 if scope == :instance output "" output "before(:each) do" @indent += 1 @constructor_params.each do |p| if p.name == 'opts' output "@opts = {}" else output "@#{p.name} = #{value_for(p.name, p.types[0])} " end end cls_instance = cls.name.snake_case param_list = (@constructor_params.collect { |p| '@'+p.name }).join(', ') output "@#{cls_instance} = #{cls.name}.new(#{param_list})" output "" attribute_name_type_pairs(mhash).each do |name, type| output "@#{cls_instance}.#{name} = #{value_for(name, type)}" end @indent -= 1 output "end" end case scope when :class methods =mhash[:class_methods] when :instance methods =mhash[:instance_methods] end methods.each do |method| begin return_tag = method.docstring.tag(:return) return_type = return_tag.types[0] params = method.docstring.(:param) output "" output "# Unit test for method: {#{method.path}}" output "# Which returns: [#{return_type}] #{return_tag.text.gsub(/\n/, ' ')}" if params.length > 0 output "# For input parameters:" params.each do |p| output "# * #{p.name} [#{p.types.join(', ')}] = #{p.text.gsub(/\n/, ' ')} " end else output "# For input parameters: (None)" end output "specify '#{method.path}' do" @indent += 1 params.each do |p| output "#{p.name} = #{value_for(p.name, p.types[0])} " end param_list = (params.collect { |p| p.name }).join(', ') if return_type == 'void' case scope when :class output "#{method.path}(#{param_list})" when :instance output "@#{cls.name.snake_case}.#{method.name}(#{param_list})" end else case scope when :class output "#{method.path}(#{param_list}).should == " when :instance output "@#{cls.name.snake_case}.#{method.name}(#{param_list}).should == " end end output_source(method) @indent -= 1 output "end" rescue Exception => e raise "Error processing method: #{method.path} - " + e. e.backtrace end end @indent -= 1 output "" output "end" end |
#value_for(name, return_type) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/tools/spec_generator.rb', line 346 def value_for(name, return_type) case return_type when 'Symbol' ":test_#{name}" when 'Integer' rand(100).to_s when 'String' "'Test #{name}'" when 'Boolean' "true" when 'Pathname' "Pathname.new('/test/#{name}')" when 'Time' "Time.now" else type = return_type.split(/[<>]/) if type.length > 1 case type[0] when 'Array' "[#{value_for(name, type[1])}]" when 'Hash', 'OrderedHash' key, value = type[1].split(/[,]/) "{#{value_for(name, key)} => #{value_for(name, value)}}" else "double(#{return_type}.name)" end else "double(#{return_type}.name)" end end end |