summaryrefslogtreecommitdiff
path: root/t/09transform.t
diff options
context:
space:
mode:
Diffstat (limited to 't/09transform.t')
-rw-r--r--t/09transform.t75
1 files changed, 75 insertions, 0 deletions
diff --git a/t/09transform.t b/t/09transform.t
new file mode 100644
index 0000000..67f7e25
--- /dev/null
+++ b/t/09transform.t
@@ -0,0 +1,75 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+
+use Future;
+
+# Result transformation
+{
+ my $f1 = Future->new;
+
+ my $future = $f1->transform(
+ done => sub { result => @_ },
+ );
+
+ $f1->done( 1, 2, 3 );
+
+ is_deeply( [ $future->get ], [ result => 1, 2, 3 ], '->transform result' );
+}
+
+# Failure transformation
+{
+ my $f1 = Future->new;
+
+ my $future = $f1->transform(
+ fail => sub { "failure\n" => @_ },
+ );
+
+ $f1->fail( "something failed\n" );
+
+ is_deeply( [ $future->failure ], [ "failure\n" => "something failed\n" ], '->transform failure' );
+}
+
+# code dies
+{
+ my $f1 = Future->new;
+
+ my $future = $f1->transform(
+ done => sub { die "It fails\n" },
+ );
+
+ $f1->done;
+
+ is_deeply( [ $future->failure ], [ "It fails\n" ], '->transform catches exceptions' );
+}
+
+# Cancellation
+{
+ my $f1 = Future->new;
+
+ my $cancelled;
+ $f1->on_cancel( sub { $cancelled++ } );
+
+ my $future = $f1->transform;
+
+ $future->cancel;
+ is( $cancelled, 1, '->transform cancel' );
+}
+
+# Void context raises a warning
+{
+ my $warnings;
+ local $SIG{__WARN__} = sub { $warnings .= $_[0]; };
+
+ Future->done->transform(
+ done => sub { }
+ );
+ like( $warnings,
+ qr/^Calling ->transform in void context at /,
+ 'Warning in void context' );
+}
+
+done_testing;