filename../lib/contract/assertions.rb
total coverage100.0
code coverage100.0
 1 # This file provides a few new assertions and class methods for expressing
 2 # contracts in a comfortable way that does not require manually writing test
 3 # methods.
 4 #
 5 # See Contract.provides.
 6 
 7 
 8 class Contract < Test::Unit::TestCase
 9   # Tests that the tested Object provides the specified methods with the 
10   # specified behavior.
11   #
12   # If a block is supplied it will be evaluated in the context of the
13   # contract so <code>@object</code> will refer to the object being tested.
14   #
15   # This can be used like this:
16   #   class ListContract < Contract
17   #     provides :size do
18   #       assert(@object.size >= 0, "#size should never be negative.")
19   #     end
20   #
21   #     provides :include? 
22   #
23   #     provides :each do
24   #       count = 0
25   #       @object.each do |item|
26   #         assert(@object.include?(item),
27   #           "#each should only yield items that the list includes.")
28   #         count += 1
29   #       end
30   #       assert_equal(@object.size, count,
31   #         "#each should yield #size items.")
32   #     end
33   #   end
34   def self.provides(*symbols, &block) # :yields:
35     symbols.each do |symbol|
36       define_method("test_provides_#{symbol}".intern) do
37         assert_respond_to(@object, symbol)
38         instance_eval(&block) if block
39       end
40     end
41   end
42 end

Valid XHTML 1.1! Valid CSS!