File: 23exception.pl

package info (click to toggle)
libfuture-perl 0.51-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 676 kB
  • sloc: perl: 4,625; makefile: 2
file content (74 lines) | stat: -rw-r--r-- 2,150 bytes parent folder | download
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
66
67
68
69
70
71
72
73
74
use v5.10;
use strict;
use warnings;

use Test2::V0;

use Future;

# ->result throws an object
{
   my $f = Future->fail( "message\n", category => qw( a b ) );
   my $e = dies { $f->result };

   isa_ok( $e, [ "Future::Exception" ], '$e' );

   # TODO: some sort of predicate test function to check this
   is( $e->message,  "message\n", '$e->message from exceptional get' );
   is( $e->category, "category",  '$e->category from exceptional get' );
   is( [ $e->details ], [qw( a b )], '$e->details from exceptional get' );

   # Still stringifies OK
   is( "$e", "message\n", '$e stringifies properly' );

   my $f2 = $e->as_future;
   is( [ $f2->failure ],
       [ "message\n", category => qw( a b ) ],
       '$e->as_future returns a failed Future' );
}

# ->fail can accept an exception object
{
   my $e = Future::Exception->from_future(
      Future->fail( "message\n", category => qw( c d ) )
   );
   my $f = Future->fail( $e );

   is( [ $f->failure ], [ "message\n", category => qw( c d ) ],
      '->failure from Future->fail on wrapped exception' );
}

# ->fail is not confused by objects that are not Future::Exception
{
   my $e = bless [ "a", "b", "c" ], "TestException";
   my $f = Future->fail( $e );

   is( [ $f->failure ], [ $e ],
      '->failure from Future->fail on object that is not Future::Exception' );
}

# ->call can rethrow the same
{
   my $f1 = Future->fail( "message\n", category => qw( e f ) );
   my $f2 = Future->call( sub {
      $f1->result;
   });

   ok( $f2->is_failed, '$f2 failed' );
   is( [ $f2->failure ], [ "message\n", category => qw( e f ) ],
      '->failure from Future->call on rethrown failure' );
}

# Future::Exception->throw
{
   my $e = dies { Future::Exception->throw( "message\n", category => qw( g h ) ) };

   is( $e->message,  "message\n", '$e->message from F::E->throw' );
   is( $e->category, "category",  '$e->category from F::E->throw' );
   is( [ $e->details ], [qw( g h )], '$e->details from F::E->throw' );

   $e = dies { Future::Exception->throw( "short", category => ) };
   like( $e->message, qr/^short at \S+ line \d+\.$/, 'F::E->throw appends file/line' );
}

done_testing;