Commit | Line | Data |
b014a523 |
1 | use strictures 1; |
2 | use Test::More; |
97612fe2 |
3 | use Test::Fatal; |
b014a523 |
4 | |
5 | { |
6 | package FrewWithIsa; |
7 | use Moo::Role; |
8 | use Sub::Quote; |
9 | |
10 | has frooh => ( |
11 | is => 'rw', |
12 | isa => sub { die 'not int' unless $_[0] =~ /^\d$/ }, |
13 | ); |
14 | |
15 | has frew => ( |
16 | is => 'rw', |
17 | isa => quote_sub(q{ die 'not int' unless $_[0] =~ /^\d$/ }), |
18 | ); |
19 | |
20 | package Bar; |
21 | use Moose; |
22 | with 'FrewWithIsa'; |
0cc17078 |
23 | |
24 | package OffByOne; |
25 | use Moo::Role; |
26 | |
27 | has off_by_one => (is => 'rw', coerce => sub { $_[0] + 1 }); |
28 | |
29 | package Baz; |
30 | use Moo; |
31 | |
32 | with 'OffByOne'; |
33 | |
34 | package Quux; |
35 | use Moose; |
36 | |
37 | with 'OffByOne'; |
38 | |
39 | __PACKAGE__->meta->make_immutable; |
b014a523 |
40 | } |
41 | |
97612fe2 |
42 | is(exception { |
b014a523 |
43 | Bar->new(frooh => 1, frew => 1); |
97612fe2 |
44 | }, undef, 'creation of valid Bar'); |
b014a523 |
45 | |
97612fe2 |
46 | ok exception { |
b014a523 |
47 | Bar->new(frooh => 'silly', frew => 1); |
97612fe2 |
48 | }, 'creation of invalid Bar validated by coderef'; |
b014a523 |
49 | |
97612fe2 |
50 | ok exception { |
b014a523 |
51 | Bar->new(frooh => 1, frew => 'goose'); |
97612fe2 |
52 | }, 'creation of invalid Bar validated by quoted sub'; |
b014a523 |
53 | |
0cc17078 |
54 | sub test_off_by_one { |
55 | my ($class, $type) = @_; |
56 | |
57 | my $obo = $class->new(off_by_one => 1); |
58 | |
59 | is($obo->off_by_one, 2, "Off by one (new) ($type)"); |
60 | |
61 | $obo->off_by_one(41); |
62 | |
63 | is($obo->off_by_one, 42, "Off by one (set) ($type)"); |
64 | } |
65 | |
66 | test_off_by_one('Baz', 'Moo'); |
67 | test_off_by_one('Quux', 'Moose'); |
68 | |
b014a523 |
69 | done_testing; |