Commit | Line | Data |
e3c07b19 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a5209c26 |
6 | use Test::More tests => 19; |
e3c07b19 |
7 | use Test::Moose 'does_ok'; |
8 | |
40ef30a5 |
9 | my $uc; |
e3c07b19 |
10 | { |
11 | package MyHomePage; |
12 | use Moose; |
a5209c26 |
13 | use Moose::AttributeHelpers; |
e3c07b19 |
14 | |
15 | has 'string' => ( |
0f93f5d2 |
16 | traits => ['String'], |
d50fc84a |
17 | is => 'rw', |
18 | isa => 'Str', |
19 | default => sub {''}, |
90f26642 |
20 | handles => { |
d50fc84a |
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 | }, |
e3c07b19 |
34 | ); |
35 | } |
36 | |
37 | my $page = MyHomePage->new(); |
d50fc84a |
38 | isa_ok( $page, 'MyHomePage' ); |
e3c07b19 |
39 | |
d50fc84a |
40 | is( $page->string, '', '... got the default value' ); |
e3c07b19 |
41 | |
42 | $page->string('a'); |
43 | |
44 | $page->inc_string; |
d50fc84a |
45 | is( $page->string, 'b', '... got the incremented value' ); |
e3c07b19 |
46 | |
47 | $page->inc_string; |
d50fc84a |
48 | is( $page->string, 'c', '... got the incremented value (again)' ); |
e3c07b19 |
49 | |
50 | $page->append_string("foo$/"); |
d50fc84a |
51 | is( $page->string, "cfoo$/", 'appended to string' ); |
e3c07b19 |
52 | |
53 | $page->chomp_string; |
d50fc84a |
54 | is( $page->string, "cfoo", 'chomped string' ); |
e3c07b19 |
55 | |
56 | $page->chomp_string; |
d50fc84a |
57 | is( $page->string, "cfoo", 'chomped is noop' ); |
e3c07b19 |
58 | |
59 | $page->chop_string; |
d50fc84a |
60 | is( $page->string, "cfo", 'chopped string' ); |
e3c07b19 |
61 | |
62 | $page->prepend_string("bar"); |
d50fc84a |
63 | is( $page->string, 'barcfo', 'prepended to string' ); |
e3c07b19 |
64 | |
d50fc84a |
65 | is_deeply( [ $page->match_string(qr/([ao])/) ], ["a"], "match" ); |
e3c07b19 |
66 | |
d50fc84a |
67 | $page->replace_string( qr/([ao])/, sub { uc($1) } ); |
68 | is( $page->string, 'bArcfo', "substitution" ); |
e3c07b19 |
69 | |
59de9de4 |
70 | $page->exclaim; |
d50fc84a |
71 | is( $page->string, 'bArcfo!', 'exclaim!' ); |
59de9de4 |
72 | |
59de9de4 |
73 | $page->string('Moosex'); |
74 | $page->capitalize_last; |
d50fc84a |
75 | is( $page->string, 'MooseX', 'capitalize last' ); |
59de9de4 |
76 | |
77 | $page->string('1234'); |
d50fc84a |
78 | ok( !$page->invalid_number, 'string "isn\'t an invalid number' ); |
59de9de4 |
79 | |
80 | $page->string('one two three four'); |
d50fc84a |
81 | ok( $page->invalid_number, 'string an invalid number' ); |
59de9de4 |
82 | |
e3c07b19 |
83 | $page->clear_string; |
d50fc84a |
84 | is( $page->string, '', "clear" ); |
e3c07b19 |
85 | |
86 | # check the meta .. |
87 | |
88 | my $string = $page->meta->get_attribute('string'); |
a40b446a |
89 | does_ok( $string, 'Moose::Meta::Attribute::Trait::Native::String' ); |
d50fc84a |
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 | ); |
e3c07b19 |
113 | |