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