# This file provides a few new assertions and class methods for expressing # contracts in a comfortable way that does not require manually writing test # methods. # # See Contract.provides. class Contract < Test::Unit::TestCase # Tests that the tested Object provides the specified methods with the # specified behavior. # # If a block is supplied it will be evaluated in the context of the # contract so @object will refer to the object being tested. # # This can be used like this: # class ListContract < Contract # provides :size do # assert(@object.size >= 0, "#size should never be negative.") # end # # provides :include? # # provides :each do # count = 0 # @object.each do |item| # assert(@object.include?(item), # "#each should only yield items that the list includes.") # count += 1 # end # assert_equal(@object.size, count, # "#each should yield #size items.") # end # end def self.provides(*symbols, &block) # :yields: symbols.each do |symbol| define_method("test_provides_#{symbol}".intern) do assert_respond_to(@object, symbol) instance_eval(&block) if block end end end end