Refactored native trait accessors so they are done entirely in roles.
[gitmo/Moose.git] / t / 070_native_traits / 070_trait_string.t
CommitLineData
e3c07b19 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
8b9641b8 6use lib 't/lib';
7
e7724627 8use Moose ();
2c963694 9use Moose::Util::TypeConstraints;
8b9641b8 10use NoInlineAttribute;
a28e50e4 11use Test::More;
e7724627 12use Test::Exception;
13use 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
82sub 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
e7724627 160 throws_ok { $obj->replace( {}, 'x' ) }
161 qr/The first argument passed to replace must be a string or regexp reference/,
162 'replace throws an error when the first argument is not a string or regexp';
e3c07b19 163
e7724627 164 throws_ok { $obj->replace( qr/x/, {} ) }
165 qr/The second argument passed to replace must be a string or code reference/,
166 'replace throws an error when the first argument is not a string or regexp';
e3c07b19 167
e7724627 168 $obj->_string('Moosex');
169 $obj->replace_curried;
170 is( $obj->_string, 'MooseX', 'capitalize last' );
e3c07b19 171
e7724627 172 $obj->_string('abcdef');
e3c07b19 173
e7724627 174 is_deeply(
175 [ $obj->match(qr/([az]).*([fy])/) ], [ 'a', 'f' ],
176 'match -barx against /[aq]/ returns matches'
177 );
59de9de4 178
e7724627 179 ok(
180 scalar $obj->match('b'),
181 'match with string as argument returns true'
182 );
59de9de4 183
e7724627 184 throws_ok { $obj->match }
185 qr/Cannot call match without at least 1 argument/,
186 'match throws an error when no arguments are passed';
59de9de4 187
e7724627 188 throws_ok { $obj->match( {} ) }
189 qr/The argument passed to match must be a string or regexp reference/,
190 'match throws an error when an invalid argument is passed';
59de9de4 191
e7724627 192 $obj->_string('1234');
193 ok( !$obj->match_curried, 'match_curried returns false' );
e3c07b19 194
e7724627 195 $obj->_string('one two three four');
196 ok( $obj->match_curried, 'match curried returns true' );
e3c07b19 197
e7724627 198 $obj->clear;
199 is( $obj->_string, q{}, 'clear' );
d50fc84a 200
e7724627 201 throws_ok { $obj->clear(42) }
202 qr/Cannot call clear with any arguments/,
203 'clear throws an error when an argument is passed';
d50fc84a 204
e7724627 205 $obj->_string('some long string');
206 is(
207 $obj->substr(1), 'ome long string',
208 'substr as getter with one argument'
209 );
210
211 $obj->_string('some long string');
212 is(
213 $obj->substr( 1, 3 ), 'ome',
214 'substr as getter with two arguments'
215 );
216
217 $obj->substr( 1, 3, 'ong' );
218
219 is(
220 $obj->_string, 'song long string',
221 'substr as setter with three arguments'
222 );
223
224 throws_ok { $obj->substr }
225 qr/Cannot call substr without at least 1 argument/,
226 'substr throws an error when no argumemts are passed';
227
228 throws_ok { $obj->substr( 1, 2, 3, 4 ) }
229 qr/Cannot call substr with more than 3 arguments/,
230 'substr throws an error when four argumemts are passed';
231
232 throws_ok { $obj->substr( {} ) }
233 qr/The first argument passed to substr must be an integer/,
234 'substr throws an error when first argument is not an integer';
235
236 throws_ok { $obj->substr( 1, {} ) }
237 qr/The second argument passed to substr must be a positive integer/,
238 'substr throws an error when second argument is not a positive integer';
239
240 throws_ok { $obj->substr( 1, 2, {} ) }
241 qr/The third argument passed to substr must be a string/,
242 'substr throws an error when third argument is not a string';
243
244 $obj->_string('some long string');
245
246 is(
247 $obj->substr_curried_1, 'ome long string',
248 'substr_curried_1 returns expected value'
249 );
250
251 is(
252 $obj->substr_curried_1(3), 'ome',
253 'substr_curried_1 with one argument returns expected value'
254 );
255
256 $obj->substr_curried_1( 3, 'ong' );
257
258 is(
259 $obj->_string, 'song long string',
260 'substr_curried_1 as setter with two arguments'
261 );
262
263 $obj->_string('some long string');
264
265 is(
266 $obj->substr_curried_2, 'ome',
267 'substr_curried_2 returns expected value'
268 );
269
270 $obj->substr_curried_2('ong');
271
272 is(
273 $obj->_string, 'song long string',
274 'substr_curried_2 as setter with one arguments'
275 );
276
277 $obj->_string('some long string');
278
279 $obj->substr_curried_3;
280
281 is(
282 $obj->_string, 'song long string',
283 'substr_curried_3 as setter'
284 );
69048e4e 285
286 if ( $class->meta->get_attribute('_string')->is_lazy ) {
287 my $obj = $class->new;
288
289 $obj->append('foo');
290
291 is(
292 $obj->_string, 'foo',
293 'append with lazy default'
294 );
295 }
e7724627 296 }
297 $class;
298}
e3c07b19 299
a28e50e4 300done_testing;