test that class overloads aren't overwritten
[gitmo/Role-Tiny.git] / t / overload.t
CommitLineData
9256ec21 1use strict;
2use warnings FATAL => 'all';
3use Test::More;
4
5BEGIN {
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
18BEGIN {
19 package MyClass;
20 use Role::Tiny::With;
21 with 'MyRole';
22 sub new { bless {}, shift }
23}
24
83a5e1e3 25BEGIN {
26 package MyClass2;
27 use overload fallback => 0;
28 use Role::Tiny::With;
29 with 'MyRole';
30 sub new { bless {}, shift }
31}
32
9256ec21 33my $o = MyClass->new;
34is "$o", 'welp', 'subref overload';
35is 0+$o, 219, 'method name overload';
36ok !!$o, 'anon subref overload';
37
83a5e1e3 38my $o2 = MyClass2->new;
39eval { my $f = 0+$o2 };
40like $@, qr/no method found/, 'fallback value not overwritten';
41
9256ec21 42done_testing;