#!/usr/bin/ruby

class Test
  
  class BlownAssert < Exception
  end

  def assert (condition)
    if !condition
      raise BlownAssert,  "Assert failed"
    end
  end

  def assertEquals (expected, actual)
    if actual != expected
      raise BlownAssert,  "Assert failed: Should be #{expected.inspect} " +
        "but was #{actual.inspect}"
    end
  end

  def assertNotEquals (expected, actual)
    if actual == expected
      raise BlownAssert,  "Assert failed: Should be != #{expected.inspect} " +
        "but was #{actual.inspect}"
    end
  end

  def assertMatches(regexp, actual)
    if regexp.match(actual) == nil
      raise BlownAssert,  "Assert failed: Should match #{regexp.inspect} " +
        "but was #{actual.inspect}"
    end
  end

  def fail(reason = "")
    raise BlownAssert,  "Test failed: #{reason}"
  end

  def assertException(exception, exceptionString = nil)
    begin
      yield
      fail("Exception #{exception} not raised")
    rescue BlownAssert
      raise
    rescue exception => e
      assertEquals(e.to_s, exceptionString) if exceptionString
    end
  end

  public
  def run
    type.public_instance_methods.each { |methodName|
      if methodName =~ /^test/
        aMethod = method(methodName)
        if aMethod.arity == 0
          setUp
          begin
            aMethod.call
          ensure
            tearDown
          end
        end
      end
    }
  end

  def setUp
  end

  def tearDown
  end

end

