File: 10_asserts.t

package info (click to toggle)
libtest-simpleunit-perl 1.21-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 264 kB
  • sloc: perl: 1,215; makefile: 2
file content (495 lines) | stat: -rw-r--r-- 15,268 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#!/usr/bin/perl
#
#		Test script for Test::SimpleUnit (import functions)
#		$Id: 10_asserts.t,v 1.4 2002/04/08 18:50:24 deveiant Exp $
#
#		Before `make install' is performed this script should be runnable with
#		`make test'. After `make install' it should work as `perl t/01_import.t'
#
#		Please do not commit any changes you make to the module without a
#		successful 'make test'!
#

# Packages for testing OO asserts
package ClassA;
sub new {return bless {}, $_[0]}

package ClassB;
use base qw{ClassA};

package ClassC;
use base qw{ClassB};

# Main testing package
package main;
use strict;

BEGIN	{ $| = 1; }

### Load up the test framework
use Test::SimpleUnit	qw{:asserts};

my @testSuite = (

	# Test the basic assert() function
	{
		name => 'Assert',
		test => sub {
			# Assert( true )
			eval { assert(1); };
			die "Failed assert(1): $@" if $@;

			# Assert( false )
			eval { assert(0); };
			die "assert(0) unexpectedly succeeded." unless $@;
			die "Unexpected error message for assert(0): $@ (expected '0')"
				unless "$@" =~ m{0};

			# Assert( false ) with message
			eval { assert(0, "message test") };
			die "assert(0,msg) unexpectedly succeeded." unless $@;
			die "Unexpected error message for assert(0): $@ (expected 'message test')"
				unless "$@" =~ m{message test};
		},
	},

	# Test assertNot()
	{
		name => 'AssertNot',
		test => sub {
			# assertNot( 0 )
			eval { assertNot(0); };
			die "Failed assertNot(0): $@" if $@;

			# assertNot( "" )
			eval { assertNot(""); };
			die "Failed assertNot(\"\"): $@" if $@;

			# assertNot( undef )
			eval { assertNot(undef); };
			die "Failed assertNot(undef): $@" if $@;

			# assertNot( 1 )
			eval { assertNot(1); };
			die "assertNot(1) unexpectedly succeeded." unless $@;
			die "Unexpected error message for assertNot(1): $@ (expected 'Expected a false value, got \"1\"')"
				unless "$@" =~ m{Expected a false value, got '1'};

			# AssertNot( false ) with message
			eval { assertNot(1, "message test") };
			die "assertNot(1,msg) unexpectedly succeeded." unless $@;
			die "Unexpected error message for assertNot(0): $@ (expected 'message test')"
				unless "$@" =~ m{message test};
		},
	},

	# Test assertDefined()
	{
		name => 'AssertDefined',
		test => sub {
			# assertDefined( 0 )
			eval { assertDefined(0); };
			die "Failed assertDefined(0): $@" if $@;

			# assertDefined( "" )
			eval { assertDefined(""); };
			die "Failed assertDefined(\"\"): $@" if $@;

			# assertDefined( undef )
			eval { assertDefined(undef); };
			die "assertDefined(undef) unexpectedly succeeded." unless $@;
			die "Unexpected error message for assertDefined(undef): $@ ",
				"(expected 'Expected a defined value, got an undef')"
				unless "$@" =~ m{Expected a defined value, got an undef};

			# AssertDefined( undef ) with message
			eval { assertDefined(undef, "message test") };
			die "assertDefined(undef,msg) unexpectedly succeeded." unless $@;
			die "Unexpected error message for assertDefined(undef,msg): $@ ",
				"(expected 'message test')"
				unless "$@" =~ m{message test};
		},
	},

	# Test assertUndef()
	{
		name => 'AssertUndef',
		test => sub {
			# assertUndef( undef )
			eval { assertUndef(undef); };
			die "Failed assertUndef(undef): $@" if $@;

			# assertUndef( undef )
			eval { assertUndef(1); };
			die "assertUndef(1) unexpectedly succeeded." unless $@;
			die "Unexpected error message for assertUndef(1): $@ ",
				"(expected 'Expected an undefined value, got '1'')"
				unless "$@" =~ m{Expected an undefined value, got '1'};

			# AssertUndef( undef ) with message
			eval { assertUndef(1, "message test") };
			die "assertUndef(1,msg) unexpectedly succeeded." unless $@;
			die "Unexpected error message for assertUndef(1,msg): $@ ",
				"(expected 'message test')"
				unless "$@" =~ m{message test};
		},
	},

	# Test assertException()
	{
		name => 'AssertException',
		test => sub {
			my $res;

			# assertException { die "test" }
			$res = eval { assertException {die "test"}; };
			die "Failed assertException {die \"test\"}: $@" if $@;
			assert( $res );
			undef $res;

			# assertException { 1 }
			eval { assertException {1} };
			die "assertException unexpectedly succeeded" unless $@;
			die "Unexpected error message for assertException {1}: $@ ",
				"(expected 'No exception raised.')"
				unless "$@" =~ m{No exception raised\.};

			# assertException { 1 }, $msg
			eval { assertException {1} "Ack! No exception?"; };
			die "assertException unexpectedly succeeded" unless $@;
			die "Unexpected error message for assertException {1}: $@ ",
				"(expected 'Ack! No exception?')"
				unless "$@" =~ m{Ack! No exception\?};
		},
	},

	# Test assertExceptionType()
	{
		name => 'AssertExceptionType',
		test => sub {

			# assertExceptionType { die "test" }
			eval { assertExceptionType {die bless ["test"], 'test'} 'test'; };
			die "Failed assertExceptionType {die bless [\"test\"], 'test'} 'test': $@" if $@;

			# assertExceptionType { 1 }
			eval { assertExceptionType {1} 'any' };
			die "assertExceptionType unexpectedly succeeded" unless $@;
			die "Unexpected error message for assertExceptionType {1} 'any': $@ ",
				"(expected 'Expected an exception of type 'any', but none was raised. at ",
				"blib/lib/Test/SimpleUnit.pm line...')"
				unless "$@" =~ m{Expected an exception of type 'any', but none was raised\.};

			# assertExceptionType { 1 }, $msg
			eval { assertExceptionType {1} 'any', "Ack! No exception?"; };
			die "assertExceptionType unexpectedly succeeded" unless $@;
			die "Unexpected error message for assertExceptionType {1} 'any', \"Ack! No exception?\": $@ ",
				"(expected 'Ack! No exception?')"
				unless "$@" =~ m{Ack! No exception\?};
		},
	},

	# Test assertExceptionMatch()
	{
		name => 'AssertExceptionMatches',
		test => sub {

			# Match a die()
			eval { assertExceptionMatches {die "Just testing"} qr{test}i; };
			die "Failed assertExceptionMatches {die \"Just testing\"} qr{test}i: $@" if $@;

			# assertExceptionMatches { 1 } 'any'
			eval { assertExceptionMatches {1} qr{any} };
			die "assertExceptionMatches unexpectedly succeeded" unless $@;
			if ( $] >= 5.014 ) {
				die "Unexpected error message for assertExceptionMatches {1} qr{any}: $@ ",
					"(expected 'Expected an exception which matched /(?^:any)/, but none ",
					"was raised.')"
					unless "$@" =~ m{Expected an exception which matched \Q/(?^:any)/\E, but none was raised\.};
			} else {
				die "Unexpected error message for assertExceptionMatches {1} qr{any}: $@ ",
					"(expected 'Expected an exception which matched /(?-xism:any)/, but none ",
					"was raised.')"
					unless "$@" =~ m{Expected an exception which matched \Q/(?-xism:any)/\E, but none was raised\.};
			}

			# assertExceptionMatches { 1 } 'any', $msg
			eval { assertExceptionMatches {1} 'any', "Ack! No exception?"; };
			die "assertExceptionMatches unexpectedly succeeded" unless $@;
			die "Unexpected error message for assertExceptionMatches {1} 'any', \"Ack! No exception?\": $@ ",
				"(expected 'Ack! No exception?')"
				unless "$@" =~ m{Ack! No exception\?};
		},
	},

	# Test assertNoException()
	{
		name => 'AssertNoException',
		test => sub {
			my $res;
			my $file = __FILE__;
			my $line;

			# assertNoException { 1 }
			$res = eval { assertNoException {1}; };
			die "Failed assertNoException {1}: $@" if $@;
			assert( $res );
			undef $res;

			# assertNoException { die "test" }
			$line = __LINE__ + 1;
			eval { assertNoException {die "test"} };
			die "assertNoException unexpectedly succeeded" unless $@;
			die "Unexpected error message for assertNoException {die \"test\"}: $@ ",
				"(expected 'Exception raised: test at $file line $line')"
				unless "$@" =~ m{Exception raised: test at $file line $line};

			# assertNoException { die "test" }, $msg
			eval { assertNoException {die "test"} "Ack! Exception raised!"; };
			die "assertNoException unexpectedly succeeded" unless $@;
			die "Unexpected error message for assertNoException {die \"test\"}: $@ ",
				"(expected 'Ack! Exception raised!')"
				unless "$@" =~ m{Ack! Exception raised!};
		},
	},

	# Test assertEquals()
	{
		name => 'AssertEquals',
		test => sub {
			my $res;

			# assertEquals( 1, 1 )
			assertNoException { $res = assertEquals( 1, 1 ) };
			assert( $res );
			undef $res;

			# assertEquals( 1, "1" )
			assertNoException { $res = assertEquals( 1, "1" ) };
			assert( $res );
			undef $res;

			# assertEquals( "this", "this" )
			assertNoException { $res = assertEquals( "this", "this" ) };
			assert( $res );
			undef $res;

			# assertEquals( undef, undef )
			assertNoException { $res = assertEquals( undef, undef ) };
			assert( $res );
			undef $res;

			# assertEquals( 1, 2 )
			assertExceptionMatches { $res = assertEquals(1, 2) } qr{Wanted '1', got '2' instead};
			assertNot( $res );
			undef $res;

			# assertEquals( 1, 1.1 )
			assertExceptionMatches { $res = assertEquals(1, 1.1) } qr{Wanted '1', got '1.1' instead};
			assertNot( $res );
			undef $res;

		},
	},

	# Test assertMatches()
	{
		name => 'AssertMatches',
		test => sub {
			my $res;

			# assertMatches( '\d+', 1 )
			assertNoException { $res = assertMatches( '\d+', 1 ) };
			assert( $res );
			undef $res;

			# assertMatches( qr{\d+}, 1 )
			assertNoException { $res = assertMatches( qr{\d+}, 1 ) };
			assert( $res );
			undef $res;

			# assertMatches( qr{\s+}, " 1" )
			assertNoException { $res = assertMatches( qr{\s+}, " 1" ) };
			assert( $res );
			undef $res;

			# assertMatches( qr{\s+}, 1 )
			if ( $] >= 5.014 ) {
				assertExceptionMatches {
					$res = assertMatches( qr{\s+}, 1 )
				} qr{Tested value '1' did not match wanted regex '\Q(?^:\s+)\E};
			} else {
				assertExceptionMatches {
					$res = assertMatches( qr{\s+}, 1 )
				} qr{Tested value '1' did not match wanted regex '\Q(?-xism:\s+)\E};
			}
			assertNot( $res );
			undef $res;
		},
	},

	# Test assertRef()
	{
		name => 'AssertRef',
		test => sub {
			my $res;

			assertNoException { $res = assertRef('HASH', {}) };
			assert( $res );
			undef $res;

			assertNoException { $res = assertRef('GLOB', \*STDIN) };
			assert( $res );
			undef $res;

			assertNoException { $res = assertRef('ARRAY', []) };
			assert( $res );
			undef $res;

			assertNoException { $res = assertRef('SCALAR', \"something") };
			assert( $res );
			undef $res;

			assertNoException { $res = assertRef('ClassA', ClassA->new) };
			assert( $res );
			undef $res;

			assertException { $res = assertRef('HASH', 'something') };
			assertMatches( qr{Expected a HASH value, got a scalar}, $@ );
			assertNot( $res );
			undef $res;

			assertException { $res = assertRef('HASH', undef) };
			assertMatches( qr{Expected a HASH value, got a undefined value}, $@ );
			assertNot( $res );
			undef $res;

			assertException { $res = assertRef('HASH', []) };
			assertMatches( qr{Expected a HASH value, got a ARRAY}, $@ );
			assertNot( $res );
			undef $res;

			assertException { $res = assertRef('ClassA', []) };
			assertMatches( qr{Expected a ClassA value, got a ARRAY}, $@ );
			assertNot( $res );
			undef $res;

		},
	},


	# Test assertInstanceOf()
	{
		name => 'AssertInstanceOf',
		test => sub {
			my ( $res, $aInstance, $bInstance, $cInstance );

			$aInstance = ClassA->new;
			$bInstance = ClassB->new;
			$cInstance = ClassC->new;

			# aInstance should be only a ClassA object...
			assertException { assertInstanceOf('ClassB', $aInstance) };
			assertMatches qr{Expected an instance of 'ClassB', got an instance of 'ClassA'}, $@;
			assertException { assertInstanceOf('ClassC', $aInstance) };
			assertMatches qr{Expected an instance of 'ClassC', got an instance of 'ClassA'}, $@;
			assertNoException { assertInstanceOf('ClassA', $aInstance) };

			# bInstance should be only a ClassB object
			assertException { assertInstanceOf('ClassA', $bInstance) };
			assertMatches qr{Expected an instance of 'ClassA', got an instance of 'ClassB'}, $@;
			assertException { assertInstanceOf('ClassC', $bInstance) };
			assertMatches qr{Expected an instance of 'ClassC', got an instance of 'ClassB'}, $@;
			assertNoException { assertInstanceOf('ClassB', $bInstance) };

			# cInstance should be only a ClassC object
			assertException { assertInstanceOf('ClassA', $cInstance) };
			assertMatches qr{Expected an instance of 'ClassA', got an instance of 'ClassC'}, $@;
			assertException { assertInstanceOf('ClassB', $cInstance) };
			assertMatches qr{Expected an instance of 'ClassB', got an instance of 'ClassC'}, $@;
			assertNoException { assertInstanceOf('ClassC', $cInstance) };

			# A simple scalar shouldn't even make the ->isa() test
			assertException { assertInstanceOf('ClassA', "something") };
			assertMatches( qr{Expected an instance of 'ClassA', got a non-object \Q('something')\E}, $@ );

			# Neither should a plain (unblessed) reference
			assertException { assertInstanceOf('ClassA', []) };
			assertMatches( qr{Expected an instance of 'ClassA', got a non-object \('ARRAY\(0x\w+\)'\)}, $@ );

		},
	},

	# Test assertKindOf()
	{
		name => 'AssertKindOf',
		test => sub {
			my ( $res, $aInstance, $bInstance, $cInstance );

			$aInstance = ClassA->new;
			$bInstance = ClassB->new;
			$cInstance = ClassC->new;

			# aInstance should be an ClassA object...
			assertNoException { $res = assertKindOf('ClassA', $aInstance) };
			assert( $res );
			undef $res;

			# bInstance should be both a ClassA and a ClassB object
			assertNoException { $res = assertKindOf('ClassA', $bInstance) };
			assert( $res );
			undef $res;

			assertNoException { $res = assertKindOf('ClassB', $bInstance) };
			assert( $res );
			undef $res;

			# cInstance should be all three
			assertNoException { $res = assertKindOf('ClassA', $cInstance) };
			assert( $res );
			undef $res;

			assertNoException { $res = assertKindOf('ClassB', $cInstance) };
			assert( $res );
			undef $res;

			assertNoException { $res = assertKindOf('ClassC', $cInstance) };
			assert( $res );
			undef $res;

			# But aInstance should be neither a B nor a C
			assertException { $res = assertKindOf('ClassB', $aInstance) };
			assertMatches( qr{Expected an instance of 'ClassB' or a subclass, got an instance of 'ClassA'}, $@ );
			assertNot( $res );
			undef $res;

			assertException { $res = assertKindOf('ClassC', $aInstance) };
			assertMatches( qr{Expected an instance of 'ClassC' or a subclass, got an instance of 'ClassA'}, $@ );
			assertNot( $res );
			undef $res;

			# Neither should bInstance be a C
			assertException { $res = assertKindOf('ClassC', $bInstance) };
			assertMatches( qr{Expected an instance of 'ClassC' or a subclass, got an instance of 'ClassB'}, $@ );
			assertNot( $res );
			undef $res;

			# A simple scalar shouldn't even make the ->isa() test
			assertException { $res = assertKindOf('ClassA', "something") };
			assertMatches( qr{Expected an instance of 'ClassA' or a subclass, got a non-object \Q('something')\E}, $@ );
			assertNot( $res );
			undef $res;

			# Neither should a plain (unblessed) reference
			assertException { $res = assertKindOf('ClassA', []) };
			assertMatches( qr{Expected an instance of 'ClassA' or a subclass, got a non-object \('ARRAY\(0x\w+\)'\)}, $@ );
			assertNot( $res );
			undef $res;

		},
	},
);

Test::SimpleUnit::runTests( @testSuite );