Skip to content

Commit 4311d97

Browse files
committed
Rewrites user example compilation test
Now everything is compiled via -c, instead of using EVAL. This is slower, but closes #2764 since apparently the evaluation of so much code was gobbling up memory, and closes #2782 since either the memory itself or the creation of multiple objects was affecting this somehow. It also causes #2789, so some changes are trying to fix that.
1 parent 73314de commit 4311d97

File tree

3 files changed

+60
-34
lines changed

3 files changed

+60
-34
lines changed

doc/Language/testing.pod6

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ Testing code is an integral part of software development. Tests provide
99
automated, repeatable verifications of code behavior, and ensures your
1010
code works as expected.
1111
12-
In Perl 6, the L<Test|/type/Test> module provides a testing framework, used also by
13-
Perl 6's official spectest suite.
12+
In Perl 6, the L<Test|/type/Test> module provides a testing framework, used also
13+
by Perl 6's official spectest suite.
1414
1515
The testing functions emit output conforming to the L<Test Anything Protocol|https://testanything.org>. In general, they are used in sink context:
1616
17-
=for code :preamble<my ($meta,$relaxed-name); sub check-name($meta,:$relaxed-name){}>
17+
=for code :preamble<use Test; my ($meta,$relaxed-name); sub check-name($meta,:$relaxed-name){}>
1818
ok check-name($meta, :$relaxed-name), "name has a hyphen rather than '::'"
1919
2020
but all functions also return as a Boolean if the test has been successful or not, which can be used to print a message if the test fails:
2121
22-
=begin code :preamble<my ($meta,$relaxed-name); sub check-name($meta,:$relaxed-name){}>
22+
=begin code :preamble<use Test; my ($meta,$relaxed-name); sub check-name($meta,:$relaxed-name){}>
2323
ok check-name($meta, :$relaxed-name), "name has a hyphen rather than '::'" \
2424
or diag "\nTo use hyphen in name, pass :relaxed-name to meta-ok\n";
2525
=end code

doc/Type/Test.pod6

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ Defined as:
2020
Specify the count of tests -- usually written at the beginning of a
2121
test file.
2222
23-
plan 15; # expect to run 15 tests
23+
=for code :preamble<use Test;>
24+
plan 15; # expect to run 15 tests
2425
2526
In C<subtest>s, C<plan> is used to specify the count of tests within
2627
the subtest.
@@ -76,11 +77,11 @@ a C<plan> function when all tests are finalized. Use of C<plan> can help detect
7677
test failures otherwise not reported because tests were accidentally skipped due
7778
to bugs in the tests or bugs in the compiler. For example:
7879
80+
=for code :preamble<use Test;>
7981
sub do-stuff {@};
8082
use Test;
8183
ok .is-prime for do-stuff;
8284
done-testing;
83-
8485
# output:
8586
1..0
8687
@@ -91,11 +92,11 @@ passes.
9192
9293
Adding C<plan> gives a true picture of the test:
9394
95+
=for code :preamble<use Test;>
9496
sub do-stuff {@};
9597
use Test;
9698
plan 1;
9799
ok .is-prime for do-stuff;
98-
99100
# output:
100101
1..1
101102
# 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
114115
to C<False>. Both functions accept an optional description of the test as second
115116
parameter.
116117
118+
=for code :preamble<use Test;>
117119
my $response; my $query; ...;
118120
ok $response.success, 'HTTP response was successful';
119121
nok $query.error, 'Query completed without error';
120122
121123
In principle, you could use C<ok> for every kind of comparison test, by
122124
including the comparison in the expression passed to C<$value>:
123125
126+
=for code :preamble<use Test;>
124127
sub factorial($x) { ... };
125128
ok factorial(6) == 720, 'Factorial - small integer';
126129
@@ -138,6 +141,7 @@ The C<nok> function marks a test as passed if the given value evaluates to
138141
C<False>. It also accepts an optional description of the test as second
139142
argument.
140143
144+
=for code :preamble<use Test;>
141145
my $response; my $query; ...;
142146
ok $response.success, 'HTTP response was successful';
143147
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,
161165
different. For those cases, use
162166
L«C<is-deeply> routine|/language/testing#index-entry-is-deeply-is-deeply($value,_$expected,_$description?)»
163167
168+
=for code :preamble<use Test;>
164169
my $pdf-document; sub factorial($x) { ... }; ...;
165170
is $pdf-document.author, "Joe", 'Retrieving the author field';
166171
is factorial(6), 720, 'Factorial - small integer';
@@ -172,7 +177,7 @@ failure message differently, to show the whitespace in each values. For example,
172177
in the output below, the second test shows the literal C<\t> in the C<got:>
173178
line:
174179
175-
=for code
180+
=for code :preamble<use Test;>
176181
is "foo\tbar", "foo\tbaz"; # expected: 'foo baz'␤# got: 'foo bar'
177182
is "foo\tbar", "foo bar"; # expected: "foo bar"␤# got: "foo\tbar"
178183
@@ -187,8 +192,8 @@ Marks a test as passed if C<$value> and C<$expected> are B<not> equal using
187192
the same rules as C<is()>. The function accepts an optional description
188193
of the test.
189194
195+
=for code :preamble<use Test;>
190196
isnt pi, 3, 'The constant π is not equal to 3';
191-
192197
my Int $a = 23;
193198
$a = Nil;
194199
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>
245250
value of C<$expected>: if it's smaller than C<1e-6>, use absolute tolerance of
246251
C<1e-5>; if it's larger, use relative tolerance of C<1e-6>.
247252
253+
=begin code :preamble<use Test;>
254+
248255
my Numeric ($value, $expected, $abs-tol, $rel-tol) = ...
249256
250257
is-approx $value, $expected;
@@ -261,18 +268,22 @@ C<1e-5>; if it's larger, use relative tolerance of C<1e-6>.
261268
262269
is-approx $value, $expected, :$abs-tol, :$rel-tol;
263270
is-approx $value, $expected, :$abs-tol, :$rel-tol, 'test description';
271+
=end code
264272
265273
=head3 Absolute tolerance
266274
267275
When an absolute tolerance is set, it's used as the actual maximum value by
268276
which the C<$value> and C<$expected> can differ. For example:
269277
278+
=begin code :preamble<use Test;>
279+
270280
is-approx 3, 4, 2; # success
271281
is-approx 3, 6, 2; # fail
272282
273283
is-approx 300, 302, 2; # success
274284
is-approx 300, 400, 2; # fail
275285
is-approx 300, 600, 2; # fail
286+
=end code
276287
277288
Regardless of values given, the difference between them cannot be more
278289
than C<2>.
@@ -285,11 +296,13 @@ larger the value they can differ by can be.
285296
286297
For example:
287298
299+
=begin code :preamble<use Test;>
288300
is-approx 10, 10.5, :rel-tol<0.1>; # success
289301
is-approx 10, 11.5, :rel-tol<0.1>; # fail
290302
291303
is-approx 100, 105, :rel-tol<0.1>; # success
292304
is-approx 100, 115, :rel-tol<0.1>; # fail
305+
=end code
293306
294307
Both versions use C<0.1> for relative tolerance, yet the first can differ
295308
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>.
305318
306319
=head3 Both absolute and relative tolerance specified
307320
308-
=for code :skip-test<compile time error>
321+
=for code :preamble<use Test; my ($value, $expected);>
309322
is-approx $value, $expected, :rel-tol<.5>, :abs-tol<10>;
310323
311324
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
375388
a L<Str|/type/Str> containing an infix operator, such as C<'=='>, a C<'~~'>, or a
376389
user-defined infix.
377390
391+
=for code :preamble<use Test;>
378392
cmp-ok 'my spelling is apperling', '~~', /perl/, "bad speller";
379393
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:
382396
397+
=for code :preamble<use Test;>
383398
cmp-ok <a b c>, &[!eqv], <b d e>, 'not equal';
384399
385400
A L<Callable|/type/Callable> C<$comparison> lets you use custom comparisons:
386401
402+
=for code :preamble<use Test;>
387403
sub my-comp { $^a / $^b < rand };
388404
cmp-ok 1, &my-comp, 2, 'the dice giveth and the dice taketh away'
389-
390405
cmp-ok 2, -> $a, $b { $a.is-prime and $b.is-prime and $a < $b }, 7,
391406
'we got primes, one larger than the other!';
392407
@@ -401,12 +416,14 @@ given C<$expected-type>. For convenience, types may also be specified as a
401416
string. The function accepts an optional description of the test, which
402417
defaults to a string that describes the object.
403418
419+
=begin code :preamble<use Test;>
404420
class Womble {}
405421
class GreatUncleBulgaria is Womble {}
406422
my $womble = GreatUncleBulgaria.new;
407423
408424
isa-ok $womble, Womble, "Great Uncle Bulgaria is a womble";
409425
isa-ok $womble, 'Womble'; # equivalent
426+
=end code
410427
411428
=head2 sub can-ok
412429
@@ -418,6 +435,7 @@ Marks a test as passed if the given C<$variable> can run the given
418435
C<$method-name>. The function accepts an optional description. For
419436
instance:
420437
438+
=begin code :preamble<use Test;>
421439
class Womble {};
422440
my $womble = Womble.new;
423441
@@ -428,6 +446,7 @@ instance:
428446
# with human-generated test description
429447
can-ok $womble, 'collect-rubbish', "Wombles can collect rubbish";
430448
# => Wombles can collect rubbish
449+
=end code
431450
432451
=head2 sub does-ok
433452
@@ -438,6 +457,7 @@ Defined as:
438457
Marks a test as passed if the given C<$variable> can do the given C<$role>.
439458
The function accepts an optional description of the test.
440459
460+
=begin code :preamble<use Test;>
441461
# create a Womble who can invent
442462
role Invent {
443463
method brainstorm { say "Aha!" }
@@ -456,7 +476,7 @@ The function accepts an optional description of the test.
456476
457477
does-ok $tobermory, Invent, "Tobermory can invent";
458478
# => Tobermory can invent
459-
479+
=end code
460480
461481
=head2 sub like
462482
@@ -466,6 +486,7 @@ Defined as:
466486
467487
Use it this way:
468488
489+
=for code :preamble<use Test;>
469490
like 'foo', /fo/, 'foo looks like fo';
470491
471492
Marks a test as passed if the C<$value>, when coerced to a string, matches the
@@ -480,6 +501,7 @@ Defined as:
480501
481502
Used this way:
482503
504+
=for code :preamble<use Test;>
483505
unlike 'foo', /bar/, 'foo does not look like bar';
484506
485507
Marks a test as passed if the C<$value>, when coerced to a string, does B<not>
@@ -494,6 +516,7 @@ Defined as:
494516
495517
Marks a test as passed if the given C<$module> loads correctly.
496518
519+
=for code :preamble<use Test;>
497520
use-ok 'Full::Qualified::ModuleName';
498521
499522
=head2 sub dies-ok
@@ -506,7 +529,7 @@ Marks a test as passed if the given C<$code> throws an exception.
506529
507530
The function accepts an optional description of the test.
508531
509-
=begin code
532+
=begin code :preamble<use Test;>
510533
sub saruman(Bool :$ents-destroy-isengard) {
511534
die "Killed by Wormtongue" if $ents-destroy-isengard;
512535
}
@@ -525,7 +548,7 @@ exception.
525548
526549
The function accepts an optional description of the test.
527550
528-
=begin code
551+
=begin code :preamble<use Test;>
529552
sub frodo(Bool :$destroys-ring) {
530553
die "Oops, that wasn't supposed to happen" unless $destroys-ring;
531554
}
@@ -544,7 +567,7 @@ C<eval>ed as code.
544567
545568
The function accepts an optional description of the test.
546569
547-
=begin code
570+
=begin code :preamble<use Test;>
548571
eval-dies-ok q[my $joffrey = "nasty";
549572
die "bye bye Ned" if $joffrey ~~ /nasty/],
550573
"Ned Stark dies";
@@ -561,7 +584,7 @@ exception when C<eval>ed as code.
561584
562585
The function accepts an optional description of the test.
563586
564-
=begin code
587+
=begin code :preamble<use Test;>
565588
eval-lives-ok q[my $daenerys-burns = False;
566589
die "Oops, Khaleesi now ashes" if $daenerys-burns],
567590
"Dany is blood of the dragon";
@@ -582,7 +605,7 @@ If an exception was thrown, it will also try to match the matcher hash,
582605
where the key is the name of the method to be called on the exception, and
583606
the value is the value it should have to pass. For example:
584607
585-
=begin code
608+
=begin code :preamble<use Test;>
586609
sub frodo(Bool :$destroys-ring) {
587610
fail "Oops. Frodo dies" unless $destroys-ring
588611
};
@@ -598,7 +621,7 @@ the tested code does not sink the possible L<Failures|/type/Failure>. If you
598621
wish to test that the code returns a L<Failure|/type/Failure> instead of
599622
throwing, use C<fails-like> routine instead.
600623
601-
=begin code
624+
=begin code :preamble<use Test;>
602625
sub fails-not-throws { +"a" }
603626
# test passes, even though it's just a Failure and would not always throw:
604627
throws-like { fails-not-throws }, Exception;
@@ -617,14 +640,16 @@ encapsulate your string with a block and an EVAL instead. For instance:
617640
618641
Defined as:
619642
620-
=for code
643+
=for code :preamble<use Test;>
644+
621645
sub fails-like ( \test where Callable:D|Str:D, $ex-type, $reason?, *%matcher)
622646
623647
Same interface as C<throws-like>, except checks that the code returns a
624648
L<Failure|/type/Failure> instead of throwing. If the code does throw or if the
625649
returned L<Failure|/type/Failure> has already been L<handled|/routine/handled>,
626650
that will be considered as a failed test.
627651
652+
=for code :preamble<use Test;>
628653
fails-like { +"a" }, X::Str::Numeric,
629654
:message(/'Cannot convert string to number'/),
630655
'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
643668
test only if B<all> tests in the block pass. The function accepts an
644669
optional description of the subtest.
645670
646-
=begin code
671+
=begin code :preamble<use Test;>
647672
class Womble {}
648673
649674
class GreatUncleBulgaria is Womble {
@@ -665,7 +690,7 @@ You can also place the description as the first positional argument, or use a
665690
C<Pair> with description as the key and subtest's code as the value. This can be
666691
useful for subtests with large bodies.
667692
668-
=begin code
693+
=begin code :preamble<use Test;>
669694
subtest 'A bunch of tests', {
670695
plan 42;
671696
...
@@ -725,7 +750,7 @@ Skip C<$count> tests, giving a C<$reason> as to why. By default only one
725750
test will be skipped. Use such functionality when a test (or tests) would
726751
die if run.
727752
728-
=begin code
753+
=begin code :preamble<use Test;>
729754
sub num-forward-slashes($arg) { ... } ;
730755
731756
if $*KERNEL ~~ 'linux' {
@@ -750,7 +775,7 @@ Skip the remaining tests. If the remainder of the tests in the test file
750775
would all fail due to some condition, use this function to skip them,
751776
providing an optional C<$reason> as to why.
752777
753-
=begin code
778+
=begin code :preamble<use Test;>
754779
my $location; sub womble { ... }; ...;
755780
unless $location ~~ "Wimbledon Common" {
756781
skip-rest "We can't womble, the remaining tests will fail";

0 commit comments

Comments
 (0)