0001-Add-documentation-for-lib-test-unit-assertions.rb.patch

Justin Collins, 06/19/2011 11:53 AM

Download (6.81 KB)

View differences:

lib/test/unit/assertions.rb
6 6
    module Assertions
7 7
      include MiniTest::Assertions
8 8

  
9
      def mu_pp(obj)
9
      def mu_pp(obj) #:nodoc:
10 10
        obj.pretty_inspect.chomp
11 11
      end
12 12

  
13 13
      UNASSIGNED = Object.new # :nodoc:
14 14

  
15
      # :call-seq:
16
      #   assert( test, failure_message = UNASSIGNED )
17
      #
18
      #Tests if +test+ is true.
19
      #
20
      #+msg+ may be a String or a Proc. If +msg+ is a String, it will be used
21
      #as the failure message. Otherwise, the result of calling +msg+ will be
22
      #used as the message if the assertion fails.
23
      #
24
      #If no +msg+ is given, a default message will be used.
25
      #
26
      #    assert(false, "This was expected to be true")
15 27
      def assert(test, msg = UNASSIGNED)
16 28
        case msg
17 29
        when UNASSIGNED
......
24 36
        super
25 37
      end
26 38

  
39
      # :call-seq:
40
      #   assert_block( failure_message = nil )
41
      #
42
      #Tests the result of the given block. If the block does not return true,
43
      #the assertion will fail. The optional +failure_message+ argument is the same as in
44
      #Assertions#assert.
45
      #
46
      #    assert_block do
47
      #      [1, 2, 3].any? { |num| num < 1 }
48
      #    end
27 49
      def assert_block(*msgs)
28 50
        assert yield, *msgs
29 51
      end
30 52

  
53
      # :call-seq:
54
      #   assert_raise( *args, &block )
55
      #
56
      #Tests if the given block raises an exception. Acceptable exception
57
      #types maye be given as optional arguments. If the last argument is a
58
      #String, it will be used as the error message.
59
      #
60
      #    assert_raise do #Fails, no Exceptions are raised
61
      #    end
62
      #
63
      #    assert_raise NameError do
64
      #      puts x  #Raises NameError, so assertion succeeds
65
      #    end
31 66
      def assert_raise(*args, &b)
32 67
        assert_raises(*args, &b)
33 68
      end
34 69

  
70
      # :call-seq:
71
      #   assert_nothing_raised( *args, &block )
72
      #
73
      #If any exceptions are given as arguments, the assertion will
74
      #fail if one of those exceptions are raised. Otherwise, the test fails
75
      #if any exceptions are raised.
76
      #
77
      #The final argument may be a failure message.
78
      #
79
      #    assert_nothing_raised RuntimeError do
80
      #      raise Exception #Assertion passes, Exception is not a RuntimeError
81
      #    end
82
      #
83
      #    assert_nothing_raised do
84
      #      raise Exception #Assertion fails
85
      #    end
35 86
      def assert_nothing_raised(*args)
36 87
        self._assertions += 1
37 88
        if Module === args.last
......
61 112
        nil
62 113
      end
63 114

  
115
      # :call-seq:
116
      #   assert_nothing_thrown( failure_message = nil, &block )
117
      #
118
      #Fails if the given block uses a call to Kernel#throw.
119
      #
120
      #An optional failure message may be provided as the final argument.
121
      #
122
      #    assert_nothing_thrown "Something was thrown!" do
123
      #      throw :problem?
124
      #    end
64 125
      def assert_nothing_thrown(msg=nil)
65 126
        begin
66 127
          yield
......
72 133
        assert(true, "Expected nothing to be thrown")
73 134
      end
74 135

  
136
      # :call-seq:
137
      #   assert_equal( expected, actual, failure_message = nil )
138
      #
139
      #Tests if +expected+ is equal to +actual+.
140
      #
141
      #An optional failure message may be provided as the final argument.
75 142
      def assert_equal(exp, act, msg = nil)
76 143
        msg = message(msg) {
77 144
          exp_str = mu_pp(exp)
......
118 185
        assert(exp == act, msg)
119 186
      end
120 187

  
188
      # :call-seq:
189
      #   assert_not_nil( expression, failure_message = nil )
190
      #
191
      #Tests if +expression+ is not nil.
192
      #
193
      #An optional failure message may be provided as the final argument.
121 194
      def assert_not_nil(exp, msg=nil)
122 195
        msg = message(msg) { "<#{mu_pp(exp)}> expected to not be nil" }
123 196
        assert(!exp.nil?, msg)
124 197
      end
125 198

  
199
      # :call-seq:
200
      #   assert_not_equal( expected, actual, failure_message = nil )
201
      #
202
      #Tests if +expected+ is not equal to +actual+.
203
      #
204
      #An optional failure message may be provided as the final argument.
126 205
      def assert_not_equal(exp, act, msg=nil)
127 206
        msg = message(msg) { "<#{mu_pp(exp)}> expected to be != to\n<#{mu_pp(act)}>" }
128 207
        assert(exp != act, msg)
129 208
      end
130 209

  
210
      # :call-seq:
211
      #   assert_no_match( regexp, string, failure_message = nil )
212
      #
213
      #Tests if the given Regexp does not match a given String.
214
      #
215
      #An optional failure message may be provided as the final argument.
131 216
      def assert_no_match(regexp, string, msg=nil)
132 217
        assert_instance_of(Regexp, regexp, "The first argument to assert_no_match should be a Regexp.")
133 218
        self._assertions -= 1
......
135 220
        assert(regexp !~ string, msg)
136 221
      end
137 222

  
223
      # :call-seq:
224
      #   assert_not_same( expected, actual, failure_message = nil )
225
      #
226
      #Tests if +expected+ is not the same object as +actual+.
227
      #This test uses Object#equal? to test equality.
228
      #
229
      #An optional failure message may be provided as the final argument.
230
      #
231
      #    assert_not_same("x", "x") #Succeeds
138 232
      def assert_not_same(expected, actual, message="")
139 233
        msg = message(msg) { build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__) }
140 234
<?>
......
145 239
        assert(!actual.equal?(expected), msg)
146 240
      end
147 241

  
148
      # get rid of overcounting
242
      # :call-seq:
243
      #   assert_respond_to( object, method, failure_message = nil )
244
      #
245
      #Tests if the given Object responds to +method+.
246
      #
247
      #An optional failure message may be provided as the final argument.
248
      #
249
      #    assert_respond_to("hello", :reverse)  #Succeeds
250
      #    assert_respond_to("hello", :does_not_exist)  #Fails
149 251
      def assert_respond_to obj, meth, msg = nil
252
        #get rid of overcounting
150 253
        super if !caller[0].rindex(MiniTest::MINI_DIR, 0) || !obj.respond_to?(meth)
151 254
      end
152 255

  
......
158 261
      alias assert_include assert_includes
159 262
      alias assert_not_include assert_not_includes
160 263

  
161
      def build_message(head, template=nil, *arguments)
264
      def build_message(head, template=nil, *arguments) #:nodoc:
162 265
        template &&= template.chomp
163 266
        template.gsub(/\?/) { mu_pp(arguments.shift) }
164 267
      end
165
-