Skip to content

Instantly share code, notes, and snippets.

@zoffixznet

zoffixznet/md.md Secret

Last active December 9, 2017 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoffixznet/2faa177a2ea0745b54d8847d6f1f8294 to your computer and use it in GitHub Desktop.
Save zoffixznet/2faa177a2ea0745b54d8847d6f1f8294 to your computer and use it in GitHub Desktop.
subtest 'tail makes use .count-only when it is implemented' => {
    plan 4;

    my $pulled = 0;
    sub make-seq ($i = 0) {
        Seq.new: class :: does Iterator {
            has $!i;
            has $!pulled;
            method !SET-SELF (\pulled, $!i) { $!pulled := pulled; self    }
            method new       (\pulled, \i) { self.bless!SET-SELF: pulled, i }
            method pull-one {
                $!pulled++;
                ++$!i ≤ 10 ?? $!i !! IterationEnd
            }
            method skip-one { ++$!i ≤ 10 ?? True !! False }
            method count-only { 0 max 10 - $!i }
        }.new: $pulled, $i
    }

    is-deeply make-seq.tail, 10, 'correct tail value';
    is-deeply $pulled,        1, 'we called .pull-one just once';

    $pulled = 0;
    is-deeply make-seq(11).tail, Nil,
        'correct tail value (when Seq got no values)';

    # Spec note: it's not a mistake to pull one time when the .count-only is
    # zero; it's simply pointless. So you should aim for a 0 in this test,
    # but 1 will get you a pass as well.
    is-deeply $pulled, 0|1, 'we did not pull (or pulled just one';
}
use strict;
use warnings;
subtest 'tail makes use .count-only when it is implemented' => {
plan 4;
my $pulled = 0;
sub make-seq ($i = 0) {
Seq.new: class :: does Iterator {
has $!i;
has $!pulled;
method !SET-SELF (\pulled, $!i) { $!pulled := pulled; self }
method new (\pulled, \i) { self.bless!SET-SELF: pulled, i }
method pull-one {
$!pulled++;
++$!i ≤ 10 ?? $!i !! IterationEnd
}
method skip-one { ++$!i ≤ 10 ?? True !! False }
method count-only { 0 max 10 - $!i }
}.new: $pulled, $i
}
is-deeply make-seq.tail, 10, 'correct tail value';
is-deeply $pulled, 1, 'we called .pull-one just once';
$pulled = 0;
is-deeply make-seq(11).tail, Nil,
'correct tail value (when Seq got no values)';
# Spec note: it's not a mistake to pull one time when the .count-only is
# zero; it's simply pointless. So you should aim for a 0 in this test,
# but 1 will get you a pass as well.
is-deeply $pulled, 0|1, 'we did not pull (or pulled just one';
}
subtest 'tail makes use .count-only when it is implemented' => {
plan 4;
my $pulled = 0;
sub make-seq ($i = 0) {
Seq.new: class :: does Iterator {
has $!i;
has $!pulled;
method !SET-SELF (\pulled, $!i) { $!pulled := pulled; self }
method new (\pulled, \i) { self.bless!SET-SELF: pulled, i }
method pull-one {
$!pulled++;
++$!i ≤ 10 ?? $!i !! IterationEnd
}
method skip-one { ++$!i ≤ 10 ?? True !! False }
method count-only { 0 max 10 - $!i }
}.new: $pulled, $i
}
is-deeply make-seq.tail, 10, 'correct tail value';
is-deeply $pulled, 1, 'we called .pull-one just once';
$pulled = 0;
is-deeply make-seq(11).tail, Nil,
'correct tail value (when Seq got no values)';
# Spec note: it's not a mistake to pull one time when the .count-only is
# zero; it's simply pointless. So you should aim for a 0 in this test,
# but 1 will get you a pass as well.
is-deeply $pulled, 0|1, 'we did not pull (or pulled just one';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment