filename./lib/contract/assertions.rb
total coverage50.0
code coverage50.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
43 # This file provides a few new assertions and class methods for expressing
44 # contracts in a comfortable way that does not require manually writing test
45 # methods.
46 #
47 # See Contract.provides.
48 
49 
50 class Contract < Test::Unit::TestCase
51   # Tests that the tested Object provides the specified methods with the 
52   # specified behavior.
53   #
54   # If a block is supplied it will be evaluated in the context of the
55   # contract so <code>@object</code> will refer to the object being tested.
56   #
57   # This can be used like this:
58   #   class ListContract < Contract
59   #     provides :size do
60   #       assert(@object.size >= 0, "#size should never be negative.")
61   #     end
62   #
63   #     provides :include? 
64   #
65   #     provides :each do
66   #       count = 0
67   #       @object.each do |item|
68   #         assert(@object.include?(item),
69   #           "#each should only yield items that the list includes.")
70   #         count += 1
71   #       end
72   #       assert_equal(@object.size, count,
73   #         "#each should yield #size items.")
74   #     end
75   #   end
76   def self.provides(*symbols, &block) # :yields:
77     symbols.each do |symbol|
78       define_method("test_provides_#{symbol}".intern) do
79         assert_respond_to(@object, symbol)
80         instance_eval(&block) if block
81       end
82     end
83   end
84 end

Valid XHTML 1.1! Valid CSS!