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 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';
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';
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 scalar $obj->match('b'),
181 'match with string as argument returns true'
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';
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';
192 $obj->_string('1234');
193 ok( !$obj->match_curried, 'match_curried returns false' );
195 $obj->_string('one two three four');
196 ok( $obj->match_curried, 'match curried returns true' );
199 is( $obj->_string, q{}, 'clear' );
201 throws_ok { $obj->clear(42) }
202 qr/Cannot call clear with any arguments/,
203 'clear throws an error when an argument is passed';
205 $obj->_string('some long string');
207 $obj->substr(1), 'ome long string',
208 'substr as getter with one argument'
211 $obj->_string('some long string');
213 $obj->substr( 1, 3 ), 'ome',
214 'substr as getter with two arguments'
217 $obj->substr( 1, 3, 'ong' );
220 $obj->_string, 'song long string',
221 'substr as setter with three arguments'
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';
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';
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';
236 throws_ok { $obj->substr( 1, {} ) }
237 qr/The second argument passed to substr must be an integer/,
238 'substr throws an error when second argument is not an integer';
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';
244 $obj->_string('some long string');
247 $obj->substr_curried_1, 'ome long string',
248 'substr_curried_1 returns expected value'
252 $obj->substr_curried_1(3), 'ome',
253 'substr_curried_1 with one argument returns expected value'
256 $obj->substr_curried_1( 3, 'ong' );
259 $obj->_string, 'song long string',
260 'substr_curried_1 as setter with two arguments'
263 $obj->_string('some long string');
266 $obj->substr_curried_2, 'ome',
267 'substr_curried_2 returns expected value'
270 $obj->substr_curried_2('ong');
273 $obj->_string, 'song long string',
274 'substr_curried_2 as setter with one arguments'
277 $obj->_string('some long string');
279 $obj->substr_curried_3;
282 $obj->_string, 'song long string',
283 'substr_curried_3 as setter'
286 if ( $class->meta->get_attribute('_string')->is_lazy ) {
287 my $obj = $class->new;
292 $obj->_string, 'foo',
293 'append with lazy default'