Fix native methods which accept string to accept the empty string
[gitmo/Moose.git] / t / 070_native_traits / 070_trait_string.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use lib 't/lib';
7
8 use Moose ();
9 use Moose::Util::TypeConstraints;
10 use NoInlineAttribute;
11 use Test::More;
12 use Test::Exception;
13 use Test::Moose;
14
15 {
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' ) ],
34     );
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
46         my @traits = 'String';
47         push @traits, 'NoInlineAttribute'
48             if delete $attr{no_inline};
49
50         $class->add_attribute(
51             _string => (
52                 traits  => \@traits,
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     }
64 }
65
66 {
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 ) );
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 ) );
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         );
129
130         throws_ok { $obj->chomp(42) }
131         qr/Cannot call chomp with any arguments/,
132             'chomp throws an error when an argument is passed';
133
134         $obj->chop;
135         is( $obj->_string, 'has n', 'chopped string' );
136
137         throws_ok { $obj->chop(42) }
138         qr/Cannot call chop with any arguments/,
139             'chop throws an error when an argument is passed';
140
141         $obj->_string('x');
142         $obj->prepend('bar');
143         is( $obj->_string, 'barx', 'prepended to string' );
144
145         $obj->prepend_curried;
146         is( $obj->_string, '-barx', 'prepend_curried prepended to string' );
147
148         $obj->replace( qr/([ao])/, sub { uc($1) } );
149         is(
150             $obj->_string, '-bArx',
151             'substitution using coderef for replacement'
152         );
153
154         $obj->replace( qr/A/, 'X' );
155         is(
156             $obj->_string, '-bXrx',
157             'substitution using string as replacement'
158         );
159
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
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';
174
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';
178
179         $obj->_string('Moosex');
180         $obj->replace_curried;
181         is( $obj->_string, 'MooseX', 'capitalize last' );
182
183         $obj->_string('abcdef');
184
185         is_deeply(
186             [ $obj->match(qr/([az]).*([fy])/) ], [ 'a', 'f' ],
187             'match -barx against /[aq]/ returns matches'
188         );
189
190         is_deeply(
191             [ $obj->match(qr/([az]).*([fy])/) ], [ 'a', 'f' ],
192             'match -barx against /[aq]/ returns matches'
193         );
194
195         ok(
196             scalar $obj->match('b'),
197             'match with string as argument returns true'
198         );
199
200         ok(
201             scalar $obj->match(q{}),
202             'match with empty string as argument returns true'
203         );
204
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';
208
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';
212
213         $obj->_string('1234');
214         ok( !$obj->match_curried, 'match_curried returns false' );
215
216         $obj->_string('one two three four');
217         ok( $obj->match_curried, 'match curried returns true' );
218
219         $obj->clear;
220         is( $obj->_string, q{}, 'clear' );
221
222         throws_ok { $obj->clear(42) }
223         qr/Cannot call clear with any arguments/,
224             'clear throws an error when an argument is passed';
225
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
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
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, {} ) }
265         qr/The second argument passed to substr must be an integer/,
266             'substr throws an error when second argument is not an integer';
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         );
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         }
324     }
325     $class;
326 }
327
328 done_testing;