1#!/usr/bin/perl
2
3use v5.10;
4use strict;
5use warnings;
6
7use Test::More;
8use Test::Fatal;
9
10use Future;
11
12# then done
13{
14   my $f1 = Future->new;
15
16   my $fdone;
17   my $fseq = $f1->then(
18      sub {
19         is( $_[0], "f1 result", '2-arg then done block passed result of $f1' );
20         return $fdone = Future->new;
21      },
22      sub {
23         die "then fail block should not be invoked";
24      },
25   );
26
27   $f1->done( "f1 result" );
28
29   ok( defined $fdone, '$fdone now defined after $f1 done' );
30
31   $fdone->done( results => "here" );
32
33   ok( $fseq->is_ready, '$fseq is done after $fdone done' );
34   is_deeply( [ $fseq->result ], [ results => "here" ], '$fseq->result returns results' );
35}
36
37# then fail
38{
39   my $f1 = Future->new;
40
41   my $ffail;
42   my $fseq = $f1->then(
43      sub {
44         die "then done block should not be invoked";
45      },
46      sub {
47         is( $_[0], "The failure\n", '2-arg then fail block passed failure of $f1' );
48         return $ffail = Future->new;
49      },
50   );
51
52   $f1->fail( "The failure\n" );
53
54   ok( defined $ffail, '$ffail now defined after $f1 fail' );
55
56   $ffail->done( fallback => "result" );
57
58   ok( $fseq->is_ready, '$fseq is done after $ffail fail' );
59   is_deeply( [ $fseq->result ], [ fallback => "result" ], '$fseq->result returns results' );
60}
61
62# then done fails doesn't trigger fail block
63{
64   my $f1 = Future->new;
65
66   my $fdone;
67   my $fseq = $f1->then(
68      sub {
69         $fdone = Future->new;
70      },
71      sub {
72         die "then fail block should not be invoked";
73      },
74   );
75
76   $f1->done( "Done" );
77   $fdone->fail( "The failure\n" );
78
79   ok( $fseq->is_ready, '$fseq is ready after $fdone fail' );
80   ok( scalar $fseq->failure, '$fseq failed after $fdone fail' );
81}
82
83# then_with_f
84{
85   my $f1 = Future->new;
86
87   my $fseq = $f1->then_with_f(
88      sub {
89         identical( $_[0], $f1, 'then_with_f done block passed $f1' );
90         is( $_[1], "f1 result", 'then_with_f done block passed result of $f1' );
91         Future->done;
92      },
93      sub {
94         die "then_with_f fail block should not be called";
95      },
96   );
97
98   $f1->done( "f1 result" );
99
100   ok( $fseq->is_ready, '$fseq is ready after $f1 done' );
101}
102
103done_testing;
104