redo the currying syntax to get rid of one of the arrayrefs
[gitmo/Moose.git] / t / 070_native_traits / 207_trait_string.t
CommitLineData
e3c07b19 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
8a1e47bc 6use Test::More tests => 22;
e3c07b19 7use Test::Moose 'does_ok';
8
40ef30a5 9my $uc;
e3c07b19 10{
11 package MyHomePage;
12 use Moose;
13
14 has 'string' => (
0f93f5d2 15 traits => ['String'],
d50fc84a 16 is => 'rw',
17 isa => 'Str',
18 default => sub {''},
90f26642 19 handles => {
d50fc84a 20 inc_string => 'inc',
21 append_string => 'append',
22 prepend_string => 'prepend',
23 match_string => 'match',
24 replace_string => 'replace',
25 chop_string => 'chop',
26 chomp_string => 'chomp',
27 clear_string => 'clear',
8a1e47bc 28 length_string => 'length',
3c573ca4 29 exclaim => [ append => '!' ],
30 capitalize_last => [ replace => qr/(.)$/, ($uc = sub { uc $1 }) ],
31 invalid_number => [ match => qr/\D/ ],
d50fc84a 32 },
e3c07b19 33 );
34}
35
36my $page = MyHomePage->new();
d50fc84a 37isa_ok( $page, 'MyHomePage' );
e3c07b19 38
d50fc84a 39is( $page->string, '', '... got the default value' );
8a1e47bc 40is( $page->length_string, 0,'... length is zero' );
e3c07b19 41
42$page->string('a');
8a1e47bc 43is( $page->length_string, 1,'... new string has length of one' );
e3c07b19 44
45$page->inc_string;
d50fc84a 46is( $page->string, 'b', '... got the incremented value' );
e3c07b19 47
48$page->inc_string;
d50fc84a 49is( $page->string, 'c', '... got the incremented value (again)' );
e3c07b19 50
51$page->append_string("foo$/");
d50fc84a 52is( $page->string, "cfoo$/", 'appended to string' );
e3c07b19 53
54$page->chomp_string;
d50fc84a 55is( $page->string, "cfoo", 'chomped string' );
e3c07b19 56
57$page->chomp_string;
d50fc84a 58is( $page->string, "cfoo", 'chomped is noop' );
e3c07b19 59
60$page->chop_string;
d50fc84a 61is( $page->string, "cfo", 'chopped string' );
e3c07b19 62
63$page->prepend_string("bar");
d50fc84a 64is( $page->string, 'barcfo', 'prepended to string' );
e3c07b19 65
d50fc84a 66is_deeply( [ $page->match_string(qr/([ao])/) ], ["a"], "match" );
e3c07b19 67
d50fc84a 68$page->replace_string( qr/([ao])/, sub { uc($1) } );
69is( $page->string, 'bArcfo', "substitution" );
8a1e47bc 70is( $page->length_string, 6, 'right length' );
e3c07b19 71
59de9de4 72$page->exclaim;
d50fc84a 73is( $page->string, 'bArcfo!', 'exclaim!' );
59de9de4 74
59de9de4 75$page->string('Moosex');
76$page->capitalize_last;
d50fc84a 77is( $page->string, 'MooseX', 'capitalize last' );
59de9de4 78
79$page->string('1234');
d50fc84a 80ok( !$page->invalid_number, 'string "isn\'t an invalid number' );
59de9de4 81
82$page->string('one two three four');
d50fc84a 83ok( $page->invalid_number, 'string an invalid number' );
59de9de4 84
e3c07b19 85$page->clear_string;
d50fc84a 86is( $page->string, '', "clear" );
e3c07b19 87
88# check the meta ..
89
90my $string = $page->meta->get_attribute('string');
c466e58f 91does_ok( $string, 'Moose::Meta::Attribute::Native::Trait::String' );
d50fc84a 92
93is(
94 $string->type_constraint->name, 'Str',
95 '... got the expected type constraint'
96);
97
98is_deeply(
99 $string->handles,
100 {
101 inc_string => 'inc',
102 append_string => 'append',
103 prepend_string => 'prepend',
104 match_string => 'match',
105 replace_string => 'replace',
106 chop_string => 'chop',
107 chomp_string => 'chomp',
108 clear_string => 'clear',
8a1e47bc 109 length_string => 'length',
3c573ca4 110 exclaim => [ append => '!' ],
111 capitalize_last => [ replace => qr/(.)$/, $uc ],
112 invalid_number => [ match => qr/\D/ ],
d50fc84a 113 },
114 '... got the right handles methods'
115);
e3c07b19 116