File: 10-tester.t

package info (click to toggle)
libtest-trap-perl 0.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 264 kB
  • ctags: 54
  • sloc: perl: 2,315; makefile: 2
file content (297 lines) | stat: -rw-r--r-- 7,486 bytes parent folder | download | duplicates (6)
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
#!perl -T
# -*- mode: cperl ; compile-command: "cd .. ; ./Build ; prove -vb t/10-*.t" -*-

BEGIN { $_ = defined && /(.*)/ && $1 for @ENV{qw/ TMPDIR TEMP TMP /} } # taint vs tempfile
use Test::Tester;
use Test::More tests => 2 + 3 + 7*15 + 5*3;
use strict;
use warnings;

use Test::Trap qw( trap $T );
use Test::Trap qw( diag_all $T :on_fail(diag_all) );
use Test::Trap qw( diag_all_once $T :on_fail(diag_all_once) );

# Trap with warning and return
my ($prem, @t) = run_tests
  ( sub {
      my $t = trap { warn "A warning"; 5 };
      $T->return_is_deeply( [5], '5 was returned' );
      $T->warn_like( 0, qr/^A warning\b/, 'A warning was given' );
    },
  );
is( $prem, '' );
is( $#t, 1 );
is( $t[0]{ok}, 1, '->return_is_deeply [5]');
is( $t[0]{actual_ok}, 1 );
is( $t[0]{name}, '5 was returned' );
is( $t[0]{diag}, '' );
is( $t[0]{depth}, 1 );
is( $t[1]{ok}, 1, '->warn_like');
is( $t[1]{actual_ok}, 1 );
is( $t[1]{name}, 'A warning was given' );
is( $t[1]{diag}, '' );
is( $t[1]{depth}, 1 );

# Trap with silent exit
($prem, @t) = run_tests
  ( sub {
      my $t = trap { exit };
      $T->return_is_deeply( [5], '5 was returned' );
    },
  );
is( $prem, '' );
is( $#t, 0 );
is( $t[0]{ok}, 0, '->return_is_deeply [5]');
is( $t[0]{actual_ok}, 0 );
is( $t[0]{name}, '5 was returned' );
is( $t[0]{diag}, <<'EOE' );
    Expecting to return(), but instead exit()ed with 0
EOE
is( $t[0]{depth}, 1 );

# Trap with exception and diag_all
($prem, @t) = run_tests
  ( sub {
      my $t = diag_all { die "Argh\n" };
      $T->return_nok(0, 'Return with (first) false value');
      $T->exit_nok(q/Exit with (Perl's idea of a) false value/);
    },
  );
is( $prem, '' );
is( $#t, 1 );
is( $t[0]{ok}, 0, '->return_nok');
is( $t[0]{actual_ok}, 0 );
is( $t[0]{name}, 'Return with (first) false value' );
is( $t[0]{diag}, sprintf <<'EOE', Data::Dump::dump($T) );
    Expecting to return(), but instead die()ed with "Argh\n"
%s
EOE
is( $t[0]{depth}, 1 );
is( $t[1]{ok}, 0, '->exit_nok');
is( $t[1]{actual_ok}, 0 );
is( $t[1]{name}, q/Exit with (Perl's idea of a) false value/ );
is( $t[1]{diag}, sprintf <<'EOE', Data::Dump::dump($T) );
    Expecting to exit(), but instead die()ed with "Argh\n"
%s
EOE
is( $t[1]{depth}, 1 );

# Trap with print, exit, and diag_all
($prem, @t) = run_tests
  ( sub {
      my $t = diag_all { print "Hello world"; exit };
      $T->exit_nok('Exit with false value');
    },
  );
is( $prem, '' );
is( $#t, 0 );
is( $t[0]{ok}, 1, '->exit_nok');
is( $t[0]{actual_ok}, 1 );
is( $t[0]{name}, 'Exit with false value' );
is( $t[0]{diag}, '' );
is( $t[0]{depth}, 1 );

# Capture some TB version dependent stuff:
($prem, @t) = run_tests sub { isnt 5, 5 };
my $diag5isnt5 = $t[0]{diag};

# Trap with print, and exit 5, and diag_all_once
($prem, @t) = run_tests
  ( sub {
      my $t = diag_all_once { print "Hello world"; exit 5 };
      $T->exit_nok('Exit with false value');
      $T->exit_isnt(5, 'Exit with non-5 value');
    },
  );
is( $prem, '' );
is( $#t, 1 );
is( $t[0]{ok}, 0, '->exit_nok');
is( $t[0]{actual_ok}, 0 );
is( $t[0]{name}, 'Exit with false value' );
is( $t[0]{diag}, sprintf <<'EOE', Data::Dump::dump($T) );
    Expecting false value in exit(), but got 5 instead
%s
EOE
is( $t[0]{depth}, 1 );
is( $t[1]{ok}, 0, '->exit_isnt');
is( $t[1]{actual_ok}, 0 );
is( $t[1]{name}, 'Exit with non-5 value' );
is( $t[1]{diag}, "$diag5isnt5(as above)\n" );
is( $t[1]{depth}, 1 );

# Trap with multiple return values and diag_all_once
($prem, @t) = run_tests
  ( sub {
      my ($t) = diag_all_once { return 3..7 };
      $T->return_like( 1, qr/4/, 'return[1] matches /4/' );
    },
  );
is( $prem, '' );
is( $#t, 0 );
is( $t[0]{ok}, 1, '->return_like');
is( $t[0]{actual_ok}, 1 );
is( $t[0]{name}, 'return[1] matches /4/' );
is( $t[0]{diag}, '' );
is( $t[0]{depth}, 1 );

# Quiet trap, with no on-test-failure callback
($prem, @t) = run_tests
  ( sub {
      my ($t) = trap { return 3..7 };
      $T->quiet;
    },
  );
is( $prem, '' );
is( $#t, 0 );
is( $t[0]{ok}, 1, '->quiet');
is( $t[0]{actual_ok}, 1 );
is( $t[0]{name}, '' );
is( $t[0]{diag}, '' );
is( $t[0]{depth}, 1 );

# Warning trap with diag_all_once
($prem, @t) = run_tests
  ( sub {
      my ($t) = diag_all_once { warn "Hello!\n" };
      $T->quiet('In denial about STDERR');
    },
  );
is( $prem, '' );
is( $#t, 0 );
is( $t[0]{ok}, 0, '->quiet');
is( $t[0]{actual_ok}, 0 );
is( $t[0]{name}, 'In denial about STDERR' );
is( $t[0]{diag}, sprintf <<'EOE', Data::Dump::dump($T) );
Expecting no STDERR, but got "Hello!\n"
%s
EOE
is( $t[0]{depth}, 1 );

# Printing trap with no on-test-failure callback
($prem, @t) = run_tests
  ( sub {
      my ($t) = trap { print "Hello!\n" };
      $T->quiet('In denial about STDOUT');
    },
  );
is( $prem, '' );
is( $#t, 0 );
is( $t[0]{ok}, 0, '->quiet');
is( $t[0]{actual_ok}, 0 );
is( $t[0]{name}, 'In denial about STDOUT' );
is( $t[0]{diag}, <<'EOE' );
Expecting no STDOUT, but got "Hello!\n"
EOE
is( $t[0]{depth}, 1 );

# Noisy trap
($prem, @t) = run_tests
  ( sub {
      my ($t) = trap { warn "world!\n"; print "Hello!\n" };
      $T->quiet('In denial about noise!');
    },
  );
is( $prem, '' );
is( $#t, 0 );
is( $t[0]{ok}, 0, '->quiet');
is( $t[0]{actual_ok}, 0 );
is( $t[0]{name}, 'In denial about noise!' );
is( $t[0]{diag}, <<'EOE' );
Expecting no STDOUT, but got "Hello!\n"
Expecting no STDERR, but got "world!\n"
EOE
is( $t[0]{depth}, 1 );

# Noisy trap
($prem, @t) = run_tests
  ( sub {
      my ($t) = trap { warn "world!\n"; print "Hello!\n" };
      $T->did_return('Should return');
    },
  );
is( $prem, '' );
is( $#t, 0 );
is( $t[0]{ok}, 1, '->did_return');
is( $t[0]{actual_ok}, 1 );
is( $t[0]{name}, 'Should return' );
is( $t[0]{diag}, '' );
is( $t[0]{depth}, 1 );

# Exiting trap
($prem, @t) = run_tests
  ( sub {
      my ($t) = trap { exit };
      $T->did_exit('Should exit');
    },
  );
is( $prem, '' );
is( $#t, 0 );
is( $t[0]{ok}, 1, '->did_exit');
is( $t[0]{actual_ok}, 1 );
is( $t[0]{name}, 'Should exit' );
is( $t[0]{diag}, '' );
is( $t[0]{depth}, 1 );

# Exiting trap
($prem, @t) = run_tests
  ( sub {
      my ($t) = trap { exit };
      $T->did_die('In denial about death');
    },
  );
is( $prem, '' );
is( $#t, 0 );
is( $t[0]{ok}, 0, '->did_die');
is( $t[0]{actual_ok}, 0 );
is( $t[0]{name}, 'In denial about death' );
is( $t[0]{diag}, <<'EOE' );
    Expecting to die(), but instead exit()ed with 0
EOE
is( $t[0]{depth}, 1 );

# Exiting TODO trap
($prem, @t) = run_tests
  ( sub {
    TODO: {
	local $TODO = 'Testing TODOs';
	my ($t) = trap { exit };
	$T->did_die('In denial about death');
      }
    },
  );
is( $prem, '' );
is( $#t, 0 );
is( $t[0]{ok}, 1, '->did_die, TODO');
is( $t[0]{actual_ok}, 0 );
is( $t[0]{name}, 'In denial about death' );
is( $t[0]{diag}, <<'EOE' );
    Expecting to die(), but instead exit()ed with 0
EOE
is( $t[0]{depth}, 1 );
# extra 2:
is( $t[0]{type}, 'todo', 'type = todo' );
is( $t[0]{reason}, 'Testing TODOs', 'reason' );

my $really_skipped = 1;
# Exiting SKIPPED trap
($prem, @t) = run_tests
  ( sub {
    SKIP: {
	skip 'Testing SKIP', 1;
	undef $really_skipped;
	my ($t) = trap { exit };
	$T->did_die('In denial about death');
      }
    },
  );
is( $prem, '' );
is( $#t, 0 );
is( $t[0]{ok}, 1, '->did_die, SKIPPED');
is( $t[0]{actual_ok}, 1 );
is( $t[0]{name}, '' );
is( $t[0]{diag}, '' );
is( $t[0]{depth}, 1 );
# extra 3:
is( $t[0]{type}, 'skip', 'type = skip' );
is( $t[0]{reason}, 'Testing SKIP', 'reason' );
is( $really_skipped, 1, 'Asserting that SKIPPED code has not been run');