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