Commit | Line | Data |
e3c07b19 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
8b9641b8 |
6 | use lib 't/lib'; |
7 | |
e7724627 |
8 | use Moose (); |
2c963694 |
9 | use Moose::Util::TypeConstraints; |
8b9641b8 |
10 | use NoInlineAttribute; |
a28e50e4 |
11 | use Test::More; |
e7724627 |
12 | use Test::Exception; |
13 | use Test::Moose; |
e3c07b19 |
14 | |
e3c07b19 |
15 | { |
e7724627 |
16 | my %handles = ( |
17 | inc => 'inc', |
18 | append => 'append', |
19 | append_curried => [ append => '!' ], |
20 | prepend => 'prepend', |
21 | prepend_curried => [ prepend => '-' ], |
22 | replace => 'replace', |
23 | replace_curried => [ replace => qr/(.)$/, sub { uc $1 } ], |
24 | chop => 'chop', |
25 | chomp => 'chomp', |
26 | clear => 'clear', |
27 | match => 'match', |
28 | match_curried => [ match => qr/\D/ ], |
29 | length => 'length', |
30 | substr => 'substr', |
31 | substr_curried_1 => [ substr => (1) ], |
32 | substr_curried_2 => [ substr => ( 1, 3 ) ], |
33 | substr_curried_3 => [ substr => ( 1, 3, 'ong' ) ], |
e3c07b19 |
34 | ); |
e7724627 |
35 | |
36 | my $name = 'Foo1'; |
37 | |
38 | sub build_class { |
39 | my %attr = @_; |
40 | |
41 | my $class = Moose::Meta::Class->create( |
42 | $name++, |
43 | superclasses => ['Moose::Object'], |
44 | ); |
45 | |
8b9641b8 |
46 | my @traits = 'String'; |
47 | push @traits, 'NoInlineAttribute' |
48 | if delete $attr{no_inline}; |
49 | |
e7724627 |
50 | $class->add_attribute( |
51 | _string => ( |
8b9641b8 |
52 | traits => \@traits, |
e7724627 |
53 | is => 'rw', |
54 | isa => 'Str', |
55 | default => q{}, |
56 | handles => \%handles, |
57 | clearer => '_clear_string', |
58 | %attr, |
59 | ), |
60 | ); |
61 | |
62 | return ( $class->name, \%handles ); |
63 | } |
e3c07b19 |
64 | } |
65 | |
e7724627 |
66 | { |
67 | run_tests(build_class); |
68 | run_tests( build_class( lazy => 1, default => q{} ) ); |
cf0da4e2 |
69 | run_tests( build_class( trigger => sub { } ) ); |
8b9641b8 |
70 | run_tests( build_class( no_inline => 1 ) ); |
2c963694 |
71 | |
72 | # Will force the inlining code to check the entire hashref when it is modified. |
73 | subtype 'MyStr', as 'Str', where { 1 }; |
74 | |
75 | run_tests( build_class( isa => 'MyStr' ) ); |
76 | |
77 | coerce 'MyStr', from 'Str', via { $_ }; |
78 | |
79 | run_tests( build_class( isa => 'MyStr', coerce => 1 ) ); |
e7724627 |
80 | } |
81 | |
82 | sub run_tests { |
83 | my ( $class, $handles ) = @_; |
84 | |
85 | can_ok( $class, $_ ) for sort keys %{$handles}; |
86 | |
87 | with_immutable { |
88 | my $obj = $class->new(); |
89 | |
90 | is( $obj->length, 0, 'length returns zero' ); |
91 | |
92 | $obj->_string('a'); |
93 | is( $obj->length, 1, 'length returns 1 for new string' ); |
94 | |
95 | throws_ok { $obj->length(42) } |
96 | qr/Cannot call length with any arguments/, |
97 | 'length throws an error when an argument is passed'; |
98 | |
99 | $obj->inc; |
100 | is( $obj->_string, 'b', 'a becomes b after inc' ); |
101 | |
102 | throws_ok { $obj->inc(42) } |
103 | qr/Cannot call inc with any arguments/, |
104 | 'inc throws an error when an argument is passed'; |
105 | |
106 | $obj->append('foo'); |
107 | is( $obj->_string, 'bfoo', 'appended to the string' ); |
108 | |
109 | throws_ok { $obj->append( 'foo', 2 ) } |
110 | qr/Cannot call append with more than 1 argument/, |
111 | 'append throws an error when two arguments are passed'; |
112 | |
113 | $obj->append_curried; |
114 | is( $obj->_string, 'bfoo!', 'append_curried appended to the string' ); |
115 | |
116 | throws_ok { $obj->append_curried('foo') } |
117 | qr/Cannot call append with more than 1 argument/, |
118 | 'append_curried throws an error when two arguments are passed'; |
119 | |
120 | $obj->_string("has nl$/"); |
121 | $obj->chomp; |
122 | is( $obj->_string, 'has nl', 'chomped string' ); |
123 | |
124 | $obj->chomp; |
125 | is( |
126 | $obj->_string, 'has nl', |
127 | 'chomp is a no-op when string has no line ending' |
128 | ); |
e3c07b19 |
129 | |
e7724627 |
130 | throws_ok { $obj->chomp(42) } |
131 | qr/Cannot call chomp with any arguments/, |
132 | 'chomp throws an error when an argument is passed'; |
e3c07b19 |
133 | |
e7724627 |
134 | $obj->chop; |
135 | is( $obj->_string, 'has n', 'chopped string' ); |
e3c07b19 |
136 | |
e7724627 |
137 | throws_ok { $obj->chop(42) } |
138 | qr/Cannot call chop with any arguments/, |
139 | 'chop throws an error when an argument is passed'; |
e3c07b19 |
140 | |
e7724627 |
141 | $obj->_string('x'); |
142 | $obj->prepend('bar'); |
143 | is( $obj->_string, 'barx', 'prepended to string' ); |
e3c07b19 |
144 | |
e7724627 |
145 | $obj->prepend_curried; |
146 | is( $obj->_string, '-barx', 'prepend_curried prepended to string' ); |
e3c07b19 |
147 | |
e7724627 |
148 | $obj->replace( qr/([ao])/, sub { uc($1) } ); |
149 | is( |
150 | $obj->_string, '-bArx', |
151 | 'substitution using coderef for replacement' |
152 | ); |
e3c07b19 |
153 | |
e7724627 |
154 | $obj->replace( qr/A/, 'X' ); |
155 | is( |
156 | $obj->_string, '-bXrx', |
157 | 'substitution using string as replacement' |
158 | ); |
e3c07b19 |
159 | |
5394a1c7 |
160 | $obj->_string('foo'); |
161 | $obj->replace( qr/oo/, q{} ); |
162 | |
163 | is( $obj->_string, 'f', |
164 | 'replace accepts an empty string as second argument' ); |
165 | |
166 | $obj->replace( q{}, 'a' ); |
167 | |
168 | is( $obj->_string, 'af', |
169 | 'replace accepts an empty string as first argument' ); |
170 | |
e7724627 |
171 | throws_ok { $obj->replace( {}, 'x' ) } |
172 | qr/The first argument passed to replace must be a string or regexp reference/, |
173 | 'replace throws an error when the first argument is not a string or regexp'; |
e3c07b19 |
174 | |
e7724627 |
175 | throws_ok { $obj->replace( qr/x/, {} ) } |
176 | qr/The second argument passed to replace must be a string or code reference/, |
177 | 'replace throws an error when the first argument is not a string or regexp'; |
e3c07b19 |
178 | |
e7724627 |
179 | $obj->_string('Moosex'); |
180 | $obj->replace_curried; |
181 | is( $obj->_string, 'MooseX', 'capitalize last' ); |
e3c07b19 |
182 | |
e7724627 |
183 | $obj->_string('abcdef'); |
e3c07b19 |
184 | |
e7724627 |
185 | is_deeply( |
186 | [ $obj->match(qr/([az]).*([fy])/) ], [ 'a', 'f' ], |
187 | 'match -barx against /[aq]/ returns matches' |
188 | ); |
59de9de4 |
189 | |
5394a1c7 |
190 | is_deeply( |
191 | [ $obj->match(qr/([az]).*([fy])/) ], [ 'a', 'f' ], |
192 | 'match -barx against /[aq]/ returns matches' |
193 | ); |
194 | |
e7724627 |
195 | ok( |
196 | scalar $obj->match('b'), |
197 | 'match with string as argument returns true' |
198 | ); |
59de9de4 |
199 | |
5394a1c7 |
200 | ok( |
201 | scalar $obj->match(q{}), |
202 | 'match with empty string as argument returns true' |
203 | ); |
204 | |
e7724627 |
205 | throws_ok { $obj->match } |
206 | qr/Cannot call match without at least 1 argument/, |
207 | 'match throws an error when no arguments are passed'; |
59de9de4 |
208 | |
e7724627 |
209 | throws_ok { $obj->match( {} ) } |
210 | qr/The argument passed to match must be a string or regexp reference/, |
211 | 'match throws an error when an invalid argument is passed'; |
59de9de4 |
212 | |
e7724627 |
213 | $obj->_string('1234'); |
214 | ok( !$obj->match_curried, 'match_curried returns false' ); |
e3c07b19 |
215 | |
e7724627 |
216 | $obj->_string('one two three four'); |
217 | ok( $obj->match_curried, 'match curried returns true' ); |
e3c07b19 |
218 | |
e7724627 |
219 | $obj->clear; |
220 | is( $obj->_string, q{}, 'clear' ); |
d50fc84a |
221 | |
e7724627 |
222 | throws_ok { $obj->clear(42) } |
223 | qr/Cannot call clear with any arguments/, |
224 | 'clear throws an error when an argument is passed'; |
d50fc84a |
225 | |
e7724627 |
226 | $obj->_string('some long string'); |
227 | is( |
228 | $obj->substr(1), 'ome long string', |
229 | 'substr as getter with one argument' |
230 | ); |
231 | |
232 | $obj->_string('some long string'); |
233 | is( |
234 | $obj->substr( 1, 3 ), 'ome', |
235 | 'substr as getter with two arguments' |
236 | ); |
237 | |
238 | $obj->substr( 1, 3, 'ong' ); |
239 | |
240 | is( |
241 | $obj->_string, 'song long string', |
242 | 'substr as setter with three arguments' |
243 | ); |
244 | |
5394a1c7 |
245 | $obj->substr( 1, 3, '' ); |
246 | |
247 | is( |
248 | $obj->_string, 's long string', |
249 | 'substr as setter with three arguments, replacment is empty string' |
250 | ); |
251 | |
e7724627 |
252 | throws_ok { $obj->substr } |
253 | qr/Cannot call substr without at least 1 argument/, |
254 | 'substr throws an error when no argumemts are passed'; |
255 | |
256 | throws_ok { $obj->substr( 1, 2, 3, 4 ) } |
257 | qr/Cannot call substr with more than 3 arguments/, |
258 | 'substr throws an error when four argumemts are passed'; |
259 | |
260 | throws_ok { $obj->substr( {} ) } |
261 | qr/The first argument passed to substr must be an integer/, |
262 | 'substr throws an error when first argument is not an integer'; |
263 | |
264 | throws_ok { $obj->substr( 1, {} ) } |
88e88a7b |
265 | qr/The second argument passed to substr must be an integer/, |
266 | 'substr throws an error when second argument is not an integer'; |
e7724627 |
267 | |
268 | throws_ok { $obj->substr( 1, 2, {} ) } |
269 | qr/The third argument passed to substr must be a string/, |
270 | 'substr throws an error when third argument is not a string'; |
271 | |
272 | $obj->_string('some long string'); |
273 | |
274 | is( |
275 | $obj->substr_curried_1, 'ome long string', |
276 | 'substr_curried_1 returns expected value' |
277 | ); |
278 | |
279 | is( |
280 | $obj->substr_curried_1(3), 'ome', |
281 | 'substr_curried_1 with one argument returns expected value' |
282 | ); |
283 | |
284 | $obj->substr_curried_1( 3, 'ong' ); |
285 | |
286 | is( |
287 | $obj->_string, 'song long string', |
288 | 'substr_curried_1 as setter with two arguments' |
289 | ); |
290 | |
291 | $obj->_string('some long string'); |
292 | |
293 | is( |
294 | $obj->substr_curried_2, 'ome', |
295 | 'substr_curried_2 returns expected value' |
296 | ); |
297 | |
298 | $obj->substr_curried_2('ong'); |
299 | |
300 | is( |
301 | $obj->_string, 'song long string', |
302 | 'substr_curried_2 as setter with one arguments' |
303 | ); |
304 | |
305 | $obj->_string('some long string'); |
306 | |
307 | $obj->substr_curried_3; |
308 | |
309 | is( |
310 | $obj->_string, 'song long string', |
311 | 'substr_curried_3 as setter' |
312 | ); |
69048e4e |
313 | |
314 | if ( $class->meta->get_attribute('_string')->is_lazy ) { |
315 | my $obj = $class->new; |
316 | |
317 | $obj->append('foo'); |
318 | |
319 | is( |
320 | $obj->_string, 'foo', |
321 | 'append with lazy default' |
322 | ); |
323 | } |
e7724627 |
324 | } |
325 | $class; |
326 | } |
e3c07b19 |
327 | |
a28e50e4 |
328 | done_testing; |