bd23c992f89115a8cb72eab933bbbc8bc160bd50
[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 => 21;
7 use Test::Moose 'does_ok';
8
9 BEGIN {
10     use_ok('Moose::AttributeHelpers');
11 }
12
13 my $uc;
14 {
15     package MyHomePage;
16     use Moose;
17
18     has 'string' => (
19         traits    => [qw/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                     exclaim         => [ append  => [ '!' ] ],
33                     capitalize_last => [ replace => [ qr/(.)$/, $uc = sub { uc $1 } ] ],
34                     invalid_number  => [ match   => [ qr/\D/ ] ],
35                    },
36     );
37 }
38
39 my $page = MyHomePage->new();
40 isa_ok($page, 'MyHomePage');
41
42 is($page->string, '', '... got the default value');
43
44 $page->string('a');
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
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::AttributeHelpers::Trait::String');
92
93 is($string->helper_type, 'Str', '... got the expected helper type');
94
95 is($string->type_constraint->name, 'Str', '... got the expected type constraint');
96
97 is_deeply($string->handles, {
98     inc_string     => 'inc',
99     append_string  => 'append',
100     prepend_string => 'prepend',
101     match_string   => 'match',
102     replace_string => 'replace',
103     chop_string    => 'chop',
104     chomp_string   => 'chomp',
105     clear_string   => 'clear',
106     exclaim         => [ append  => [ '!' ] ],
107     capitalize_last => [ replace => [ qr/(.)$/, $uc ] ],
108     invalid_number  => [ match   => [ qr/\D/ ] ],
109 }, '... got the right provides methods');
110