Refactored native trait accessors so they are done entirely in roles.
[gitmo/Moose.git] / t / 070_native_traits / 040_trait_counter.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use lib 't/lib';
7
8 use Moose ();
9 use Moose::Util::TypeConstraints;
10 use NoInlineAttribute;
11 use Test::Exception;
12 use Test::More;
13 use Test::Moose;
14
15 {
16     my %handles = (
17         inc_counter    => 'inc',
18         inc_counter_2  => [ inc => 2 ],
19         dec_counter    => 'dec',
20         dec_counter_2  => [ dec => 2 ],
21         reset_counter  => 'reset',
22         set_counter    => 'set',
23         set_counter_42 => [ set => 42 ],
24     );
25
26     my $name = 'Foo1';
27
28     sub build_class {
29         my %attr = @_;
30
31         my $class = Moose::Meta::Class->create(
32             $name++,
33             superclasses => ['Moose::Object'],
34         );
35
36         my @traits = 'Counter';
37         push @traits, 'NoInlineAttribute'
38             if delete $attr{no_inline};
39
40         $class->add_attribute(
41             counter => (
42                 traits  => \@traits,
43                 is      => 'ro',
44                 isa     => 'Int',
45                 default => 0,
46                 handles => \%handles,
47                 clearer => '_clear_counter',
48                 %attr,
49             ),
50         );
51
52         return ( $class->name, \%handles );
53     }
54 }
55
56 {
57     run_tests(build_class);
58     run_tests( build_class( lazy => 1 ) );
59     run_tests( build_class( trigger => sub { } ) );
60     run_tests( build_class( no_inline => 1 ) );
61
62     # Will force the inlining code to check the entire hashref when it is modified.
63     subtype 'MyInt', as 'Int', where { 1 };
64
65     run_tests( build_class( isa => 'MyInt' ) );
66
67     coerce 'MyInt', from 'Int', via { $_ };
68
69     run_tests( build_class( isa => 'MyInt', coerce => 1 ) );
70 }
71
72 sub run_tests {
73     my ( $class, $handles ) = @_;
74
75     can_ok( $class, $_ ) for sort keys %{$handles};
76
77     with_immutable {
78         my $obj = $class->new();
79
80         is( $obj->counter, 0, '... got the default value' );
81
82         $obj->inc_counter;
83         is( $obj->counter, 1, '... got the incremented value' );
84
85         $obj->inc_counter;
86         is( $obj->counter, 2, '... got the incremented value (again)' );
87
88         throws_ok { $obj->inc_counter( 1, 2 ) }
89         qr/Cannot call inc with more than 1 argument/,
90             'inc throws an error when two arguments are passed';
91
92         $obj->dec_counter;
93         is( $obj->counter, 1, '... got the decremented value' );
94
95         throws_ok { $obj->dec_counter( 1, 2 ) }
96         qr/Cannot call dec with more than 1 argument/,
97             'dec throws an error when two arguments are passed';
98
99         $obj->reset_counter;
100         is( $obj->counter, 0, '... got the original value' );
101
102         throws_ok { $obj->reset_counter(2) }
103         qr/Cannot call reset with any arguments/,
104             'reset throws an error when an argument is passed';
105
106         $obj->set_counter(5);
107         is( $obj->counter, 5, '... set the value' );
108
109         throws_ok { $obj->set_counter( 1, 2 ) }
110         qr/Cannot call set with more than 1 argument/,
111             'set throws an error when two arguments are passed';
112
113         $obj->inc_counter(2);
114         is( $obj->counter, 7, '... increment by arg' );
115
116         $obj->dec_counter(5);
117         is( $obj->counter, 2, '... decrement by arg' );
118
119         $obj->inc_counter_2;
120         is( $obj->counter, 4, '... curried increment' );
121
122         $obj->dec_counter_2;
123         is( $obj->counter, 2, '... curried deccrement' );
124
125         $obj->set_counter_42;
126         is( $obj->counter, 42, '... curried set' );
127
128         if ( $class->meta->get_attribute('counter')->is_lazy ) {
129             my $obj = $class->new;
130
131             $obj->inc_counter;
132             is( $obj->counter, 1, 'inc increments - with lazy default' );
133
134             $obj->_clear_counter;
135
136             $obj->dec_counter;
137             is( $obj->counter, -1, 'dec decrements - with lazy default' );
138         }
139     }
140     $class;
141 }
142
143 done_testing;