Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Expand/Clarify/Reword cmp-ok section
  • Loading branch information
zoffixznet committed Jun 17, 2016
1 parent 6b0b678 commit 9307750
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions doc/Language/testing.pod
Expand Up @@ -242,20 +242,25 @@ not sorted, and converting a L<Hash|/type/Hash> to a string loses information.
=item X<cmp-ok($value, $comparison, $expected, $description?)|cmp-ok>
Compares C<$value> and C<$expected> with the given C<$comparison> operator and
passes the test if the comparison yields a C<True> value. For ease of use,
operators may be passed as strings, such as C<'=='> or C<'~~'>. The function
accepts an optional C<$description> of the test.
Compares C<$value> and C<$expected> with the given C<$comparison> comparator and
passes the test if the comparison yields a C<True> value. The C<$description>
of the test is optional.
The C<$comparison> comparator can be either a L<Callable> or
a L<Str> containing a I<core> infix operator, such as C<'=='> or C<'~~'>.
cmp-ok 'my spelling is apperling', '~~', /perl/, "bad speller";
The C<$comparison> can be any L<Callable>, allowing you to use custom comparisons:
A L<Callable> C<$comparison> lets you use custom comparisons:
sub my-comp { $^a / $^b < rand };
cmp-ok 1, &my-comp, 2, 'the dice giveth and the dice taketh away'
cmp-ok 2, sub { $^a.is-prime and $^b.is-prime and $^a < $^b }, 7,
cmp-ok 2, -> $a, $b { $a.is-prime and $b.is-prime and $a < $b }, 7,
'we got primes, one larger than the other!';
B<Note:> user-defined operators cannot be passed as strings. Use
the C<&[]> notation instead:
the C<&[]> notation to create a L<Callable> for them instead:
sub infix:<◀> { $^a < $^b };
cmp-ok 4, &[◀], 5, 'comparing using my fancy operator';
Expand Down

0 comments on commit 9307750

Please sign in to comment.