~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to test/testunit/runit/test_assert.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Author:: Masaki Suketa.
 
2
# Adapted by:: Nathaniel Talbott.
 
3
# Copyright:: Copyright (c) Masaki Suketa. All rights reserved.
 
4
# Copyright:: Copyright (c) 2002 Nathaniel Talbott. All rights reserved.
 
5
# License:: Ruby license.
 
6
 
 
7
require 'rubyunit'
 
8
 
 
9
module RUNIT
 
10
  class TargetAssert
 
11
    include RUNIT::Assert
 
12
  end
 
13
 
 
14
  class TestAssert < RUNIT::TestCase
 
15
    def setup
 
16
      @assert = TargetAssert.new
 
17
      @e = nil
 
18
    end
 
19
 
 
20
    def test_assert
 
21
      sub_test_assert_pass(true)
 
22
      sub_test_assert_pass(TRUE)
 
23
      sub_test_assert_failure(false)
 
24
      sub_test_assert_failure(FALSE)
 
25
      sub_test_assert_failure(nil)
 
26
      sub_test_assert_pass("")
 
27
      sub_test_assert_pass("ok")
 
28
      sub_test_assert_pass(0)
 
29
      sub_test_assert_pass(1)
 
30
    end
 
31
 
 
32
    def test_assert_with_2_argument
 
33
      assert_no_exception {
 
34
        assert(true, "3")
 
35
      }
 
36
      assert_no_exception {
 
37
        assert(true)
 
38
      }
 
39
    end
 
40
 
 
41
    def test_assert_equal_float_0_1
 
42
      assert_proc = Proc.new {
 
43
        @assert.assert_equal_float(1.4, 1.35, 0.1)
 
44
      }
 
45
      sub_assert_pass(assert_proc)
 
46
    end
 
47
 
 
48
    def test_assert_equal_float_0_5
 
49
      assert_proc = Proc.new {
 
50
        @assert.assert_equal_float(1.4, 1.34, 0.5)
 
51
      }
 
52
      sub_assert_pass(assert_proc)
 
53
    end
 
54
 
 
55
    def test_assert_equal_float_0
 
56
      assert_proc = Proc.new {
 
57
        @assert.assert_equal_float(1.4, 1.4, 0)
 
58
      }
 
59
      sub_assert_pass(assert_proc)
 
60
    end
 
61
 
 
62
    def test_assert_equal_float_0_raise
 
63
      assert_proc = Proc.new {
 
64
        @assert.assert_equal_float(1.4, 1.34, 0)
 
65
      }
 
66
      sub_assert_raise_fail(assert_proc)
 
67
    end
 
68
 
 
69
    def test_assert_equal_float_0_01
 
70
      assert_proc = Proc.new {
 
71
        @assert.assert_equal_float(1.4, 1.35, 0.01)
 
72
      }
 
73
      sub_assert_raise_fail(assert_proc)
 
74
    end
 
75
 
 
76
    def test_assert_equal_float_0_001
 
77
      assert_proc = Proc.new {
 
78
        @assert.assert_equal_float(Math.sqrt(2), 1.414, 0.001)
 
79
      }
 
80
      sub_assert_pass(assert_proc)
 
81
    end
 
82
 
 
83
    def test_assert_equal_float_minus_1_0
 
84
      assert_proc = Proc.new {
 
85
        @assert.assert_equal_float(1.4, 1.35, -1.0)
 
86
      }
 
87
      sub_assert_raise_fail(assert_proc)
 
88
    end
 
89
 
 
90
    def test_assert_fail
 
91
      except = nil
 
92
      begin
 
93
        @assert.assert_fail("failure")
 
94
      rescue Exception
 
95
        except = $!
 
96
      end
 
97
      assert_not_nil(except)
 
98
    end
 
99
 
 
100
    def sub_test_assert_pass(obj)
 
101
      assert_proc = Proc.new {
 
102
        @assert.assert(obj)
 
103
      }
 
104
      sub_assert_pass(assert_proc)
 
105
    end
 
106
 
 
107
    def sub_test_assert_failure(obj)
 
108
      assert_proc = Proc.new {
 
109
        @assert.assert(obj)
 
110
      }
 
111
      sub_assert_raise_fail(assert_proc)
 
112
    end
 
113
 
 
114
    def test_assert_equal
 
115
      assert_proc = Proc.new {
 
116
        @assert.assert_equal(2, 2)
 
117
      }
 
118
      sub_assert_pass(assert_proc)
 
119
      assert_proc = Proc.new {
 
120
        @assert.assert_equal(2, 3)
 
121
      }
 
122
      sub_assert_raise_fail(assert_proc)
 
123
    end
 
124
 
 
125
    def test_assert_nil
 
126
      obj = nil
 
127
      assert_proc = Proc.new {
 
128
        @assert.assert_nil(obj)
 
129
      }
 
130
      sub_assert_pass(assert_proc)
 
131
      obj = 'string'
 
132
      sub_assert_raise_fail(assert_proc)
 
133
    end
 
134
 
 
135
    def test_assert_not_nil
 
136
      obj = 'string'
 
137
      assert_proc = Proc.new {
 
138
        @assert.assert_not_nil(obj)
 
139
      }
 
140
      sub_assert_pass(assert_proc)
 
141
 
 
142
      obj = nil
 
143
      sub_assert_raise_fail(assert_proc)
 
144
    end
 
145
 
 
146
    def test_assert_operator
 
147
      assert_proc = Proc.new {
 
148
        @assert.assert_operator(2, :<, 3)
 
149
      }
 
150
      sub_assert_pass(assert_proc)
 
151
      assert_proc = Proc.new {
 
152
        @assert.assert_operator(2, :>, 3)
 
153
      }
 
154
      sub_assert_raise_fail(assert_proc)
 
155
    end
 
156
 
 
157
    def test_assert_respond_to
 
158
      sub_test_assert_respond_to('string', 'sub', 'foo')
 
159
      sub_test_assert_respond_to('string', :sub, :foo)
 
160
    end
 
161
 
 
162
    def sub_test_assert_respond_to(obj, msg, dummy_msg)
 
163
      assert_proc = Proc.new {
 
164
        @assert.assert_respond_to(msg, obj)
 
165
      }
 
166
      sub_assert_pass(assert_proc)
 
167
      assert_proc = Proc.new {
 
168
        @assert.assert_respond_to(dummy_msg, obj)
 
169
      }
 
170
      sub_assert_raise_fail(assert_proc)
 
171
    end
 
172
 
 
173
    def test_assert_send
 
174
      assert_proc = Proc.new {
 
175
        ary = []
 
176
        @assert.assert_send ary, :empty?
 
177
      }
 
178
      sub_assert_pass(assert_proc)
 
179
      assert_proc = Proc.new {
 
180
        ary = [2,3]
 
181
        @assert.assert_send ary, :empty?
 
182
      }
 
183
      sub_assert_raise_fail(assert_proc)
 
184
      assert_proc = Proc.new {
 
185
        str = "abc"
 
186
        @assert.assert_send str, :sub!, "z", "y"
 
187
      }
 
188
      sub_assert_raise_fail(assert_proc)
 
189
    end
 
190
 
 
191
    def test_assert_kind_of
 
192
      assert_proc = Proc.new {
 
193
        @assert.assert_kind_of(String, "string")
 
194
      }
 
195
      sub_assert_pass(assert_proc)
 
196
      assert_proc = Proc.new {
 
197
        @assert.assert_kind_of(Regexp, "string")
 
198
      }
 
199
      sub_assert_raise_fail(assert_proc)
 
200
    end
 
201
 
 
202
    def test_assert_instance_of
 
203
      assert_proc = Proc.new {
 
204
        @assert.assert_instance_of(String, "string")
 
205
      }
 
206
      sub_assert_pass(assert_proc)
 
207
      assert_proc = Proc.new {
 
208
        @assert.assert_instance_of(Object, "string")
 
209
      }
 
210
      sub_assert_raise_fail(assert_proc)
 
211
    end
 
212
 
 
213
    def test_assert_match
 
214
      assert_proc = Proc.new{
 
215
        @assert.assert_match('foostring', /foo/)
 
216
      }
 
217
      sub_assert_pass(assert_proc)
 
218
      assert_proc = Proc.new {
 
219
        @assert.assert_match('barstring', /foo/)
 
220
      }
 
221
      sub_assert_raise_fail(assert_proc)
 
222
      match = @assert.assert_match('foostring', /foo/)
 
223
      assert_instance_of(MatchData, match)
 
224
      assert_equal('foo', match[0])
 
225
    end
 
226
 
 
227
    def test_assert_matches
 
228
      assert_proc = Proc.new{
 
229
        @assert.assert_matches('foostring', /foo/)
 
230
      }
 
231
      sub_assert_pass(assert_proc)
 
232
      assert_proc = Proc.new {
 
233
        @assert.assert_matches('barstring', /foo/)
 
234
      }
 
235
      sub_assert_raise_fail(assert_proc)
 
236
    end
 
237
 
 
238
    def test_assert_not_match
 
239
      assert_proc = Proc.new{
 
240
        @assert.assert_not_match('barstring', /foo/)
 
241
      }
 
242
      sub_assert_pass(assert_proc)
 
243
      assert_proc = Proc.new {
 
244
        @assert.assert_not_match('foostring', /foo/)
 
245
      }
 
246
      sub_assert_raise_fail(assert_proc)
 
247
      assert_proc = Proc.new {
 
248
        @assert.assert_not_match('foobarbaz', /ba.+/)
 
249
      }
 
250
      sub_assert_raise_fail(assert_proc)
 
251
    end
 
252
 
 
253
    def test_assert_same
 
254
      flag = false
 
255
      e = "foo"
 
256
      a = e
 
257
      assert_proc = Proc.new {@assert.assert_same(e, a)}
 
258
      sub_assert_pass(assert_proc)
 
259
 
 
260
      a = "foo"
 
261
      sub_assert_raise_fail(assert_proc)
 
262
    end
 
263
 
 
264
    def test_assert_exception
 
265
      assert_proc = Proc.new{
 
266
        @assert.assert_exception(IOError) {
 
267
    raise IOError
 
268
        }
 
269
      }
 
270
      sub_assert_pass(assert_proc)
 
271
 
 
272
      assert_proc = Proc.new{
 
273
        @assert.assert_exception(StandardError) {
 
274
    raise IOError
 
275
        }
 
276
      }
 
277
      sub_assert_raise_fail(assert_proc)
 
278
 
 
279
      assert_proc = Proc.new{
 
280
        @assert.assert_exception(IOError, "Exception") {
 
281
    raise StandardError
 
282
        }
 
283
      }
 
284
      sub_assert_raise_fail(assert_proc)
 
285
 
 
286
      assert_proc = Proc.new {
 
287
        @assert.assert_exception(StandardError) {
 
288
    "No Exception raised in this block"
 
289
        }
 
290
      }
 
291
      sub_assert_raise_fail(assert_proc)
 
292
 
 
293
      assert_proc = Proc.new {
 
294
        @assert.assert_exception(StandardError) {
 
295
    exit(33)
 
296
        }
 
297
      }
 
298
      sub_assert_raise_fail(assert_proc)
 
299
 
 
300
      t = @assert.assert_exception(IOError) {
 
301
        raise IOError
 
302
      }
 
303
      assert_instance_of(IOError, t)
 
304
      t = @assert.assert_exception(NameError) {
 
305
        non_existent_method
 
306
      }
 
307
      assert_instance_of(NameError, t)
 
308
      t = @assert.assert_exception(SystemExit) {
 
309
        exit(33)
 
310
      }
 
311
      assert_instance_of(SystemExit, t)
 
312
    end
 
313
 
 
314
    def test_assert_no_exception
 
315
      assert_proc = Proc.new{
 
316
        @assert.assert_no_exception(IOError, ArgumentError) {
 
317
    "No Exception raised in this block"
 
318
        }
 
319
      }
 
320
      sub_assert_pass(assert_proc)
 
321
 
 
322
      assert_proc = Proc.new{
 
323
        @assert.assert_no_exception(IOError, ArgumentError) {
 
324
    raise StandardError, "Standard Error raised"
 
325
        }
 
326
      }
 
327
      sub_assert_raise_error(assert_proc)
 
328
 
 
329
      assert_proc = Proc.new{
 
330
        @assert.assert_no_exception(IOError, ArgumentError) {
 
331
    raise ArgumentError, "Bad Argument"
 
332
        }
 
333
      }
 
334
      sub_assert_raise_fail(assert_proc)
 
335
 
 
336
      assert_proc = Proc.new{
 
337
        @assert.assert_no_exception {
 
338
          raise ArgumentError, "Bad Argument"
 
339
        }
 
340
      }
 
341
      sub_assert_raise_fail(assert_proc)
 
342
 
 
343
      assert_proc = Proc.new{
 
344
        @assert.assert_no_exception {
 
345
          raise NameError, "Bad Name"
 
346
        }
 
347
      }
 
348
      sub_assert_raise_fail(assert_proc)
 
349
      assert_proc = Proc.new {
 
350
        @assert.assert_no_exception {
 
351
    raise NoMemoryError
 
352
        }
 
353
      }
 
354
      sub_assert_raise_fail(assert_proc)
 
355
    end
 
356
 
 
357
    def sub_assert_pass(p)
 
358
      flag = false
 
359
      err = nil
 
360
      begin
 
361
        p.call
 
362
        flag = true
 
363
      rescue
 
364
        err = $!
 
365
        flag = false
 
366
      end
 
367
      assert(flag, err.to_s)
 
368
    end
 
369
 
 
370
    def sub_assert_raise_fail(p)
 
371
      flag = false
 
372
      err = nil
 
373
      begin
 
374
        p.call
 
375
        flag = false
 
376
      rescue RUNIT::AssertionFailedError
 
377
        flag = true
 
378
        err = $!
 
379
      rescue Exception
 
380
        flag = false
 
381
        err = $!
 
382
      end
 
383
      assert(flag, err.to_s)
 
384
    end
 
385
      
 
386
    def sub_assert_raise_error(p)
 
387
      flag = false
 
388
      err = nil
 
389
      begin
 
390
        p.call
 
391
        flag = false
 
392
      rescue RUNIT::AssertionFailedError
 
393
        flag = false
 
394
        err = $!
 
395
      rescue Exception
 
396
        flag = true
 
397
        err = $!
 
398
      end
 
399
      assert(flag, err.to_s)
 
400
    end
 
401
  end
 
402
end