@@ -20,7 +20,8 @@ Defined as:
20
20
Specify the count of tests -- usually written at the beginning of a
21
21
test file.
22
22
23
- plan 15; # expect to run 15 tests
23
+ = for code :preamble<use Test;>
24
+ plan 15; # expect to run 15 tests
24
25
25
26
In C < subtest > s, C < plan > is used to specify the count of tests within
26
27
the subtest.
@@ -76,11 +77,11 @@ a C<plan> function when all tests are finalized. Use of C<plan> can help detect
76
77
test failures otherwise not reported because tests were accidentally skipped due
77
78
to bugs in the tests or bugs in the compiler. For example:
78
79
80
+ = for code :preamble<use Test;>
79
81
sub do-stuff {@};
80
82
use Test;
81
83
ok .is-prime for do-stuff;
82
84
done-testing;
83
-
84
85
# output:
85
86
1..0
86
87
@@ -91,11 +92,11 @@ passes.
91
92
92
93
Adding C < plan > gives a true picture of the test:
93
94
95
+ = for code :preamble<use Test;>
94
96
sub do-stuff {@};
95
97
use Test;
96
98
plan 1;
97
99
ok .is-prime for do-stuff;
98
-
99
100
# output:
100
101
1..1
101
102
# Looks like you planned 1 test, but ran 0
@@ -114,13 +115,15 @@ C<True>. The C<nok> function marks a test as passed if the given value evaluates
114
115
to C < False > . Both functions accept an optional description of the test as second
115
116
parameter.
116
117
118
+ = for code :preamble<use Test;>
117
119
my $response; my $query; ...;
118
120
ok $response.success, 'HTTP response was successful';
119
121
nok $query.error, 'Query completed without error';
120
122
121
123
In principle, you could use C < ok > for every kind of comparison test, by
122
124
including the comparison in the expression passed to C < $value > :
123
125
126
+ = for code :preamble<use Test;>
124
127
sub factorial($x) { ... };
125
128
ok factorial(6) == 720, 'Factorial - small integer';
126
129
@@ -138,6 +141,7 @@ The C<nok> function marks a test as passed if the given value evaluates to
138
141
C < False > . It also accepts an optional description of the test as second
139
142
argument.
140
143
144
+ = for code :preamble<use Test;>
141
145
my $response; my $query; ...;
142
146
ok $response.success, 'HTTP response was successful';
143
147
nok $query.error, 'Query completed without error';
@@ -161,6 +165,7 @@ a good function for testing more complex things, such as lists: C<is (1, (2,
161
165
different. For those cases, use
162
166
L « C < is-deeply > routine|/language/testing#index-entry-is-deeply-is-deeply($value,_$expected,_$description?)»
163
167
168
+ = for code :preamble<use Test;>
164
169
my $pdf-document; sub factorial($x) { ... }; ...;
165
170
is $pdf-document.author, "Joe", 'Retrieving the author field';
166
171
is factorial(6), 720, 'Factorial - small integer';
@@ -172,7 +177,7 @@ failure message differently, to show the whitespace in each values. For example,
172
177
in the output below, the second test shows the literal C < \t > in the C < got: >
173
178
line:
174
179
175
- = for code
180
+ = for code :preamble<use Test;>
176
181
is "foo\tbar", "foo\tbaz"; # expected: 'foo baz'# got: 'foo bar'
177
182
is "foo\tbar", "foo bar"; # expected: "foo bar"# got: "foo\tbar"
178
183
@@ -187,8 +192,8 @@ Marks a test as passed if C<$value> and C<$expected> are B<not> equal using
187
192
the same rules as C < is() > . The function accepts an optional description
188
193
of the test.
189
194
195
+ = for code :preamble<use Test;>
190
196
isnt pi, 3, 'The constant π is not equal to 3';
191
-
192
197
my Int $a = 23;
193
198
$a = Nil;
194
199
isnt $a, Nil, 'Nil should not survive being put in a container';
@@ -245,6 +250,8 @@ If no tolerance is set, the function will base the tolerance on the I<absolute>
245
250
value of C < $expected > : if it's smaller than C < 1e-6 > , use absolute tolerance of
246
251
C < 1e-5 > ; if it's larger, use relative tolerance of C < 1e-6 > .
247
252
253
+ = begin code :preamble<use Test;>
254
+
248
255
my Numeric ($value, $expected, $abs-tol, $rel-tol) = ...
249
256
250
257
is-approx $value, $expected;
@@ -261,18 +268,22 @@ C<1e-5>; if it's larger, use relative tolerance of C<1e-6>.
261
268
262
269
is-approx $value, $expected, :$abs-tol, :$rel-tol;
263
270
is-approx $value, $expected, :$abs-tol, :$rel-tol, 'test description';
271
+ = end code
264
272
265
273
= head3 Absolute tolerance
266
274
267
275
When an absolute tolerance is set, it's used as the actual maximum value by
268
276
which the C < $value > and C < $expected > can differ. For example:
269
277
278
+ = begin code :preamble<use Test;>
279
+
270
280
is-approx 3, 4, 2; # success
271
281
is-approx 3, 6, 2; # fail
272
282
273
283
is-approx 300, 302, 2; # success
274
284
is-approx 300, 400, 2; # fail
275
285
is-approx 300, 600, 2; # fail
286
+ = end code
276
287
277
288
Regardless of values given, the difference between them cannot be more
278
289
than C < 2 > .
@@ -285,11 +296,13 @@ larger the value they can differ by can be.
285
296
286
297
For example:
287
298
299
+ = begin code :preamble<use Test;>
288
300
is-approx 10, 10.5, :rel-tol<0.1>; # success
289
301
is-approx 10, 11.5, :rel-tol<0.1>; # fail
290
302
291
303
is-approx 100, 105, :rel-tol<0.1>; # success
292
304
is-approx 100, 115, :rel-tol<0.1>; # fail
305
+ = end code
293
306
294
307
Both versions use C < 0.1 > for relative tolerance, yet the first can differ
295
308
by about C < 1 > while the second can differ by about C < 10 > . The function used
@@ -305,7 +318,7 @@ and the test will fail if C<rel-diff> is higher than C<$rel-tol>.
305
318
306
319
= head3 Both absolute and relative tolerance specified
307
320
308
- = for code :skip-test<compile time error >
321
+ = for code :preamble<use Test; my ($value, $expected); >
309
322
is-approx $value, $expected, :rel-tol<.5>, :abs-tol<10>;
310
323
311
324
When both absolute and relative tolerances are specified, each will be
@@ -375,18 +388,20 @@ The C<$comparison> comparator can be either a L<Callable|/type/Callable> or
375
388
a L < Str|/type/Str > containing an infix operator, such as C < '==' > , a C < '~~' > , or a
376
389
user-defined infix.
377
390
391
+ = for code :preamble<use Test;>
378
392
cmp-ok 'my spelling is apperling', '~~', /perl/, "bad speller";
379
393
380
- Metaoperators cannot be given as a string; pass them as a L < Callable|/type/Callable >
381
- instead:
394
+ Metaoperators cannot be given as a string; pass them as a
395
+ L < Callable|/type/Callable > instead:
382
396
397
+ = for code :preamble<use Test;>
383
398
cmp-ok <a b c>, &[!eqv], <b d e>, 'not equal';
384
399
385
400
A L < Callable|/type/Callable > C < $comparison > lets you use custom comparisons:
386
401
402
+ = for code :preamble<use Test;>
387
403
sub my-comp { $^a / $^b < rand };
388
404
cmp-ok 1, &my-comp, 2, 'the dice giveth and the dice taketh away'
389
-
390
405
cmp-ok 2, -> $a, $b { $a.is-prime and $b.is-prime and $a < $b }, 7,
391
406
'we got primes, one larger than the other!';
392
407
@@ -401,12 +416,14 @@ given C<$expected-type>. For convenience, types may also be specified as a
401
416
string. The function accepts an optional description of the test, which
402
417
defaults to a string that describes the object.
403
418
419
+ = begin code :preamble<use Test;>
404
420
class Womble {}
405
421
class GreatUncleBulgaria is Womble {}
406
422
my $womble = GreatUncleBulgaria.new;
407
423
408
424
isa-ok $womble, Womble, "Great Uncle Bulgaria is a womble";
409
425
isa-ok $womble, 'Womble'; # equivalent
426
+ = end code
410
427
411
428
= head2 sub can-ok
412
429
@@ -418,6 +435,7 @@ Marks a test as passed if the given C<$variable> can run the given
418
435
C < $method-name > . The function accepts an optional description. For
419
436
instance:
420
437
438
+ = begin code :preamble<use Test;>
421
439
class Womble {};
422
440
my $womble = Womble.new;
423
441
@@ -428,6 +446,7 @@ instance:
428
446
# with human-generated test description
429
447
can-ok $womble, 'collect-rubbish', "Wombles can collect rubbish";
430
448
# => Wombles can collect rubbish
449
+ = end code
431
450
432
451
= head2 sub does-ok
433
452
@@ -438,6 +457,7 @@ Defined as:
438
457
Marks a test as passed if the given C < $variable > can do the given C < $role > .
439
458
The function accepts an optional description of the test.
440
459
460
+ = begin code :preamble<use Test;>
441
461
# create a Womble who can invent
442
462
role Invent {
443
463
method brainstorm { say "Aha!" }
@@ -456,7 +476,7 @@ The function accepts an optional description of the test.
456
476
457
477
does-ok $tobermory, Invent, "Tobermory can invent";
458
478
# => Tobermory can invent
459
-
479
+ = end code
460
480
461
481
= head2 sub like
462
482
@@ -466,6 +486,7 @@ Defined as:
466
486
467
487
Use it this way:
468
488
489
+ = for code :preamble<use Test;>
469
490
like 'foo', /fo/, 'foo looks like fo';
470
491
471
492
Marks a test as passed if the C < $value > , when coerced to a string, matches the
@@ -480,6 +501,7 @@ Defined as:
480
501
481
502
Used this way:
482
503
504
+ = for code :preamble<use Test;>
483
505
unlike 'foo', /bar/, 'foo does not look like bar';
484
506
485
507
Marks a test as passed if the C < $value > , when coerced to a string, does B < not >
@@ -494,6 +516,7 @@ Defined as:
494
516
495
517
Marks a test as passed if the given C < $module > loads correctly.
496
518
519
+ = for code :preamble<use Test;>
497
520
use-ok 'Full::Qualified::ModuleName';
498
521
499
522
= head2 sub dies-ok
@@ -506,7 +529,7 @@ Marks a test as passed if the given C<$code> throws an exception.
506
529
507
530
The function accepts an optional description of the test.
508
531
509
- = begin code
532
+ = begin code :preamble<use Test;>
510
533
sub saruman(Bool :$ents-destroy-isengard) {
511
534
die "Killed by Wormtongue" if $ents-destroy-isengard;
512
535
}
@@ -525,7 +548,7 @@ exception.
525
548
526
549
The function accepts an optional description of the test.
527
550
528
- = begin code
551
+ = begin code :preamble<use Test;>
529
552
sub frodo(Bool :$destroys-ring) {
530
553
die "Oops, that wasn't supposed to happen" unless $destroys-ring;
531
554
}
@@ -544,7 +567,7 @@ C<eval>ed as code.
544
567
545
568
The function accepts an optional description of the test.
546
569
547
- = begin code
570
+ = begin code :preamble<use Test;>
548
571
eval-dies-ok q[my $joffrey = "nasty";
549
572
die "bye bye Ned" if $joffrey ~~ /nasty/],
550
573
"Ned Stark dies";
@@ -561,7 +584,7 @@ exception when C<eval>ed as code.
561
584
562
585
The function accepts an optional description of the test.
563
586
564
- = begin code
587
+ = begin code :preamble<use Test;>
565
588
eval-lives-ok q[my $daenerys-burns = False;
566
589
die "Oops, Khaleesi now ashes" if $daenerys-burns],
567
590
"Dany is blood of the dragon";
@@ -582,7 +605,7 @@ If an exception was thrown, it will also try to match the matcher hash,
582
605
where the key is the name of the method to be called on the exception, and
583
606
the value is the value it should have to pass. For example:
584
607
585
- = begin code
608
+ = begin code :preamble<use Test;>
586
609
sub frodo(Bool :$destroys-ring) {
587
610
fail "Oops. Frodo dies" unless $destroys-ring
588
611
};
@@ -598,7 +621,7 @@ the tested code does not sink the possible L<Failures|/type/Failure>. If you
598
621
wish to test that the code returns a L < Failure|/type/Failure > instead of
599
622
throwing, use C < fails-like > routine instead.
600
623
601
- = begin code
624
+ = begin code :preamble<use Test;>
602
625
sub fails-not-throws { +"a" }
603
626
# test passes, even though it's just a Failure and would not always throw:
604
627
throws-like { fails-not-throws }, Exception;
@@ -617,14 +640,16 @@ encapsulate your string with a block and an EVAL instead. For instance:
617
640
618
641
Defined as:
619
642
620
- = for code
643
+ = for code :preamble<use Test;>
644
+
621
645
sub fails-like ( \test where Callable:D|Str:D, $ex-type, $reason?, *%matcher)
622
646
623
647
Same interface as C < throws-like > , except checks that the code returns a
624
648
L < Failure|/type/Failure > instead of throwing. If the code does throw or if the
625
649
returned L < Failure|/type/Failure > has already been L < handled|/routine/handled > ,
626
650
that will be considered as a failed test.
627
651
652
+ = for code :preamble<use Test;>
628
653
fails-like { +"a" }, X::Str::Numeric,
629
654
:message(/'Cannot convert string to number'/),
630
655
'converting non-numeric string to number fails';
@@ -643,7 +668,7 @@ I<one> test in C<plan>, C<todo>, or C<skip> counts. It will pass the
643
668
test only if B < all > tests in the block pass. The function accepts an
644
669
optional description of the subtest.
645
670
646
- = begin code
671
+ = begin code :preamble<use Test;>
647
672
class Womble {}
648
673
649
674
class GreatUncleBulgaria is Womble {
@@ -665,7 +690,7 @@ You can also place the description as the first positional argument, or use a
665
690
C < Pair > with description as the key and subtest's code as the value. This can be
666
691
useful for subtests with large bodies.
667
692
668
- = begin code
693
+ = begin code :preamble<use Test;>
669
694
subtest 'A bunch of tests', {
670
695
plan 42;
671
696
...
@@ -725,7 +750,7 @@ Skip C<$count> tests, giving a C<$reason> as to why. By default only one
725
750
test will be skipped. Use such functionality when a test (or tests) would
726
751
die if run.
727
752
728
- = begin code
753
+ = begin code :preamble<use Test;>
729
754
sub num-forward-slashes($arg) { ... } ;
730
755
731
756
if $*KERNEL ~~ 'linux' {
@@ -750,7 +775,7 @@ Skip the remaining tests. If the remainder of the tests in the test file
750
775
would all fail due to some condition, use this function to skip them,
751
776
providing an optional C < $reason > as to why.
752
777
753
- = begin code
778
+ = begin code :preamble<use Test;>
754
779
my $location; sub womble { ... }; ...;
755
780
unless $location ~~ "Wimbledon Common" {
756
781
skip-rest "We can't womble, the remaining tests will fail";
0 commit comments