Skip to content

Commit bb626e4

Browse files
committed
Complete documentation for Test module
1 parent cbc53e7 commit bb626e4

File tree

1 file changed

+113
-12
lines changed

1 file changed

+113
-12
lines changed

lib/Language/testing.pod

Lines changed: 113 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ test file.
6060
6161
plan 15; # expect to have run 15 tests
6262
63-
In L<subtest>s, C<plan> is used to specify the total number of tests within
63+
In C<subtest>s, C<plan> is used to specify the total number of tests within
6464
the subtest.
6565
6666
If a C<plan> is used, it is not necessary to denote the end of testing with
@@ -69,7 +69,7 @@ C<done>.
6969
=item done
7070
7171
Specify that testing has finished. Use this function when you do not as yet
72-
have a L<plan> for the number of tests to run. A C<plan> is thus not
72+
have a C<plan> for the number of tests to run. A C<plan> is thus not
7373
required when using C<done>.
7474
7575
=head1 Test functions
@@ -105,9 +105,9 @@ to provide visual markers on how the testing of a test-file is progressing
105105
=head2 Skipping tests
106106
107107
Sometimes tests just aren't ready to be run, for instance a feature might
108-
not yet be implemented, in which case tests can be marked as L<todo>. It
108+
not yet be implemented, in which case tests can be marked as C<todo>. It
109109
could be the case that a given feature only works on a particular platform.
110-
In which case one can L<skip> tests.
110+
In which case one can C<skip> tests.
111111
112112
=item todo($reason, $count = 1)
113113
@@ -189,28 +189,129 @@ strings, such as C<'=='> or C<'~~'>.
189189
190190
=head2 Structural comparison with C<< infix:<eqv> >>
191191
192-
=item is_deeply
192+
=item is_deeply($obtained, $expected, $description?)
193+
194+
Marks a test as passed if the C<$obtained> and C<$expected> values are
195+
C<eqv>. This is the best way to check for equality of (deep) data
196+
structures. The function accepts an optional C<$description> of the test.
197+
198+
TODO: add an example
193199
194200
=head2 Numeric comparison within a tolerance of 1e-5
195201
196-
=item is_approx
202+
=item is_approx($obtained, $expected, $description?)
203+
204+
Marks a test as passed if the C<$obtained> and C<$expected> numerical values
205+
are within C<1e-5> of each other. The function accepts an optional
206+
C<$description> of the test.
207+
208+
my $eulers_constant_approx = 2.71828;
209+
is_approx($eulers_constant_approx, e, "approximate Euler's constant");
197210
198211
=head2 Class membership testing
199212
200-
=item isa_ok
213+
=item isa_ok($object, $type, $description?)
214+
215+
Marks a test as passed if the given C<$object> is, or inherits from, the
216+
given C<$type>. For convenience, types may also be specified as a string.
217+
The function accepts an optional C<$description> of the test.
218+
219+
class Womble {}
220+
class GreatUncleBulgaria is Womble {}
221+
222+
my $womble = GreatUncleBulgaria.new;
223+
isa_ok($womble, Womble, "Great Uncle Bulgaria is a womble");
224+
isa_ok($womble, 'Womble'); # equivalent
201225
202226
=head2 Grouping tests
203227
204228
The result of a group of subtests is only C<ok> if all subtests are C<ok>.
205229
206-
=item subtest
230+
=item subtest(&subtests, $description?)
231+
232+
The C<subtest> function executes the given block, consisting of usually more
233+
than one test, possibly including a C<plan> or C<done>. It will pass the
234+
test only if B<all> tests in the block, pass. The function accepts an
235+
optional C<$description> of the test.
236+
237+
class Womble {}
238+
239+
class GreatUncleBulgaria is Womble {
240+
has $.location = "Wimbledon Common";
241+
has $.spectacles = True;
242+
}
243+
244+
subtest {
245+
my $womble = GreatUncleBulgaria.new;
246+
isa_ok($womble, Womble, "Great Uncle Bulgaria is a womble");
247+
is($womble->location, "Wimbledon Common", "Correct location");
248+
ok($womble->spectacles, "Wears spectacles");
249+
}, "Check Great Uncle Bulgaria";
207250
208251
=head2 Exception testing
209252
210-
=item dies_ok
211-
=item lives_ok
212-
=item eval_dies_ok
253+
=item dies_ok($code, $description?)
254+
255+
Marks the test as passed if the given C<$code> throws an exception.
256+
257+
The function accepts an optional C<$description> of the test.
258+
259+
sub saruman(Bool :$ents-destroy-isengard) {
260+
die "Killed by Wormtongue" if $ents-destroy-isengard;
261+
}
262+
263+
dies_ok { saruman(ents-destroy-isengard => True) }, "Saruman dies";
264+
265+
=item lives_ok($code, $description?)
266+
267+
Marks the test as passed if the given C<$code> B<does not> throw an
268+
exception.
269+
270+
The function accepts an optional C<$description> of the test.
271+
272+
sub frodo(Bool :$destroys-ring) {
273+
die "Oops, that wasn't supposed to happen" unless $destroys-ring;
274+
}
275+
276+
lives_ok { frodo(destroys-ring => True) }, "Frodo survivies";
277+
278+
=item eval_dies_ok($code, $description?)
279+
280+
Marks the test as passed if the given C<eval>ed C<$code> throws an
281+
exception.
282+
283+
The function accepts an optional C<$description> of the test.
284+
285+
eval_dies_ok q[my $joffrey = "nasty";
286+
die "bye bye Ned" if $joffrey ~~ /nasty/],
287+
"Ned Stark dies";
288+
213289
=item eval_lives_ok
214-
=item throws_like
290+
291+
Marks the test as passed if the given C<eval>ed C<$code> throws an
292+
exception.
293+
294+
The function accepts an optional C<$description> of the test.
295+
296+
eval_lives_ok q[my $daenerys_burns = False;
297+
die "Oops, Khaleesi now ashes" if $daenerys_burns],
298+
"Dany is blood of the dragon";
299+
300+
=item throws_like($code, $description?)
301+
302+
The C<throws_like> function checks whether the given C<$code> (specified as
303+
either something C<Callable>, or as a something to be C<EVAL>led) throws a
304+
specific exception (either specified as a Type object, or as a string). If
305+
an exception was thrown, it will also try to match the matcher hash, in
306+
which the key is the name of the method to be called on the exception, and
307+
the value is the value it should have to pass.
308+
309+
The function accepts an optional C<$description> of the test.
310+
311+
Please note that you can only use the C<EVAL> form if you are not referencing
312+
any symbols in the surrounding scope. If you are, you should encapsulate
313+
your string with a block and an EVAL. For instance:
314+
315+
throws_like { EVAL q[ fac("foo") ] }, X::TypeCheck::Argument;
215316
216317
=end pod

0 commit comments

Comments
 (0)