9 use Moose::Util::TypeConstraints;
10 use NoInlineAttribute;
19 append_curried => [ append => '!' ],
21 prepend_curried => [ prepend => '-' ],
23 replace_curried => [ replace => qr/(.)$/, sub { uc $1 } ],
28 match_curried => [ match => qr/\D/ ],
31 substr_curried_1 => [ substr => (1) ],
32 substr_curried_2 => [ substr => ( 1, 3 ) ],
33 substr_curried_3 => [ substr => ( 1, 3, 'ong' ) ],
41 my $class = Moose::Meta::Class->create(
43 superclasses => ['Moose::Object'],
46 my @traits = 'String';
47 push @traits, 'NoInlineAttribute'
48 if delete $attr{no_inline};
50 $class->add_attribute(
57 clearer => '_clear_string',
62 return ( $class->name, \%handles );
67 run_tests(build_class);
68 run_tests( build_class( lazy => 1, default => q{} ) );
69 run_tests( build_class( trigger => sub { } ) );
70 run_tests( build_class( no_inline => 1 ) );
72 # Will force the inlining code to check the entire hashref when it is modified.
73 subtype 'MyStr', as 'Str', where { 1 };
75 run_tests( build_class( isa => 'MyStr' ) );
77 coerce 'MyStr', from 'Str', via { $_ };
79 run_tests( build_class( isa => 'MyStr', coerce => 1 ) );
83 my ( $class, $handles ) = @_;
85 can_ok( $class, $_ ) for sort keys %{$handles};
88 my $obj = $class->new();
90 is( $obj->length, 0, 'length returns zero' );
93 is( $obj->length, 1, 'length returns 1 for new string' );
95 throws_ok { $obj->length(42) }
96 qr/Cannot call length with any arguments/,
97 'length throws an error when an argument is passed';
100 is( $obj->_string, 'b', 'a becomes b after inc' );
102 throws_ok { $obj->inc(42) }
103 qr/Cannot call inc with any arguments/,
104 'inc throws an error when an argument is passed';
107 is( $obj->_string, 'bfoo', 'appended to the string' );
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';
113 $obj->append_curried;
114 is( $obj->_string, 'bfoo!', 'append_curried appended to the string' );
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';
120 $obj->_string("has nl$/");
122 is( $obj->_string, 'has nl', 'chomped string' );
126 $obj->_string, 'has nl',
127 'chomp is a no-op when string has no line ending'
130 throws_ok { $obj->chomp(42) }
131 qr/Cannot call chomp with any arguments/,
132 'chomp throws an error when an argument is passed';
135 is( $obj->_string, 'has n', 'chopped string' );
137 throws_ok { $obj->chop(42) }
138 qr/Cannot call chop with any arguments/,
139 'chop throws an error when an argument is passed';
142 $obj->prepend('bar');
143 is( $obj->_string, 'barx', 'prepended to string' );
145 $obj->prepend_curried;
146 is( $obj->_string, '-barx', 'prepend_curried prepended to string' );
148 $obj->replace( qr/([ao])/, sub { uc($1) } );
150 $obj->_string, '-bArx',
151 'substitution using coderef for replacement'
154 $obj->replace( qr/A/, 'X' );
156 $obj->_string, '-bXrx',
157 'substitution using string as replacement'
160 $obj->_string('foo');
161 $obj->replace( qr/oo/, q{} );
163 is( $obj->_string, 'f',
164 'replace accepts an empty string as second argument' );
166 $obj->replace( q{}, 'a' );
168 is( $obj->_string, 'af',
169 'replace accepts an empty string as first argument' );
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';
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';
179 $obj->_string('Moosex');
180 $obj->replace_curried;
181 is( $obj->_string, 'MooseX', 'capitalize last' );
183 $obj->_string('abcdef');
186 [ $obj->match(qr/([az]).*([fy])/) ], [ 'a', 'f' ],
187 'match -barx against /[aq]/ returns matches'
191 [ $obj->match(qr/([az]).*([fy])/) ], [ 'a', 'f' ],
192 'match -barx against /[aq]/ returns matches'
196 scalar $obj->match('b'),
197 'match with string as argument returns true'
201 scalar $obj->match(q{}),
202 'match with empty string as argument returns true'
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';
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';
213 $obj->_string('1234');
214 ok( !$obj->match_curried, 'match_curried returns false' );
216 $obj->_string('one two three four');
217 ok( $obj->match_curried, 'match curried returns true' );
220 is( $obj->_string, q{}, 'clear' );
222 throws_ok { $obj->clear(42) }
223 qr/Cannot call clear with any arguments/,
224 'clear throws an error when an argument is passed';
226 $obj->_string('some long string');
228 $obj->substr(1), 'ome long string',
229 'substr as getter with one argument'
232 $obj->_string('some long string');
234 $obj->substr( 1, 3 ), 'ome',
235 'substr as getter with two arguments'
238 $obj->substr( 1, 3, 'ong' );
241 $obj->_string, 'song long string',
242 'substr as setter with three arguments'
245 $obj->substr( 1, 3, '' );
248 $obj->_string, 's long string',
249 'substr as setter with three arguments, replacment is empty string'
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';
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';
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';
264 throws_ok { $obj->substr( 1, {} ) }
265 qr/The second argument passed to substr must be an integer/,
266 'substr throws an error when second argument is not an integer';
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';
272 $obj->_string('some long string');
275 $obj->substr_curried_1, 'ome long string',
276 'substr_curried_1 returns expected value'
280 $obj->substr_curried_1(3), 'ome',
281 'substr_curried_1 with one argument returns expected value'
284 $obj->substr_curried_1( 3, 'ong' );
287 $obj->_string, 'song long string',
288 'substr_curried_1 as setter with two arguments'
291 $obj->_string('some long string');
294 $obj->substr_curried_2, 'ome',
295 'substr_curried_2 returns expected value'
298 $obj->substr_curried_2('ong');
301 $obj->_string, 'song long string',
302 'substr_curried_2 as setter with one arguments'
305 $obj->_string('some long string');
307 $obj->substr_curried_3;
310 $obj->_string, 'song long string',
311 'substr_curried_3 as setter'
314 if ( $class->meta->get_attribute('_string')->is_lazy ) {
315 my $obj = $class->new;
320 $obj->_string, 'foo',
321 'append with lazy default'