Skip to content

Commit df9a2f3

Browse files
committed
testing: be more explicit about contexts; example for is_deeply
1 parent b1abbbc commit df9a2f3

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

lib/Language/testing.pod

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ summary will not count skipped tests).
152152
=item ok($condition, $description?), nok($condition, $description?)
153153
154154
The C<ok> function marks a test as passed if the given C<$condition>
155-
evaluates to C<True>. The C<nok> function marks a test as passed if the
156-
given condition evaluates to C<False>.
155+
evaluates to C<True> in a boolean context. The C<nok> function marks
156+
a test as passed if the given condition evaluates to C<False>.
157157
158158
plan 2;
159159
@@ -166,7 +166,8 @@ given condition evaluates to C<False>.
166166
=item is($obtained, $expected, $description?)
167167
168168
Marks a test as passed if the C<$obtained> and C<$expected> values are equal
169-
(i.e., they compare as equal when using the C<'eq'> operator). The function
169+
when coerced to strings (i.e., they compare as equal when using the
170+
L<eq operator|/routine/eq>). The function
170171
accepts an optional C<$description> of the test.
171172
172173
my $pi = 3;
@@ -197,7 +198,27 @@ Marks a test as passed if the C<$obtained> and C<$expected> values are
197198
C<eqv>. This is the best way to check for equality of (deep) data
198199
structures. The function accepts an optional C<$description> of the test.
199200
200-
TODO: add an example
201+
=begin code
202+
use v6;
203+
use Test;
204+
plan 1;
205+
206+
sub count-chars(Str() $x) {
207+
my %chars;
208+
%chars{$_}++ for $x.comb;
209+
return %chars;
210+
}
211+
my %expected =
212+
f => 1,
213+
l => 1,
214+
e => 3,
215+
c => 1,
216+
;
217+
is_deeply count-chars('fleece'), %expected, 'count-chars works on "fleece"';
218+
=end code
219+
220+
Note that string comparison here would have been fragile, because hashes are
221+
not sorted, and converting a L<Hash|/type/Hash> to a string loses information.
201222
202223
=head2 Numeric comparison within a tolerance of 1e-5
203224

0 commit comments

Comments
 (0)