1use 5.014;
2
3use strict;
4use warnings;
5use routines;
6
7use Test::Auto;
8use Test::More;
9
10=name
11
12Data::Object::Role::Throwable
13
14=cut
15
16=abstract
17
18Throwable Role for Perl 5
19
20=cut
21
22=includes
23
24method: throw
25
26=cut
27
28=synopsis
29
30  package Example;
31
32  use Moo;
33
34  with 'Data::Object::Role::Throwable';
35
36  package main;
37
38  my $example = Example->new;
39
40  # $example->throw
41
42=cut
43
44=description
45
46This package provides mechanisms for throwing the object as an exception.
47
48=cut
49
50=method throw
51
52The throw method throws an exception with the object and the given message. The
53object is thrown as the exception context. See L<Data::Object::Exception> for
54more information.
55
56=signature throw
57
58throw(Tuple[Str, Str] | Str $message, Maybe[Number] $offset) : Any
59
60=example-1 throw
61
62  # given: synopsis
63
64  $example->throw('Oops!');
65
66=example-2 throw
67
68  # given: synopsis
69
70  $example->throw(['E001', 'Oops!']);
71
72=cut
73
74package main;
75
76my $test = testauto(__FILE__);
77
78my $subs = $test->standard;
79
80$subs->synopsis(fun($tryable) {
81  ok my $result = $tryable->result;
82
83  $result
84});
85
86$subs->example(-1, 'throw', 'method', fun($tryable) {
87  my $died = 0;
88
89  $tryable->default(fun($error) {
90    $died++;
91    $error
92  });
93
94  ok my $result = $tryable->result;
95  ok $result->isa('Data::Object::Exception');
96  ok $result->context->isa('Example');
97  is $result->message, 'Oops!';
98  ok !$result->id;
99  is $died, 1;
100
101  $result
102});
103
104$subs->example(-2, 'throw', 'method', fun($tryable) {
105  my $died = 0;
106
107  $tryable->default(fun($error) {
108    $died++;
109    $error
110  });
111
112  ok my $result = $tryable->result;
113  ok $result->isa('Data::Object::Exception');
114  ok $result->context->isa('Example');
115  is $result->message, 'Oops!';
116  is $result->id, 'E001';
117  is $died, 1;
118
119  $result
120});
121
122ok 1 and done_testing;
123