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