make the test style match the rest of the (modern) Moose tests
[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 => 20;
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 =>
34                 [ 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
45 $page->string('a');
46
47 $page->inc_string;
48 is( $page->string, 'b', '... got the incremented value' );
49
50 $page->inc_string;
51 is( $page->string, 'c', '... got the incremented value (again)' );
52
53 $page->append_string("foo$/");
54 is( $page->string, "cfoo$/", 'appended to string' );
55
56 $page->chomp_string;
57 is( $page->string, "cfoo", 'chomped string' );
58
59 $page->chomp_string;
60 is( $page->string, "cfoo", 'chomped is noop' );
61
62 $page->chop_string;
63 is( $page->string, "cfo", 'chopped string' );
64
65 $page->prepend_string("bar");
66 is( $page->string, 'barcfo', 'prepended to string' );
67
68 is_deeply( [ $page->match_string(qr/([ao])/) ], ["a"], "match" );
69
70 $page->replace_string( qr/([ao])/, sub { uc($1) } );
71 is( $page->string, 'bArcfo', "substitution" );
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::AttributeHelpers::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         exclaim         => [ append => ['!'] ],
111         capitalize_last => [ replace => [ qr/(.)$/, $uc ] ],
112         invalid_number => [ match => [qr/\D/] ],
113     },
114     '... got the right handles methods'
115 );
116