Commit | Line | Data |
e3c07b19 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
f1ae167f |
6 | use Moose (); |
7 | use Test::Exception; |
a28e50e4 |
8 | use Test::More; |
e3c07b19 |
9 | use Test::Moose; |
10 | |
e3c07b19 |
11 | { |
f1ae167f |
12 | my %handles = ( |
13 | abs => 'abs', |
14 | add => 'add', |
15 | inc => [ add => 1 ], |
16 | div => 'div', |
17 | cut_in_half => [ div => 2 ], |
18 | mod => 'mod', |
19 | odd => [ mod => 2 ], |
20 | mul => 'mul', |
21 | set => 'set', |
22 | sub => 'sub', |
23 | dec => [ sub => 1 ], |
e3c07b19 |
24 | ); |
f1ae167f |
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 | $class->add_attribute( |
37 | integer => ( |
38 | traits => ['Number'], |
39 | is => 'ro', |
40 | isa => 'Int', |
41 | default => 5, |
42 | handles => \%handles, |
43 | clearer => '_clear_integer', |
44 | %attr, |
45 | ), |
46 | ); |
47 | |
48 | return ( $class->name, \%handles ); |
49 | } |
e3c07b19 |
50 | } |
51 | |
f1ae167f |
52 | { |
53 | run_tests(build_class); |
54 | run_tests( build_class( lazy => 1 ) ); |
55 | } |
e3c07b19 |
56 | |
f1ae167f |
57 | sub run_tests { |
58 | my ( $class, $handles ) = @_; |
e3c07b19 |
59 | |
f1ae167f |
60 | can_ok( $class, $_ ) for sort keys %{$handles}; |
e3c07b19 |
61 | |
f1ae167f |
62 | with_immutable { |
63 | my $obj = $class->new; |
e3c07b19 |
64 | |
f1ae167f |
65 | is( $obj->integer, 5, 'Default to five' ); |
e3c07b19 |
66 | |
f1ae167f |
67 | $obj->add(10); |
e3c07b19 |
68 | |
f1ae167f |
69 | is( $obj->integer, 15, 'Add ten for fithteen' ); |
e3c07b19 |
70 | |
167ec185 |
71 | throws_ok { $obj->add( 10, 2 ) } |
72 | qr/Cannot call add with more than 1 argument/, |
73 | 'add throws an error when 2 arguments are passed'; |
74 | |
f1ae167f |
75 | $obj->sub(3); |
e3c07b19 |
76 | |
f1ae167f |
77 | is( $obj->integer, 12, 'Subtract three for 12' ); |
e3c07b19 |
78 | |
167ec185 |
79 | throws_ok { $obj->sub( 10, 2 ) } |
80 | qr/Cannot call sub with more than 1 argument/, |
81 | 'sub throws an error when 2 arguments are passed'; |
82 | |
f1ae167f |
83 | $obj->set(10); |
e3c07b19 |
84 | |
f1ae167f |
85 | is( $obj->integer, 10, 'Set to ten' ); |
e3c07b19 |
86 | |
167ec185 |
87 | throws_ok { $obj->set( 10, 2 ) } |
88 | qr/Cannot call set with more than 1 argument/, |
89 | 'set throws an error when 2 arguments are passed'; |
90 | |
f1ae167f |
91 | $obj->div(2); |
e3c07b19 |
92 | |
f1ae167f |
93 | is( $obj->integer, 5, 'divide by 2' ); |
e3c07b19 |
94 | |
167ec185 |
95 | throws_ok { $obj->div( 10, 2 ) } |
96 | qr/Cannot call div with more than 1 argument/, |
97 | 'div throws an error when 2 arguments are passed'; |
98 | |
f1ae167f |
99 | $obj->mul(2); |
e3c07b19 |
100 | |
f1ae167f |
101 | is( $obj->integer, 10, 'multiplied by 2' ); |
e3c07b19 |
102 | |
167ec185 |
103 | throws_ok { $obj->mul( 10, 2 ) } |
104 | qr/Cannot call mul with more than 1 argument/, |
105 | 'mul throws an error when 2 arguments are passed'; |
106 | |
f1ae167f |
107 | $obj->mod(2); |
e3c07b19 |
108 | |
f1ae167f |
109 | is( $obj->integer, 0, 'Mod by 2' ); |
e3c07b19 |
110 | |
167ec185 |
111 | throws_ok { $obj->mod( 10, 2 ) } |
112 | qr/Cannot call mod with more than 1 argument/, |
113 | 'mod throws an error when 2 arguments are passed'; |
114 | |
f1ae167f |
115 | $obj->set(7); |
e3c07b19 |
116 | |
f1ae167f |
117 | $obj->mod(5); |
e3c07b19 |
118 | |
f1ae167f |
119 | is( $obj->integer, 2, 'Mod by 5' ); |
e3c07b19 |
120 | |
f1ae167f |
121 | $obj->set(-1); |
e3c07b19 |
122 | |
f1ae167f |
123 | $obj->abs; |
59de9de4 |
124 | |
167ec185 |
125 | throws_ok { $obj->abs(10) } |
126 | qr/Cannot call abs with any arguments/, |
127 | 'abs throws an error when an argument is passed'; |
128 | |
f1ae167f |
129 | is( $obj->integer, 1, 'abs 1' ); |
59de9de4 |
130 | |
f1ae167f |
131 | $obj->set(12); |
59de9de4 |
132 | |
f1ae167f |
133 | $obj->inc; |
59de9de4 |
134 | |
f1ae167f |
135 | is( $obj->integer, 13, 'inc 12' ); |
59de9de4 |
136 | |
f1ae167f |
137 | $obj->dec; |
e3c07b19 |
138 | |
f1ae167f |
139 | is( $obj->integer, 12, 'dec 13' ); |
d50fc84a |
140 | |
f1ae167f |
141 | if ( $class->meta->get_attribute('integer')->is_lazy ) { |
142 | my $obj = $class->new; |
143 | |
144 | $obj->add(2); |
145 | |
146 | is( $obj->integer, 7, 'add with lazy default' ); |
147 | |
148 | $obj->_clear_integer; |
149 | |
150 | $obj->mod(2); |
151 | |
152 | is( $obj->integer, 1, 'mod with lazy default' ); |
153 | } |
154 | } |
155 | $class; |
156 | } |
e3c07b19 |
157 | |
a28e50e4 |
158 | done_testing; |