File: Test-Assertions.t

package info (click to toggle)
libtest-assertions-perl 1.054-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 148 kB
  • ctags: 31
  • sloc: perl: 562; makefile: 15
file content (232 lines) | stat: -rw-r--r-- 6,871 bytes parent folder | download | duplicates (4)
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
#!/usr/local/bin/perl

#
# Unit test for Test::Assertions
# $Id: Test-Assertions.t,v 1.21 2006/01/20 12:22:12 tims Exp $
#
# Options:
# -s : save output files
#

use strict;
use lib qw(./lib ../lib);

use Test::Assertions qw(test);
use Getopt::Std;
use vars qw($opt_s);

#Options
getopts("s");

#Test data
my $lhs = {0=>1, b=>2, c=>3};
my $rhs = {};
$rhs->{c}=3;
$rhs->{b}=2;
$rhs->{0}=1;

#Files generated by this test
my $file1 = 'Test-Assertions.1';
my $file2 = 'Test-Assertions.2';
my $file3 = 'Test-Assertions_child_1.pl';

#Ensure any preserved output files are cleaned away
unlink($file1, $file2, $file3);
die("Unable to clean up output files") if(-e $file1 || -e $file2 || -e $file3);

#Tests
plan tests => 57;
chdir('t') if -d 't';

ASSERT(1, 'compiled');

#
# Test/ok mode
#

import Test::Assertions qw(test/ok);
ok(1);

#
# DIED function
#

ASSERT(DIED(sub  {die()} ), 'die() is detected');

#
# Assess functions
#
ASSERT(ASSESS_FILE("perl fails.pl") =~ /not ok/, 'a failing script is seen as failing');
ASSERT(ASSESS(["not ok"]) =~ /not ok/, 'check that "not ok" is assessed ok');
ASSERT(ASSESS(["ok"]) !~ /not ok/, 'check that "ok" is assessed ok');
ASSERT(ASSESS(["1..3","ok","ok"]) =~ /not ok/, 'check that wrong number of tests is not ok');
my @list = ASSESS(["not ok"], "assess in list context");
ASSERT(!$list[0], $list[1]);
@list = ASSESS(["ok"], "assess in list context"), 
ASSERT($list[0], $list[1]);

#
# Test the EQUAL function
#

ASSERT( EQUAL($lhs, $rhs), 'deep comparisons' );
ASSERT( EQUAL(15, 0x0F), 'scalars' );
ASSERT( EQUAL('hello', 'hello'), 'scalars' );
ASSERT( ! EQUAL('hello', 'world'), 'scalars' );

ASSERT( EQUAL([1, 3, 'e', 't'], [1, 3, 'e', 't']), 'array refs' );
ASSERT( ! EQUAL([1, 3, 'e', 't'], [3, 1, 'e', 't']), 'array refs' );

ASSERT( EQUAL(
{
	hello => 'world',
	234 => 'whoo!!',
	'blah blah' => '',
},
{
	hello => 'world',
	'blah blah' => '',
	234 => 'whoo!!',
}), 'hash refs' );

ASSERT( ! EQUAL(
{
	hello => 'world',
	234 => 'whoo!!',
	'blah blah' => '',
},
{
	hello => 'world',
	234 => 'whoo!!',
}), 'hash refs' );

#
# FILES_EQUAL
#

ASSERT( ! FILES_EQUAL($file1, $file2), 'FILES_EQUAL works on nonexistent files');

WRITE_FILE($file1, '');
WRITE_FILE($file2, '');
ASSERT( -e $file1, 'file written');
ASSERT( -e $file2, 'file written');
ASSERT( FILES_EQUAL($file1, $file2), 'FILES_EQUAL works on zero-sized files');

WRITE_FILE($file1, 'hello');
WRITE_FILE($file2, 'world');
ASSERT( ! FILES_EQUAL($file1, $file2), 'FILES_EQUAL works on nonzero-sized files');

WRITE_FILE($file1, 'hello');
WRITE_FILE($file2, 'hello');
ASSERT( FILES_EQUAL($file1, $file2), 'FILES_EQUAL works on nonzero-sized files');

#
# EQUALS_FILE
#

unlink($file1, $file2);
WRITE_FILE($file1, '');
ASSERT( EQUALS_FILE('', $file1), 'EQUALS_FILE works on zero-sized files');

WRITE_FILE($file1, 'hello');
ASSERT( ! EQUALS_FILE('world', $file1), 'EQUALS_FILE works on nonzero-sized files');

WRITE_FILE($file1, 'hello');
ASSERT( EQUALS_FILE('hello', $file1), 'EQUALS_FILE works on nonzero-sized files');

#
# MATCHES_FILE
#

unlink($file1, $file2);
WRITE_FILE($file1, '');
ASSERT( MATCHES_FILE('', $file1), 'MATCHES_FILE works on zero-sized files');

WRITE_FILE($file1, 'Y\wZ');
ASSERT( ! MATCHES_FILE('LHR', $file1), 'MATCHES_FILE works on nonzero-sized files');

WRITE_FILE($file1, 'Y\wZ');
ASSERT( ! MATCHES_FILE('Callsign YYZ OK', $file1), 'MATCHES_FILE works on nonzero-sized files');
ASSERT( MATCHES_FILE('YYZ', $file1), 'MATCHES_FILE works on nonzero-sized files');

#
# READ_FILE and WRITE_FILE
#

WRITE_FILE($file3, 'use strict;use lib qw(./lib ../lib);
use Test::Assertions qw(test);
plan tests => 2;
ASSERT(1,"OK");ASSERT(1,"OK");');
system("$^X $file3 > $file1 2> $file2");
ASSERT( scalar(READ_FILE($file1) =~ m/1\.\.2.*ok 1.*ok 2/s), "child process writes to $file1");
ASSERT( ! -s $file2, "child process writes nothing to $file2");


WRITE_FILE($file3, 'use strict;use lib qw(./lib ../lib);
use Test::Assertions qw(test);
plan tests => 2;
ASSERT(1);');
system("$^X $file3 > $file1 2> $file2");
ASSERT( scalar(READ_FILE($file1) =~ m/1\.\.2.*ok 1/s), "child process writes to $file1");
ASSERT( scalar(READ_FILE($file2) =~ m/# Looks like.*2.*1/s), "child process writes to $file2");

# plan tests with a chdir
WRITE_FILE($file3, 'use strict;use lib qw(./lib ../lib);
use Test::Assertions qw(test);
chdir("..");
plan tests;
#ASSERT(0)
ASSERT(1);');
system("$^X $file3 > $file1 2> $file2");
ASSERT( scalar(READ_FILE($file1) =~ m/1\.\.1.*ok 1/s), "child process writes to $file1");
ASSERT( length(READ_FILE($file2)) == 0, "child process writes nothing to $file2");
WRITE_FILE($file3, 'use strict;use lib qw(./lib ../lib);
use Test::Assertions qw(warn);
ASSERT(1,"OK");');
system("$^X $file3 > $file1 2> $file2");
ASSERT( ! -s $file1, "child process writes nothing to $file1");
ASSERT( ! -s $file2, "child process writes nothing to $file2");

my $rv = WRITE_FILE($file1, 'hello world 123');
ASSERT($rv == 1, 'file was written');
ASSERT((-e $file1), 'file was written');
ASSERT( WRITE_FILE($file1, 'hello world 123'), 'file was written');

$rv = READ_FILE($file1);
ASSERT($rv eq 'hello world 123', 'file was read OK');
ASSERT( READ_FILE($file1), 'file was read OK' );
ASSERT( READ_FILE($file1) eq 'hello world 123', 'file was read OK' );

$rv = READ_FILE('nonexistent.YYZ');
chomp($@);
ASSERT(! defined $rv, "file was not read: $@");

#
# Different styles
# 

$rv = system("$^X Test-Assertion_style.pl die > $file1 2> $file2");
ASSERT($rv != 0, "child exited not OK");
ASSERT( scalar(READ_FILE($file1) eq "1\.\.1\n"), "child process writes to $file1");
ASSERT( scalar(READ_FILE($file2) =~ m/Assertion failure at line 100 in.*deliberatefail\)\s*$/s), "child process writes to $file2");

$rv = system("$^X Test-Assertion_style.pl warn > $file1 2> $file2");
ASSERT($rv == 0, "child exited OK");
ASSERT( scalar(READ_FILE($file1) eq "1\.\.1\n"), "child process writes to $file1");
ASSERT( scalar(READ_FILE($file2) =~ m/Assertion failure at line 100 in.*deliberatefail\)\s*$/s), "child process writes to $file2");

$rv = system("$^X Test-Assertion_style.pl confess > $file1 2> $file2");
ASSERT($rv != 0, "child exited not OK");
ASSERT( scalar(READ_FILE($file1) eq "1\.\.1\n"), "child process writes to $file1");
ASSERT( scalar(READ_FILE($file2) =~ m/Assertion failure at line 9 in.*deliberatefail.*ASSERT_confess.*called at.*main::to.*called at.*main::go.*called at/s), "child process writes to $file2");

$rv = system("$^X Test-Assertion_style.pl cluck > $file1 2> $file2");
ASSERT($rv == 0, "child exited OK");
ASSERT( scalar(READ_FILE($file1) eq "1\.\.1\n"), "child process writes to $file1");
ASSERT( scalar(READ_FILE($file2) =~ m/Assertion failure at line 9 in.*deliberatefail.*ASSERT_cluck.*called at.*main::to.*called at.*main::go.*called at/s), "child process writes to $file2");

#
# Clean up
#

unlink($file1, $file2, $file3) unless($opt_s);