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 like( exception { $obj->length(42) }, qr/Cannot call length with any arguments/, 'length throws an error when an argument is passed' );
97 is( $obj->inc, 'b', 'inc returns new value' );
98 is( $obj->_string, 'b', 'a becomes b after inc' );
100 like( exception { $obj->inc(42) }, qr/Cannot call inc with any arguments/, 'inc throws an error when an argument is passed' );
102 is( $obj->append('foo'), 'bfoo', 'append returns new value' );
103 is( $obj->_string, 'bfoo', 'appended to the string' );
105 like( exception { $obj->append( 'foo', 2 ) }, qr/Cannot call append with more than 1 argument/, 'append throws an error when two arguments are passed' );
107 $obj->append_curried;
108 is( $obj->_string, 'bfoo!', 'append_curried appended to the string' );
110 like( exception { $obj->append_curried('foo') }, qr/Cannot call append with more than 1 argument/, 'append_curried throws an error when two arguments are passed' );
112 $obj->_string("has nl$/");
113 is( $obj->chomp, 1, 'chomp returns number of characters removed' );
114 is( $obj->_string, 'has nl', 'chomped string' );
116 is( $obj->chomp, 0, 'chomp returns number of characters removed' );
118 $obj->_string, 'has nl',
119 'chomp is a no-op when string has no line ending'
122 like( exception { $obj->chomp(42) }, qr/Cannot call chomp with any arguments/, 'chomp throws an error when an argument is passed' );
124 is( $obj->chop, 'l', 'chop returns character removed' );
125 is( $obj->_string, 'has n', 'chopped string' );
127 like( exception { $obj->chop(42) }, qr/Cannot call chop with any arguments/, 'chop throws an error when an argument is passed' );
130 is( $obj->prepend('bar'), 'barx', 'prepend returns new value' );
131 is( $obj->_string, 'barx', 'prepended to string' );
133 $obj->prepend_curried;
134 is( $obj->_string, '-barx', 'prepend_curried prepended to string' );
137 $obj->replace( qr/([ao])/, sub { uc($1) } ),
139 'replace returns new value'
143 $obj->_string, '-bArx',
144 'substitution using coderef for replacement'
147 $obj->replace( qr/A/, 'X' );
149 $obj->_string, '-bXrx',
150 'substitution using string as replacement'
153 $obj->_string('foo');
154 $obj->replace( qr/oo/, q{} );
156 is( $obj->_string, 'f',
157 'replace accepts an empty string as second argument' );
159 $obj->replace( q{}, 'a' );
161 is( $obj->_string, 'af',
162 'replace accepts an empty string as first argument' );
164 like( exception { $obj->replace( {}, 'x' ) }, qr/The first argument passed to replace must be a string or regexp reference/, 'replace throws an error when the first argument is not a string or regexp' );
166 like( exception { $obj->replace( qr/x/, {} ) }, qr/The second argument passed to replace must be a string or code reference/, 'replace throws an error when the first argument is not a string or regexp' );
168 $obj->_string('Moosex');
169 $obj->replace_curried;
170 is( $obj->_string, 'MooseX', 'capitalize last' );
172 $obj->_string('abcdef');
175 [ $obj->match(qr/([az]).*([fy])/) ], [ 'a', 'f' ],
176 'match -barx against /[aq]/ returns matches'
180 [ $obj->match(qr/([az]).*([fy])/) ], [ 'a', 'f' ],
181 'match -barx against /[aq]/ returns matches'
185 scalar $obj->match('b'),
186 'match with string as argument returns true'
190 scalar $obj->match(q{}),
191 'match with empty string as argument returns true'
194 like( exception { $obj->match }, qr/Cannot call match without at least 1 argument/, 'match throws an error when no arguments are passed' );
196 like( exception { $obj->match( {} ) }, qr/The argument passed to match must be a string or regexp reference/, 'match throws an error when an invalid argument is passed' );
198 $obj->_string('1234');
199 ok( !$obj->match_curried, 'match_curried returns false' );
201 $obj->_string('one two three four');
202 ok( $obj->match_curried, 'match curried returns true' );
205 is( $obj->_string, q{}, 'clear' );
207 like( exception { $obj->clear(42) }, qr/Cannot call clear with any arguments/, 'clear throws an error when an argument is passed' );
209 $obj->_string('some long string');
211 $obj->substr(1), 'ome long string',
212 'substr as getter with one argument'
215 $obj->_string('some long string');
217 $obj->substr( 1, 3 ), 'ome',
218 'substr as getter with two arguments'
222 $obj->substr( 1, 3, 'ong' ),
224 'substr as setter returns replaced string'
228 $obj->_string, 'song long string',
229 'substr as setter with three arguments'
232 $obj->substr( 1, 3, '' );
235 $obj->_string, 's long string',
236 'substr as setter with three arguments, replacment is empty string'
239 like( exception { $obj->substr }, qr/Cannot call substr without at least 1 argument/, 'substr throws an error when no argumemts are passed' );
241 like( exception { $obj->substr( 1, 2, 3, 4 ) }, qr/Cannot call substr with more than 3 arguments/, 'substr throws an error when four argumemts are passed' );
243 like( exception { $obj->substr( {} ) }, qr/The first argument passed to substr must be an integer/, 'substr throws an error when first argument is not an integer' );
245 like( exception { $obj->substr( 1, {} ) }, qr/The second argument passed to substr must be an integer/, 'substr throws an error when second argument is not an integer' );
247 like( exception { $obj->substr( 1, 2, {} ) }, qr/The third argument passed to substr must be a string/, 'substr throws an error when third argument is not a string' );
249 $obj->_string('some long string');
252 $obj->substr_curried_1, 'ome long string',
253 'substr_curried_1 returns expected value'
257 $obj->substr_curried_1(3), 'ome',
258 'substr_curried_1 with one argument returns expected value'
261 $obj->substr_curried_1( 3, 'ong' );
264 $obj->_string, 'song long string',
265 'substr_curried_1 as setter with two arguments'
268 $obj->_string('some long string');
271 $obj->substr_curried_2, 'ome',
272 'substr_curried_2 returns expected value'
275 $obj->substr_curried_2('ong');
278 $obj->_string, 'song long string',
279 'substr_curried_2 as setter with one arguments'
282 $obj->_string('some long string');
284 $obj->substr_curried_3;
287 $obj->_string, 'song long string',
288 'substr_curried_3 as setter'
291 if ( $class->meta->get_attribute('_string')->is_lazy ) {
292 my $obj = $class->new;
297 $obj->_string, 'foo',
298 'append with lazy default'