# Ruby Treasures 0.1
# Copyright (C) 2001 Paul Brannan <paul@atdesk.com>
# 
# You may distribute this software under the same terms as Ruby (see the file
# COPYING that was distributed with this library).
# 
require 'runit/testcase'

class DS_Test_Case < RUNIT::TestCase

public

  @@methods_checked = Hash.new()
  @@class_methods_checked = Hash.new()

  def self.init_ds_test_case(klass)
    if not @@methods_checked[klass] then
      @@methods_checked[klass] = Array.new
    end
    if not @@class_methods_checked[klass] then
      @@class_methods_checked[klass] = Array.new
    end
  end

  def self.method_checked(method, klass)
    init_ds_test_case(klass)
    @@methods_checked[klass] << method.to_s
  end

  def self.class_method_checked(method, klass)
    init_ds_test_case(klass)
    @@class_methods_checked[klass] << method.to_s
  end

  def self.assert_all_public_methods_checked(klass)
    retval = assert_all_methods_checked_helper(
        :public_instance_methods,
        @@methods_checked[klass],
        "instance",
        klass)
    retval = assert_all_methods_checked_helper(
        :public_methods,
        @@class_methods_checked[klass],
        "class",
        klass) && retval
    return retval
  end

private
  def self.assert_all_methods_checked_helper(
        methods,
        methods_checked,
        str,
        klass)
    if not methods_checked then
      methods_checked = []
    end
    retval = true
    ignore_methods = Object.methods + Kernel.methods + ["append_features"]
    klass.ancestors.each do |ancestor|
      ancestor.send(methods).each do |method|
        if ignore_methods.index(method) then
          next
        end
        if not methods_checked.index(method) then
          # TODO: Integrate this check with RubyUnit
          # TODO: If we are going to return an error code due to this failing,
          # then is it really just a warning?
          puts "Warning: #{str} method #{klass}##{method} was not checked"
          retval = false
        end
      end
    end
    return retval
  end
end

