line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Assert::Refute; |
2
|
|
|
|
|
|
|
|
3
|
34
|
|
|
34
|
|
1656419
|
use 5.006; |
|
34
|
|
|
|
|
358
|
|
4
|
34
|
|
|
34
|
|
200
|
use strict; |
|
34
|
|
|
|
|
71
|
|
|
34
|
|
|
|
|
861
|
|
5
|
34
|
|
|
34
|
|
171
|
use warnings; |
|
34
|
|
|
|
|
70
|
|
|
34
|
|
|
|
|
2324
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.16'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Assert::Refute - Unified testing and assertion tool |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 DESCRIPTION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
This module allows injecting L-like code snippets |
15
|
|
|
|
|
|
|
into production code, without turning the whole application |
16
|
|
|
|
|
|
|
into a giant testing script. |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
This can be though of as a lightweight design-by-contract form. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
The following code will die unless the conditions listed there are fulfilled: |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use Assert::Refute ":all", { on_fail => 'croak' }; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# Lots of code here |
27
|
|
|
|
|
|
|
try_refute { |
28
|
|
|
|
|
|
|
cmp_ok $price + $fee, "==", $total, "Money added up correctly"; |
29
|
|
|
|
|
|
|
like $description, qr/\w{3}/, "A readable description is present"; |
30
|
|
|
|
|
|
|
isa_ok $my_obj, "My::Class"; |
31
|
|
|
|
|
|
|
}; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
A code snippet like this can guard important invariants, |
34
|
|
|
|
|
|
|
ensure data correctness, or serve as a safety net while reworking |
35
|
|
|
|
|
|
|
a monolithic application into separate testable modules. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Note that the inside of the block can be copied into a unit-test as is, |
38
|
|
|
|
|
|
|
giving one a fine-grained I----E accuracy> control. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
The same can be done without polluting the main package namespace: |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
use Assert::Refute { on_fail => 'croak' }; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
try_refute { |
45
|
|
|
|
|
|
|
my $report = shift; |
46
|
|
|
|
|
|
|
$report->cmp_ok( $price + $fee, "==", $total, "Money added up correctly" ); |
47
|
|
|
|
|
|
|
$report->like( $description, qr/\w{3}/, "A readable description is present" ); |
48
|
|
|
|
|
|
|
$report->isa_ok( $my_obj, "My::Class" ); |
49
|
|
|
|
|
|
|
}; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Relying on a global (in fact, per-package) callback is not required: |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
use Assert::Refute {}, ":all"; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $report = try_refute { |
56
|
|
|
|
|
|
|
# ... assertions here |
57
|
|
|
|
|
|
|
}; |
58
|
|
|
|
|
|
|
if (!$report->is_passing) { |
59
|
|
|
|
|
|
|
$my_logger->error( "Something is not right: ".$report->get_tap ); |
60
|
|
|
|
|
|
|
# do whatever error handling is needed |
61
|
|
|
|
|
|
|
}; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
See L for more information about the underlying |
64
|
|
|
|
|
|
|
object-oriented interface. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 ASSERTIONS, CONTRACTS, AND SUBCONTRACTS |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=over |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=item * We use the term I here to refer to a binary statement |
71
|
|
|
|
|
|
|
that can be proven wrong using a well-defined, finite calculation. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
We say that assertion Is if such proof is provided, |
74
|
|
|
|
|
|
|
and Ies otherwise. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
"X equals Y" and "a string contains such and such words" |
77
|
|
|
|
|
|
|
are assertions by this definition. |
78
|
|
|
|
|
|
|
"This code terminates" isn't because it requires solving the halting problem. |
79
|
|
|
|
|
|
|
"All swans are white" isn't either unless there's code that produces |
80
|
|
|
|
|
|
|
a black swan. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=item * We use the term I here to refer to a code block |
83
|
|
|
|
|
|
|
containing zero or more assertions. |
84
|
|
|
|
|
|
|
A contract is said to I if any of its assertions fails, |
85
|
|
|
|
|
|
|
and is assumed to I otherwise. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
This is not to be confused with full-fledged design-by-contract |
88
|
|
|
|
|
|
|
which is much more specific about what contracts are. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=item * Note that a contract itself is an assertion by this definition. |
91
|
|
|
|
|
|
|
We use the term I to refer to an assertion that another |
92
|
|
|
|
|
|
|
contract passes given certain arguments. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
These building blocks allow to create and verify |
95
|
|
|
|
|
|
|
arbitrarily complex specifications. |
96
|
|
|
|
|
|
|
See L below for limitations, though. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=back |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 EXPORT |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Any number of hash references may be added to the C |
103
|
|
|
|
|
|
|
resulting in an implicit Cconfigure> call. |
104
|
|
|
|
|
|
|
A literal C<{}> will also trigger C. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Everything else will be passed on to L. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
use Assert::Refute; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
as well as |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
use Assert::Refute qw(:core); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
would only export C, C, C, |
115
|
|
|
|
|
|
|
C, C, and C functions. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Also for convenience some basic assertions mirroring the L suite |
118
|
|
|
|
|
|
|
are exportable via C<:all> and C<:basic> export tag. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
use Assert::Refute qw(:all); |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
would also export the following assertions: |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
C, C, C, C, C, C, |
125
|
|
|
|
|
|
|
C, C, C, C, C, |
126
|
|
|
|
|
|
|
C, C, C, C, C. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
See L for more. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
This distribution also bundles some extra assertions: |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=over |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item * L - inspect list structure; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item * L - verify exceptions and warnings; |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item * L - inspect hash keys and values; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item * L - make sure numbers fit certain intervals; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=back |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
These need to be C |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=cut |
147
|
|
|
|
|
|
|
|
148
|
34
|
|
|
34
|
|
275
|
use Carp; |
|
34
|
|
|
|
|
92
|
|
|
34
|
|
|
|
|
2211
|
|
149
|
34
|
|
|
34
|
|
209
|
use Exporter; |
|
34
|
|
|
|
|
69
|
|
|
34
|
|
|
|
|
1346
|
|
150
|
|
|
|
|
|
|
|
151
|
34
|
|
|
34
|
|
14833
|
use Assert::Refute::Report; |
|
34
|
|
|
|
|
104
|
|
|
34
|
|
|
|
|
1167
|
|
152
|
34
|
|
|
34
|
|
229
|
use Assert::Refute::Build qw(current_contract); |
|
34
|
|
|
|
|
65
|
|
|
34
|
|
|
|
|
1552
|
|
153
|
34
|
|
|
34
|
|
215
|
use Assert::Refute::T::Basic; |
|
34
|
|
|
|
|
63
|
|
|
34
|
|
|
|
|
57132
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
my @basic = ( |
156
|
|
|
|
|
|
|
@Assert::Refute::T::Basic::EXPORT, 'plan' |
157
|
|
|
|
|
|
|
); |
158
|
|
|
|
|
|
|
my @core = qw( |
159
|
|
|
|
|
|
|
contract refute_these try_refute |
160
|
|
|
|
|
|
|
refute subcontract contract_is current_contract |
161
|
|
|
|
|
|
|
); |
162
|
|
|
|
|
|
|
my @extra = qw( refute_and_report assert_refute ); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
165
|
|
|
|
|
|
|
our @EXPORT = @core; |
166
|
|
|
|
|
|
|
our @EXPORT_OK = (@basic, @extra); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
169
|
|
|
|
|
|
|
basic => [@basic], |
170
|
|
|
|
|
|
|
core => [@core, @extra], |
171
|
|
|
|
|
|
|
all => [@EXPORT, @EXPORT_OK], |
172
|
|
|
|
|
|
|
); |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
our $DRIVER; # Used by other modules, declaration JFYI |
175
|
|
|
|
|
|
|
our %CALLER_CONF; |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
our $NDEBUG; |
178
|
|
|
|
|
|
|
$NDEBUG = $ENV{PERL_NDEBUG} unless defined $NDEBUG; |
179
|
|
|
|
|
|
|
$NDEBUG = $ENV{NDEBUG} unless defined $NDEBUG; |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
sub import { |
182
|
41
|
|
|
41
|
|
128572
|
my $class = shift; |
183
|
41
|
|
|
|
|
96
|
my (%conf, @exp, $need_conf); |
184
|
41
|
|
|
|
|
108
|
foreach (@_) { |
185
|
35
|
100
|
33
|
|
|
276
|
if (ref $_ eq 'HASH') { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
186
|
8
|
|
|
|
|
32
|
%conf = (%conf, %$_); |
187
|
8
|
|
|
|
|
22
|
$need_conf++; |
188
|
|
|
|
|
|
|
} elsif (!ref $_ and $_ eq '{}') { |
189
|
|
|
|
|
|
|
# TODO 0.15 remove together with auto-carp |
190
|
0
|
|
|
|
|
0
|
$need_conf++; # allow for -MAssert::Refute={} |
191
|
|
|
|
|
|
|
} elsif (!ref $_) { |
192
|
27
|
|
|
|
|
82
|
push @exp, $_; |
193
|
|
|
|
|
|
|
} else { |
194
|
0
|
|
|
|
|
0
|
croak "Unexpected argument in Assert::Refute->import: ".ref $_; |
195
|
|
|
|
|
|
|
}; |
196
|
|
|
|
|
|
|
}; |
197
|
|
|
|
|
|
|
|
198
|
41
|
100
|
|
|
|
162
|
$class->configure( \%conf, scalar caller ) if $need_conf; |
199
|
41
|
|
|
|
|
39863
|
$class->export_to_level(1, undef, @exp); |
200
|
|
|
|
|
|
|
}; |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
my %known_callback = ( |
203
|
|
|
|
|
|
|
skip => '', |
204
|
|
|
|
|
|
|
carp => sub { |
205
|
|
|
|
|
|
|
my $report = shift; |
206
|
|
|
|
|
|
|
carp $report->get_tap |
207
|
|
|
|
|
|
|
.($report->is_passing ? "Contract passed" : "Contract failed"); |
208
|
|
|
|
|
|
|
}, |
209
|
|
|
|
|
|
|
croak => sub { |
210
|
|
|
|
|
|
|
my $report = shift; |
211
|
|
|
|
|
|
|
croak $report->get_tap |
212
|
|
|
|
|
|
|
.($report->is_passing ? "Contract passed" : "Contract failed"); |
213
|
|
|
|
|
|
|
}, |
214
|
|
|
|
|
|
|
); |
215
|
|
|
|
|
|
|
my %default_conf = ( |
216
|
|
|
|
|
|
|
on_fail => 'skip', |
217
|
|
|
|
|
|
|
on_pass => 'skip', |
218
|
|
|
|
|
|
|
); |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=head2 try_refute { ... } |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Check whether given contract BLOCK containing zero or more assertions passes. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
Contract will fail if any of the assertions fails, |
225
|
|
|
|
|
|
|
a C is declared and not fulfilled, |
226
|
|
|
|
|
|
|
or an exception is thrown. |
227
|
|
|
|
|
|
|
Otherwise it is assumed to pass. |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
The BLOCK must accept one argument, the contract execution report, |
230
|
|
|
|
|
|
|
likely a L instance. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
More arguments MAY be added in the future. |
233
|
|
|
|
|
|
|
Return value is ignored. |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
A read-only report instance is returned by C instead. |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
If C/C callbacks were specified during C |
238
|
|
|
|
|
|
|
using C, they will also be executed if appropriate. |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
If C or C environment variable is set at compile time, |
241
|
|
|
|
|
|
|
this block is replaced with a stub |
242
|
|
|
|
|
|
|
which returns an unconditionally passing report. |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
This is basically what one expects from a module in C namespace. |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=cut |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
sub try_refute(&;@) { ## no critic # need prototype |
249
|
18
|
|
|
18
|
1
|
334
|
my ( $block, @arg ) = @_; |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
# Should a missing config even happen? Ok, play defensively... |
252
|
18
|
|
|
|
|
55
|
my $conf = $CALLER_CONF{+caller}; |
253
|
18
|
50
|
|
|
|
46
|
if( !$conf ) { |
254
|
0
|
|
|
|
|
0
|
$conf = __PACKAGE__->configure( { on_fail => 'carp' }, scalar caller ); |
255
|
|
|
|
|
|
|
}; |
256
|
18
|
100
|
|
|
|
52
|
return $conf->{skip_all} if exists $conf->{skip_all}; |
257
|
|
|
|
|
|
|
|
258
|
17
|
|
|
|
|
75
|
my $report = $conf->{driver}->new; |
259
|
|
|
|
|
|
|
eval { |
260
|
17
|
|
|
|
|
60
|
$report->do_run($block); |
261
|
16
|
|
|
|
|
48
|
1; |
262
|
17
|
100
|
|
|
|
29
|
} || do { |
263
|
1
|
|
33
|
|
|
14
|
$report->done_testing( |
264
|
|
|
|
|
|
|
$@ || Carp::shortmess( 'Contract execution interrupted' ) ); |
265
|
|
|
|
|
|
|
}; |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
# perform whatever action is needed |
268
|
17
|
100
|
|
|
|
52
|
my $callback = $conf->{ $report->is_passing ? "on_pass" : "on_fail" }; |
269
|
17
|
100
|
|
|
|
52
|
$callback->($report) if $callback; |
270
|
|
|
|
|
|
|
|
271
|
15
|
|
|
|
|
74
|
return $report; |
272
|
|
|
|
|
|
|
}; |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
=head2 assert_refute { ... } |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
Check whether given contract BLOCK containing zero or more assertions passes. |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
Contract will fail if any of the assertions fail |
279
|
|
|
|
|
|
|
or a C is declared and not fulfilled. |
280
|
|
|
|
|
|
|
Otherwise it is assumed to pass. |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
Unlike with try_refute, exceptions are just let through. |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
The BLOCK must accept one argument, the contract execution report, |
285
|
|
|
|
|
|
|
likely a L instance. |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
More arguments MAY be added in the future. |
288
|
|
|
|
|
|
|
Return value is ignored. |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
A read-only report instance is returned by C instead. |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
If C/C callbacks were specified during C |
293
|
|
|
|
|
|
|
using C, they will also be executed if appropriate. |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
If C or C environment variable is set at compile time, |
296
|
|
|
|
|
|
|
this block is replaced with a stub |
297
|
|
|
|
|
|
|
which returns an unconditionally passing report. |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
This is basically what one expects from a module in C namespace. |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
B<[EXPERIMENTAL]>. Name and behavior MAY change in the future. |
302
|
|
|
|
|
|
|
Should this function prove useful, it will become the successor |
303
|
|
|
|
|
|
|
or C. |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=cut |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
sub assert_refute(&;@) { ## no critic # need prototype |
308
|
2
|
|
|
2
|
1
|
4145
|
my ( $block, @arg ) = @_; |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
# Should a missing config even happen? Ok, play defensively... |
311
|
2
|
|
|
|
|
6
|
my $conf = $CALLER_CONF{+caller}; |
312
|
2
|
50
|
|
|
|
8
|
if( !$conf ) { |
313
|
|
|
|
|
|
|
# TODO add configure_global & use default configuration |
314
|
0
|
|
|
|
|
0
|
$conf = __PACKAGE__->configure( { on_fail => 'carp' }, scalar caller ); |
315
|
|
|
|
|
|
|
}; |
316
|
2
|
50
|
|
|
|
7
|
return $conf->{skip_all} if exists $conf->{skip_all}; |
317
|
|
|
|
|
|
|
|
318
|
2
|
|
|
|
|
12
|
my $report = $conf->{driver}->new->do_run($block); |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
# perform whatever action is needed |
321
|
2
|
100
|
|
|
|
7
|
my $callback = $conf->{ $report->is_passing ? "on_pass" : "on_fail" }; |
322
|
2
|
50
|
|
|
|
12
|
$callback->($report) if $callback; |
323
|
|
|
|
|
|
|
|
324
|
2
|
|
|
|
|
10
|
return $report; |
325
|
|
|
|
|
|
|
}; |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
=head2 refute_and_report { ... } |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
Run a block of code with a fresh L object as argument. |
330
|
|
|
|
|
|
|
Lock the report afterwards and return it. |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
For instance, |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
my $report = refute_and_report { |
335
|
|
|
|
|
|
|
my $c = shift; |
336
|
|
|
|
|
|
|
$c->is( $price * $amount, $total, "Numbers add up" ); |
337
|
|
|
|
|
|
|
$c->like( $header, qr//, "Header as expected" ); |
338
|
|
|
|
|
|
|
$c->can_ok( $duck, "quack" ); |
339
|
|
|
|
|
|
|
}; |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
Or alternatively one may resort to L-like DSL: |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
use Assert::Refute qw(:all); |
344
|
|
|
|
|
|
|
my $report = refute_and_report { |
345
|
|
|
|
|
|
|
is $price * $amount, $total, "Numbers add up"; |
346
|
|
|
|
|
|
|
like $header, qr//, "Header as expected"; |
347
|
|
|
|
|
|
|
can_ok $duck, "quack"; |
348
|
|
|
|
|
|
|
}; |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
This method does not adhere C, apply callbacks, or handle expections. |
351
|
|
|
|
|
|
|
It just executes the checks. |
352
|
|
|
|
|
|
|
Not exported by default. |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
B<[EXPERIMENTAL]>. Name and behavior MAY change in the future. |
355
|
|
|
|
|
|
|
Should this function prove useful, it will become the successor |
356
|
|
|
|
|
|
|
or C. |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
=cut |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
sub refute_and_report (&;@) { ## no critic # need prototype |
361
|
27
|
|
|
27
|
1
|
1103
|
my ( $block, @arg ) = @_; |
362
|
|
|
|
|
|
|
|
363
|
27
|
|
|
|
|
160
|
return Assert::Refute::Report->new->do_run($block); |
364
|
|
|
|
|
|
|
}; |
365
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
=head2 contract { ... } |
367
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
Save a contract BLOCK for future use: |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
my $contract = contract { |
371
|
|
|
|
|
|
|
my ($foo, $bar) = @_; |
372
|
|
|
|
|
|
|
# conditions here |
373
|
|
|
|
|
|
|
}; |
374
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
# much later |
376
|
|
|
|
|
|
|
my $report = $contract->apply( $real_foo, $real_bar ); |
377
|
|
|
|
|
|
|
# Returns an Assert::Refute::Report with conditions applied |
378
|
|
|
|
|
|
|
|
379
|
|
|
|
|
|
|
This is similar to how C / C works in L. |
380
|
|
|
|
|
|
|
|
381
|
|
|
|
|
|
|
B<[DEPRECATED]> This function will disappear in v.0.20. |
382
|
|
|
|
|
|
|
|
383
|
|
|
|
|
|
|
Prior to advent of C, this call used to be the main entry point |
384
|
|
|
|
|
|
|
to this module. |
385
|
|
|
|
|
|
|
This is no more the case, and a simple subroutine containing assertions |
386
|
|
|
|
|
|
|
would fit in most places where C is appropriate. |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
Use L instead. |
389
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
=cut |
391
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
sub contract (&@) { ## no critic |
393
|
1
|
|
|
1
|
1
|
232
|
carp "contract{ ... } is DEPRECATED, use Assert::Refute::Contract::contract instead"; |
394
|
|
|
|
|
|
|
|
395
|
1
|
|
|
|
|
9
|
require Assert::Refute::Contract; |
396
|
1
|
|
|
|
|
7
|
goto &Assert::Refute::Contract::contract; |
397
|
|
|
|
|
|
|
}; |
398
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
=head2 plan tests => $n |
400
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
Plan to run exactly C assertions within a contract block. |
402
|
|
|
|
|
|
|
Plan is optional, contract blocks can run fine without a plan. |
403
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
A contract will fail unconditionally if plan is present and is not fulfilled. |
405
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
C may only be called before executing any assertions. |
407
|
|
|
|
|
|
|
C dies if called outside a contract block. |
408
|
|
|
|
|
|
|
|
409
|
|
|
|
|
|
|
Not exported by default to avoid namespace pollution. |
410
|
|
|
|
|
|
|
|
411
|
|
|
|
|
|
|
=head2 plan skip_all => $reason |
412
|
|
|
|
|
|
|
|
413
|
|
|
|
|
|
|
B<[EXPERIMENTAL]>. |
414
|
|
|
|
|
|
|
Like above, but plan is assumed to be zero and a reason for that is specified. |
415
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
Note that the contract block is not interrupted, |
417
|
|
|
|
|
|
|
it's up to the user to call return. |
418
|
|
|
|
|
|
|
This MAY change in the future. |
419
|
|
|
|
|
|
|
|
420
|
|
|
|
|
|
|
=cut |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
sub plan(@) { ## no critic |
423
|
2
|
|
|
2
|
1
|
12
|
current_contract->plan( @_ ); |
424
|
|
|
|
|
|
|
}; |
425
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
=head2 refute( $reason, $message ) |
427
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
Verify (or, rather, try hard to disprove) |
429
|
|
|
|
|
|
|
an assertion in scope of the current contract. |
430
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
The test passes if the C<$reason> is I, i.e. an empty string, C<0>, |
432
|
|
|
|
|
|
|
or C. |
433
|
|
|
|
|
|
|
Otherwise the C<$reason> is assumed to be a description of what went wrong. |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
You can think of it as C and C from L combined: |
436
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
ok !$reason, $message |
438
|
|
|
|
|
|
|
or diag $reason; |
439
|
|
|
|
|
|
|
|
440
|
|
|
|
|
|
|
As a special case, a literal C<1> is considered to be a boolean value |
441
|
|
|
|
|
|
|
and the assertions just fails, without further explanation. |
442
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
As another special case, an C<\@arrayref> reason |
444
|
|
|
|
|
|
|
will be unfolded into multiple C lines, for instance |
445
|
|
|
|
|
|
|
|
446
|
|
|
|
|
|
|
refute [ $answer, "isn't", 42 ], "life, universe, and everything"; |
447
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
will output 3 diag lines. |
449
|
|
|
|
|
|
|
|
450
|
|
|
|
|
|
|
Returns true for a passing assertion and false for a failing one. |
451
|
|
|
|
|
|
|
Dies if no contract is being executed at the time. |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
=cut |
454
|
|
|
|
|
|
|
|
455
|
|
|
|
|
|
|
sub refute ($$) { ## no critic |
456
|
38
|
|
|
38
|
1
|
373
|
current_contract()->refute(@_); |
457
|
|
|
|
|
|
|
}; |
458
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
=head2 subcontract( "Message" => $contract, @arguments ) |
460
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
"The specified contract passes, given the arguments" assertion. |
462
|
|
|
|
|
|
|
This is similar to C in L. |
463
|
|
|
|
|
|
|
|
464
|
|
|
|
|
|
|
B<[NOTE]> that the message comes first, unlike in C |
465
|
|
|
|
|
|
|
or other assertion types, and is I. |
466
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
A I may be an L object, |
468
|
|
|
|
|
|
|
a plain subroutine with some assertions inside, or |
469
|
|
|
|
|
|
|
an L instance from a previous contract run. |
470
|
|
|
|
|
|
|
|
471
|
|
|
|
|
|
|
A subroutine MUST accept an empty L object. |
472
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
For instance, one could apply a previously defined validation to a |
474
|
|
|
|
|
|
|
structure member: |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
my $valid_email = contract { |
477
|
|
|
|
|
|
|
my $email = shift; |
478
|
|
|
|
|
|
|
# ... define your checks here |
479
|
|
|
|
|
|
|
}; |
480
|
|
|
|
|
|
|
|
481
|
|
|
|
|
|
|
my $valid_user = contract { |
482
|
|
|
|
|
|
|
my $user = shift; |
483
|
|
|
|
|
|
|
is ref $user, 'HASH' |
484
|
|
|
|
|
|
|
or die "Bail out - not a hash"; |
485
|
|
|
|
|
|
|
like $user->{id}, qr/^\d+$/, "id is a number"; |
486
|
|
|
|
|
|
|
subcontract "Check e-mail" => $valid_email, $user->{email}; |
487
|
|
|
|
|
|
|
}; |
488
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
# much later |
490
|
|
|
|
|
|
|
$valid_user->apply( $form_input ); |
491
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
Or pass a definition as I to be applied to specific structure parts |
493
|
|
|
|
|
|
|
(think I, like C |
494
|
|
|
|
|
|
|
|
495
|
|
|
|
|
|
|
my $array_of_foo = contract { |
496
|
|
|
|
|
|
|
my ($is_foo, $ref) = @_; |
497
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
foreach (@$ref) { |
499
|
|
|
|
|
|
|
subcontract "Element check", $is_foo, $_; |
500
|
|
|
|
|
|
|
}; |
501
|
|
|
|
|
|
|
}; |
502
|
|
|
|
|
|
|
|
503
|
|
|
|
|
|
|
$array_of_foo->apply( $valid_user, \@user_list ); |
504
|
|
|
|
|
|
|
|
505
|
|
|
|
|
|
|
=cut |
506
|
|
|
|
|
|
|
|
507
|
|
|
|
|
|
|
sub subcontract($$@) { ## no critic |
508
|
7
|
|
|
7
|
1
|
133
|
current_contract()->subcontract( @_ ); |
509
|
|
|
|
|
|
|
}; |
510
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
=head2 contract_is |
512
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
contract_is $report, $signature, "Message"; |
514
|
|
|
|
|
|
|
|
515
|
|
|
|
|
|
|
Assert that a contract is fulfilled exactly to the specified extent. |
516
|
|
|
|
|
|
|
See L for signature format. |
517
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
This may be useful for verifying assertions and contracts themselves. |
519
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
This is actually a clone of L. |
521
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
=cut |
523
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
=head2 current_contract |
525
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
Returns the L object being worked on. |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
If L has been detected and no contract block |
529
|
|
|
|
|
|
|
is executed explicitly, returns a L instance. |
530
|
|
|
|
|
|
|
This allows to define assertions and run them uniformly under |
531
|
|
|
|
|
|
|
both L and L control. |
532
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
Dies if no contract could be detected. |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
It is actually a clone of L. |
536
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
=head1 STATIC METHODS |
538
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
Use these methods to configure Assert::Refute globally. |
540
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
=head2 configure |
542
|
|
|
|
|
|
|
|
543
|
|
|
|
|
|
|
use Assert::Refute \%options; |
544
|
|
|
|
|
|
|
Assert::Refute->configure( \%options ); |
545
|
|
|
|
|
|
|
Assert::Refute->configure( \%options, "My::Package"); |
546
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
Set per-caller configuration values for given package. |
548
|
|
|
|
|
|
|
C is called implicitly by C |
549
|
|
|
|
|
|
|
if hash parameter(s) are present. |
550
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
%options may include: |
552
|
|
|
|
|
|
|
|
553
|
|
|
|
|
|
|
=over |
554
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
=item * on_pass - callback to execute if tests pass (default: C) |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
=item * on_fail - callback to execute if tests fail (default: C, |
558
|
|
|
|
|
|
|
but not just C - see below). |
559
|
|
|
|
|
|
|
|
560
|
|
|
|
|
|
|
=item * driver - use that class instead of L |
561
|
|
|
|
|
|
|
as contract report. |
562
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
=item * skip_all - reason for skipping ALL C blocks |
564
|
|
|
|
|
|
|
in the affected package. |
565
|
|
|
|
|
|
|
This defaults to C or C environment variable. |
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
B<[EXPERIMENTAL]>. Name and meaning MAY change in the future. |
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
=back |
570
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
The callbacks MUST be either |
572
|
|
|
|
|
|
|
a C accepting L object, |
573
|
|
|
|
|
|
|
or one of predefined strings: |
574
|
|
|
|
|
|
|
|
575
|
|
|
|
|
|
|
=over |
576
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
=item * skip - do nothing; |
578
|
|
|
|
|
|
|
|
579
|
|
|
|
|
|
|
=item * carp - warn the stringified report; |
580
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
=item * croak - die with stringified report as error message; |
582
|
|
|
|
|
|
|
|
583
|
|
|
|
|
|
|
=back |
584
|
|
|
|
|
|
|
|
585
|
|
|
|
|
|
|
Returns the resulting config (with default values added,etc). |
586
|
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
As of current, this method only affects C. |
588
|
|
|
|
|
|
|
|
589
|
|
|
|
|
|
|
=cut |
590
|
|
|
|
|
|
|
|
591
|
|
|
|
|
|
|
my %conf_known; |
592
|
|
|
|
|
|
|
$conf_known{$_}++ for qw( on_pass on_fail driver skip_all ); |
593
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
sub configure { |
595
|
11
|
|
|
11
|
1
|
138
|
my ($class, $given_conf, $caller) = @_; |
596
|
|
|
|
|
|
|
|
597
|
11
|
100
|
|
|
|
273
|
croak "Usage: $class->configure( \\%hash, \$target )" |
598
|
|
|
|
|
|
|
unless ref $given_conf eq 'HASH'; |
599
|
|
|
|
|
|
|
|
600
|
10
|
|
|
|
|
45
|
my @extra = grep { !$conf_known{$_} } keys %$given_conf; |
|
10
|
|
|
|
|
33
|
|
601
|
10
|
50
|
|
|
|
36
|
croak "$class->configure: unknown parameters (@extra)" |
602
|
|
|
|
|
|
|
if @extra; |
603
|
|
|
|
|
|
|
|
604
|
|
|
|
|
|
|
# configure whoever called us by default |
605
|
10
|
|
100
|
|
|
38
|
$caller ||= scalar caller; |
606
|
|
|
|
|
|
|
|
607
|
10
|
|
|
|
|
44
|
my $conf = { %default_conf, %$given_conf }; |
608
|
10
|
|
|
|
|
48
|
$conf->{on_fail} = _coerce_cb($conf->{on_fail}); |
609
|
10
|
|
|
|
|
27
|
$conf->{on_pass} = _coerce_cb($conf->{on_pass}); |
610
|
|
|
|
|
|
|
|
611
|
|
|
|
|
|
|
# Load driver |
612
|
10
|
100
|
|
|
|
40
|
if( $conf->{driver} ) { |
613
|
1
|
|
|
|
|
4
|
my $mod = "$conf->{driver}.pm"; |
614
|
1
|
|
|
|
|
3
|
$mod =~ s#::#/#g; |
615
|
1
|
|
|
|
|
6
|
require $mod; |
616
|
|
|
|
|
|
|
croak "$conf->{driver} is not Assert::Refute::Report, cannot use as driver" |
617
|
1
|
50
|
|
|
|
154
|
unless $conf->{driver}->isa('Assert::Refute::Report'); |
618
|
|
|
|
|
|
|
} else { |
619
|
9
|
|
|
|
|
21
|
$conf->{driver} = 'Assert::Refute::Report'; # this works for sure |
620
|
|
|
|
|
|
|
}; |
621
|
|
|
|
|
|
|
|
622
|
9
|
100
|
66
|
|
|
35
|
if ($NDEBUG and !$conf->{skip_all}) { |
623
|
1
|
|
|
|
|
3
|
$conf->{skip_all} = "Assert::Refute turned off via NDEBUG=$NDEBUG"; |
624
|
|
|
|
|
|
|
}; |
625
|
|
|
|
|
|
|
|
626
|
9
|
100
|
|
|
|
24
|
if ($conf->{skip_all}) { |
627
|
1
|
|
|
|
|
3
|
my $default_report = $conf->{driver}->new; |
628
|
1
|
|
|
|
|
4
|
$default_report->plan( skip_all => $conf->{skip_all} ); |
629
|
1
|
|
|
|
|
4
|
$default_report->done_testing; |
630
|
1
|
|
|
|
|
2
|
$conf->{skip_all} = $default_report; |
631
|
|
|
|
|
|
|
} else { |
632
|
8
|
|
|
|
|
13
|
delete $conf->{skip_all}; |
633
|
|
|
|
|
|
|
}; |
634
|
|
|
|
|
|
|
|
635
|
9
|
|
|
|
|
27
|
$CALLER_CONF{$caller} = $conf; |
636
|
|
|
|
|
|
|
}; |
637
|
|
|
|
|
|
|
|
638
|
|
|
|
|
|
|
=head2 get_config |
639
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
Returns configuration from above, initializing with defaults if needed. |
641
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
=cut |
643
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
sub get_config { |
645
|
0
|
|
|
0
|
1
|
0
|
my ($class, $caller) = @_; |
646
|
|
|
|
|
|
|
|
647
|
0
|
|
0
|
|
|
0
|
$caller ||= scalar caller; |
648
|
0
|
|
0
|
|
|
0
|
return $CALLER_CONF{$caller} ||= $class->configure({}, $caller); |
649
|
|
|
|
|
|
|
}; |
650
|
|
|
|
|
|
|
|
651
|
|
|
|
|
|
|
sub _coerce_cb { |
652
|
20
|
|
|
20
|
|
32
|
my $sub = shift; |
653
|
|
|
|
|
|
|
|
654
|
20
|
100
|
|
|
|
65
|
$sub = defined $known_callback{$sub} ? $known_callback{$sub} : $sub; |
655
|
20
|
100
|
|
|
|
52
|
return unless $sub; |
656
|
8
|
50
|
33
|
|
|
42
|
croak "Bad callback $sub" |
657
|
|
|
|
|
|
|
unless ref $sub and UNIVERSAL::isa( $sub, 'CODE' ); |
658
|
8
|
|
|
|
|
32
|
return $sub; |
659
|
|
|
|
|
|
|
}; |
660
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
=head2 refute_these |
662
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
B<[DEPRECATED]> This used to be the old name of C. |
664
|
|
|
|
|
|
|
It just dies now and will be removed completely in the future. |
665
|
|
|
|
|
|
|
|
666
|
|
|
|
|
|
|
=cut |
667
|
|
|
|
|
|
|
|
668
|
|
|
|
|
|
|
# TODO v.0.20 remove completely |
669
|
|
|
|
|
|
|
# Keep it prototyped just in case some poor guy/gal forgot to change it |
670
|
|
|
|
|
|
|
# or else they'll get a very confusing error message |
671
|
|
|
|
|
|
|
sub refute_these (&;@) { ## no critic # need prototype |
672
|
1
|
|
|
1
|
1
|
186
|
croak "refute_these { ... } is no more supported, use try_refute{ ... } instead"; |
673
|
|
|
|
|
|
|
} |
674
|
|
|
|
|
|
|
|
675
|
|
|
|
|
|
|
=head1 EXTENDING THE SUITE |
676
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
Although building wrappers around C call is easy enough, |
678
|
|
|
|
|
|
|
specialized tool exists for doing that. |
679
|
|
|
|
|
|
|
|
680
|
|
|
|
|
|
|
Use L to define new I as |
681
|
|
|
|
|
|
|
both prototyped exportable functions and their counterpart methods |
682
|
|
|
|
|
|
|
in L. |
683
|
|
|
|
|
|
|
These functions will perform absolutely the same |
684
|
|
|
|
|
|
|
under control of C, C, and L: |
685
|
|
|
|
|
|
|
|
686
|
|
|
|
|
|
|
package My::Prime; |
687
|
|
|
|
|
|
|
|
688
|
|
|
|
|
|
|
use Assert::Refute::Build; |
689
|
|
|
|
|
|
|
use parent qw(Exporter); |
690
|
|
|
|
|
|
|
|
691
|
|
|
|
|
|
|
build_refute is_prime => sub { |
692
|
|
|
|
|
|
|
my $n = shift; |
693
|
|
|
|
|
|
|
return "Not a natural number: $n" unless $n =~ /^\d+$/; |
694
|
|
|
|
|
|
|
return "$n is not prime" if $n <= 1; |
695
|
|
|
|
|
|
|
for (my $i = 2; $i*$i <= $n; $i++) { |
696
|
|
|
|
|
|
|
return "$i divides $n" unless $n % $i; |
697
|
|
|
|
|
|
|
}; |
698
|
|
|
|
|
|
|
return ''; |
699
|
|
|
|
|
|
|
}, args => 1, export => 1; |
700
|
|
|
|
|
|
|
|
701
|
|
|
|
|
|
|
Much later: |
702
|
|
|
|
|
|
|
|
703
|
|
|
|
|
|
|
use My::Prime; |
704
|
|
|
|
|
|
|
|
705
|
|
|
|
|
|
|
is_prime 101, "101 is prime"; |
706
|
|
|
|
|
|
|
is_prime 42, "Life is simple"; # not true |
707
|
|
|
|
|
|
|
|
708
|
|
|
|
|
|
|
Note that the implementation C only cares about its arguments, |
709
|
|
|
|
|
|
|
and doesn't do anything except returning a value. |
710
|
|
|
|
|
|
|
Suddenly it's a L! |
711
|
|
|
|
|
|
|
|
712
|
|
|
|
|
|
|
Yet the exact reason for $n not being a prime will be reflected in test output. |
713
|
|
|
|
|
|
|
|
714
|
|
|
|
|
|
|
One can also subclass L |
715
|
|
|
|
|
|
|
to create new I, for instance, |
716
|
|
|
|
|
|
|
to register failed/passed tests in a unit-testing framework of choice |
717
|
|
|
|
|
|
|
or generate warnings/exceptions when conditions are not met. |
718
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
That's how L integration is done - |
720
|
|
|
|
|
|
|
see L. |
721
|
|
|
|
|
|
|
|
722
|
|
|
|
|
|
|
=head1 PERFORMANCE |
723
|
|
|
|
|
|
|
|
724
|
|
|
|
|
|
|
Set C or C (takes precedence) |
725
|
|
|
|
|
|
|
environment variable to true to replace I C blocks with a stub. |
726
|
|
|
|
|
|
|
L was used as reference. |
727
|
|
|
|
|
|
|
|
728
|
|
|
|
|
|
|
If that's not enough, use L |
729
|
|
|
|
|
|
|
or just define a DEBUG constant and |
730
|
|
|
|
|
|
|
append an C statement to C blocks. |
731
|
|
|
|
|
|
|
|
732
|
|
|
|
|
|
|
That said, refute is reasonably fast. |
733
|
|
|
|
|
|
|
Special care is taken to minimize the CPU usage by I contracts. |
734
|
|
|
|
|
|
|
|
735
|
|
|
|
|
|
|
The C file in this distribution is capable of |
736
|
|
|
|
|
|
|
verifying around 4000 contracts of 100 statements each in just under a second |
737
|
|
|
|
|
|
|
on my 4500 C laptop. |
738
|
|
|
|
|
|
|
Your mileage may vary! |
739
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
=head1 WHY REFUTE |
741
|
|
|
|
|
|
|
|
742
|
|
|
|
|
|
|
Communicating a passing test normally requires 1 bit of information: |
743
|
|
|
|
|
|
|
everything went as planned. |
744
|
|
|
|
|
|
|
For failing test, however, as much information as possible is desired. |
745
|
|
|
|
|
|
|
|
746
|
|
|
|
|
|
|
Thus C stands for an inverted assertion. |
747
|
|
|
|
|
|
|
If $condition is B, it is regarded as a B. |
748
|
|
|
|
|
|
|
If it is B, however, it is considered to be the B |
749
|
|
|
|
|
|
|
for a failing test. |
750
|
|
|
|
|
|
|
|
751
|
|
|
|
|
|
|
This is similar to how Unix programs set their exit code, |
752
|
|
|
|
|
|
|
or to Perl's own C<$@> variable, |
753
|
|
|
|
|
|
|
or to the I concept in science. |
754
|
|
|
|
|
|
|
|
755
|
|
|
|
|
|
|
A C is a result of multiple checks, |
756
|
|
|
|
|
|
|
combined into a single refutation. |
757
|
|
|
|
|
|
|
It will succeed silently, yet spell out details if it doesn't pass. |
758
|
|
|
|
|
|
|
|
759
|
|
|
|
|
|
|
These primitives can serve as building blocks for arbitrarily complex |
760
|
|
|
|
|
|
|
assertions, tests, and validations. |
761
|
|
|
|
|
|
|
|
762
|
|
|
|
|
|
|
=head1 DEPRECATION WARNING |
763
|
|
|
|
|
|
|
|
764
|
|
|
|
|
|
|
The following modules used to be part of this package, but are separate |
765
|
|
|
|
|
|
|
CPAN distributions now: |
766
|
|
|
|
|
|
|
|
767
|
|
|
|
|
|
|
=over |
768
|
|
|
|
|
|
|
|
769
|
|
|
|
|
|
|
=item * L |
770
|
|
|
|
|
|
|
|
771
|
|
|
|
|
|
|
=item * L |
772
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
=item * L |
774
|
|
|
|
|
|
|
|
775
|
|
|
|
|
|
|
=item * L |
776
|
|
|
|
|
|
|
|
777
|
|
|
|
|
|
|
=back |
778
|
|
|
|
|
|
|
|
779
|
|
|
|
|
|
|
=head1 SEE ALSO |
780
|
|
|
|
|
|
|
|
781
|
|
|
|
|
|
|
L, L, L |
782
|
|
|
|
|
|
|
|
783
|
|
|
|
|
|
|
=head1 BUGS |
784
|
|
|
|
|
|
|
|
785
|
|
|
|
|
|
|
This module is still under heavy development. |
786
|
|
|
|
|
|
|
See C file in this distribution for an approximate roadmap. |
787
|
|
|
|
|
|
|
|
788
|
|
|
|
|
|
|
New features are marked as B<[EXPERIMENTAL]>. |
789
|
|
|
|
|
|
|
Features that are to be removed will |
790
|
|
|
|
|
|
|
stay B<[DEPRECATED]> (with a corresponding warning) for at least 5 releases, |
791
|
|
|
|
|
|
|
unless such deprecation is extremely cumbersome. |
792
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
Test coverage is maintained at >90%, but who knows what lurks in the other 10%. |
794
|
|
|
|
|
|
|
|
795
|
|
|
|
|
|
|
See L |
796
|
|
|
|
|
|
|
to browse old bugs or report new ones. |
797
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
=head1 SUPPORT |
799
|
|
|
|
|
|
|
|
800
|
|
|
|
|
|
|
You can find documentation for this module with the C command. |
801
|
|
|
|
|
|
|
|
802
|
|
|
|
|
|
|
perldoc Assert::Refute |
803
|
|
|
|
|
|
|
|
804
|
|
|
|
|
|
|
You can also look for information at: |
805
|
|
|
|
|
|
|
|
806
|
|
|
|
|
|
|
=over |
807
|
|
|
|
|
|
|
|
808
|
|
|
|
|
|
|
=item * First and foremost, use |
809
|
|
|
|
|
|
|
L! |
810
|
|
|
|
|
|
|
|
811
|
|
|
|
|
|
|
=item * C |
812
|
|
|
|
|
|
|
|
813
|
|
|
|
|
|
|
L |
814
|
|
|
|
|
|
|
|
815
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
816
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
L |
818
|
|
|
|
|
|
|
|
819
|
|
|
|
|
|
|
=item * CPAN Ratings |
820
|
|
|
|
|
|
|
|
821
|
|
|
|
|
|
|
L |
822
|
|
|
|
|
|
|
|
823
|
|
|
|
|
|
|
=item * Search CPAN |
824
|
|
|
|
|
|
|
|
825
|
|
|
|
|
|
|
L |
826
|
|
|
|
|
|
|
|
827
|
|
|
|
|
|
|
=back |
828
|
|
|
|
|
|
|
|
829
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
830
|
|
|
|
|
|
|
|
831
|
|
|
|
|
|
|
=over |
832
|
|
|
|
|
|
|
|
833
|
|
|
|
|
|
|
=item * Thanks to L |
834
|
|
|
|
|
|
|
for C function name as well as a lot of feedback. |
835
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
=item * This L |
837
|
|
|
|
|
|
|
by C inspired me to actually start working |
838
|
|
|
|
|
|
|
on the first incarnation of this project. |
839
|
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
=item * Thanks to C for pass() and fail() calls. |
841
|
|
|
|
|
|
|
|
842
|
|
|
|
|
|
|
=back |
843
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
845
|
|
|
|
|
|
|
|
846
|
|
|
|
|
|
|
Copyright 2017-2018 Konstantin S. Uvarin. C<< >> |
847
|
|
|
|
|
|
|
|
848
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
849
|
|
|
|
|
|
|
under the terms of the the Artistic License (2.0). You may obtain a |
850
|
|
|
|
|
|
|
copy of the full license at: |
851
|
|
|
|
|
|
|
|
852
|
|
|
|
|
|
|
L |
853
|
|
|
|
|
|
|
|
854
|
|
|
|
|
|
|
Any use, modification, and distribution of the Standard or Modified |
855
|
|
|
|
|
|
|
Versions is governed by this Artistic License. By using, modifying or |
856
|
|
|
|
|
|
|
distributing the Package, you accept this license. Do not use, modify, |
857
|
|
|
|
|
|
|
or distribute the Package, if you do not accept this license. |
858
|
|
|
|
|
|
|
|
859
|
|
|
|
|
|
|
If your Modified Version has been derived from a Modified Version made |
860
|
|
|
|
|
|
|
by someone other than you, you are nevertheless required to ensure that |
861
|
|
|
|
|
|
|
your Modified Version complies with the requirements of this license. |
862
|
|
|
|
|
|
|
|
863
|
|
|
|
|
|
|
This license does not grant you the right to use any trademark, service |
864
|
|
|
|
|
|
|
mark, tradename, or logo of the Copyright Holder. |
865
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
This license includes the non-exclusive, worldwide, free-of-charge |
867
|
|
|
|
|
|
|
patent license to make, have made, use, offer to sell, sell, import and |
868
|
|
|
|
|
|
|
otherwise transfer the Package with respect to any patent claims |
869
|
|
|
|
|
|
|
licensable by the Copyright Holder that are necessarily infringed by the |
870
|
|
|
|
|
|
|
Package. If you institute patent litigation (including a cross-claim or |
871
|
|
|
|
|
|
|
counterclaim) against any party alleging that the Package constitutes |
872
|
|
|
|
|
|
|
direct or contributory patent infringement, then this Artistic License |
873
|
|
|
|
|
|
|
to you shall terminate on the date that such litigation is filed. |
874
|
|
|
|
|
|
|
|
875
|
|
|
|
|
|
|
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER |
876
|
|
|
|
|
|
|
AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. |
877
|
|
|
|
|
|
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
878
|
|
|
|
|
|
|
PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY |
879
|
|
|
|
|
|
|
YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR |
880
|
|
|
|
|
|
|
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR |
881
|
|
|
|
|
|
|
CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, |
882
|
|
|
|
|
|
|
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
883
|
|
|
|
|
|
|
|
884
|
|
|
|
|
|
|
=cut |
885
|
|
|
|
|
|
|
|
886
|
|
|
|
|
|
|
1; # End of Assert::Refute |