lib/zentest.rb in ZenTest-3.6.1 vs lib/zentest.rb in ZenTest-3.7.0

- old
+ new

@@ -54,11 +54,11 @@ # # See ZenTestMapping for documentation on method naming. class ZenTest - VERSION = '3.6.1' + VERSION = '3.7.0' include ZenTestMapping if $TESTING then attr_reader :missing_methods @@ -126,25 +126,28 @@ # be included in the resuting array. def get_methods_for(klass, full=false) klass = self.get_class(klass) if klass.kind_of? String # WTF? public_instance_methods: default vs true vs false = 3 answers + # to_s on all results if ruby >= 1.9 public_methods = klass.public_instance_methods(false) - klass_methods = klass.singleton_methods(full) - klass_methods -= Class.public_methods(true) - klass_methods -= %w(suite new) - klass_methods = klass_methods.map { |m| "self." + m } - public_methods += klass_methods public_methods -= Kernel.methods unless full + public_methods.map! { |m| m.to_s } public_methods -= %w(pretty_print pretty_print_cycle) - klassmethods = {} - public_methods.each do |meth| + + klass_methods = klass.singleton_methods(full) + klass_methods -= Class.public_methods(true) + klass_methods = klass_methods.map { |m| "self.#{m}" } + klass_methods -= %w(self.suite new) + + result = {} + (public_methods + klass_methods).each do |meth| puts "# found method #{meth}" if $DEBUG - klassmethods[meth] = true + result[meth] = true end - return klassmethods + return result end # Return the methods for class klass, as a hash with the # method nemas as keys, and true as the value for all keys. # Unless full is true, leave out the methods for Object which @@ -163,11 +166,11 @@ the_methods -= Object.instance_methods(true) the_methods -= Kernel.methods # FIX (true) - check 1.6 vs 1.8 end the_methods.each do |meth| - klassmethods[meth] = true + klassmethods[meth.to_s] = true end end end return klassmethods end @@ -324,18 +327,24 @@ @result.push "# ERROR method #{klassname}\##{methodname} does not exist (1)" if $DEBUG and not $TESTING @error_count += 1 @missing_methods[klassname][methodname] = true end + # looks up the methods and the corresponding test methods + # in the collection already built. To reduce duplication + # and hide implementation details. + def methods_and_tests(klassname, testklassname) + return @klasses[klassname], @test_klasses[testklassname] + end + # Checks, for the given class klassname, that each method # has a corrsponding test method. If it doesn't this is # added to the information for that class def analyze_impl(klassname) testklassname = self.convert_class_name(klassname) if @test_klasses[testklassname] then - methods = @klasses[klassname] - testmethods = @test_klasses[testklassname] + methods, testmethods = methods_and_tests(klassname,testklassname) # check that each method has a test method @klasses[klassname].each_key do | methodname | testmethodname = normal_to_test(methodname) unless testmethods[testmethodname] then @@ -369,12 +378,11 @@ self.process_class(klassname, true) self.analyze_impl(klassname) end if @klasses[klassname] then - methods = @klasses[klassname] - testmethods = @test_klasses[testklassname] + methods, testmethods = methods_and_tests(klassname,testklassname) # check that each test method has a method testmethods.each_key do | testmethodname | if testmethodname =~ /^test_(?!integration_)/ then @@ -412,10 +420,23 @@ @missing_methods[klassname][test_to_normal(testmethodname)] = true end end # @klasses[klassname] end + # create a given method at a given + # indentation. Returns an array containing + # the lines of the method. + def create_method(indentunit, indent, name) + meth = [] + meth.push indentunit*indent + "def #{name}" + meth.last << "(*args)" unless name =~ /^test/ + indent += 1 + meth.push indentunit*indent + "raise NotImplementedError, 'Need to write #{name}'" + indent -= 1 + meth.push indentunit*indent + "end" + return meth + end # Walk each known class and test that each method has # a test method # Then do it in the other direction... def analyze @@ -475,28 +496,16 @@ indent += 1 meths = [] cls_methods.sort.each do |method| - meth = [] - meth.push indentunit*indent + "def #{method}" - meth.last << "(*args)" unless method =~ /^test/ - indent += 1 - meth.push indentunit*indent + "raise NotImplementedError, 'Need to write #{method}'" - indent -= 1 - meth.push indentunit*indent + "end" + meth = create_method(indentunit, indent, method) meths.push meth.join("\n") end methods.keys.sort.each do |method| next if method =~ /pretty_print/ - meth = [] - meth.push indentunit*indent + "def #{method}" - meth.last << "(*args)" unless method =~ /^test/ - indent += 1 - meth.push indentunit*indent + "raise NotImplementedError, 'Need to write #{method}'" - indent -= 1 - meth.push indentunit*indent + "end" + meth = create_method(indentunit, indent, method) meths.push meth.join("\n") end @result.push meths.join("\n\n")