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