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