summaryrefslogtreecommitdiff
path: root/t/33utils-repeat-generate.t
blob: 72410fb28f3eeaccfb109c64ae1ecbba1cfd0bb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;

use Future;
use Future::Utils qw( repeat );

# generate without otherwise
{
   my $trial_f;
   my $arg;

   my $i = 0;
   my $future = repeat {
      $arg = shift;
      return $trial_f = Future->new;
   } generate => sub { $i < 3 ? ++$i : () };

   is( $arg, 1, '$arg 1 for first iteration' );
   $trial_f->done;

   ok( !$future->is_ready, '$future not ready' );

   is( $arg, 2, '$arg 2 for second iteratoin' );
   $trial_f->done( "not yet" );

   ok( !$future->is_ready, '$future still not ready' );

   is( $arg, 3, '$arg 3 for third iteration' );
   $trial_f->done( "result" );

   ok( $future->is_ready, '$future now ready' );
   is( scalar $future->get, "result", '$future->get' );
}

# generate otherwise
{
   my $last_trial_f;
   my $i = 0;
   my $future = repeat {
      Future->done( "ignore me $_[0]" );
   } generate => sub { $i < 3 ? ++$i : () },
     otherwise => sub {
        $last_trial_f = shift;
        return Future->fail( "Nothing succeeded\n" );
     };

   is( scalar $future->failure, "Nothing succeeded\n", '$future returns otherwise failure' );
   is( scalar $last_trial_f->get, "ignore me 3", '$last_trial_f->get' );

   $future = repeat {
      Future->done( "ignore me" );
   } generate => sub { () },
     otherwise => sub { Future->fail( "Nothing to do\n" ) };

   is( scalar $future->failure, "Nothing to do\n", '$future returns otherwise failure for empty generator' );
}

# Probably don't need much more testing since most combinations are test with
# foreach - while/until, die, etc..

done_testing;