Commit | Line | Data |
9256ec21 |
1 | use strict; |
2 | use warnings FATAL => 'all'; |
3 | use Test::More; |
4 | |
5 | BEGIN { |
6 | package MyRole; |
7 | use Role::Tiny; |
8 | |
9 | sub as_string { "welp" } |
10 | sub as_num { 219 } |
11 | use overload |
12 | '""' => \&as_string, |
13 | '0+' => 'as_num', |
14 | bool => sub(){1}, |
15 | fallback => 1; |
16 | } |
17 | |
18 | BEGIN { |
19 | package MyClass; |
20 | use Role::Tiny::With; |
21 | with 'MyRole'; |
22 | sub new { bless {}, shift } |
23 | } |
24 | |
83a5e1e3 |
25 | BEGIN { |
26 | package MyClass2; |
27 | use overload fallback => 0; |
28 | use Role::Tiny::With; |
29 | with 'MyRole'; |
30 | sub new { bless {}, shift } |
31 | } |
32 | |
9256ec21 |
33 | my $o = MyClass->new; |
34 | is "$o", 'welp', 'subref overload'; |
35 | is 0+$o, 219, 'method name overload'; |
36 | ok !!$o, 'anon subref overload'; |
37 | |
83a5e1e3 |
38 | my $o2 = MyClass2->new; |
39 | eval { my $f = 0+$o2 }; |
40 | like $@, qr/no method found/, 'fallback value not overwritten'; |
41 | |
9256ec21 |
42 | done_testing; |