Commit | Line | Data |
e3c07b19 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
8a1e47bc |
6 | use Test::More tests => 22; |
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', |
d50fc84a |
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' ); |
8a1e47bc |
41 | is( $page->length_string, 0,'... length is zero' ); |
e3c07b19 |
42 | |
43 | $page->string('a'); |
8a1e47bc |
44 | is( $page->length_string, 1,'... new string has length of one' ); |
e3c07b19 |
45 | |
46 | $page->inc_string; |
d50fc84a |
47 | is( $page->string, 'b', '... got the incremented value' ); |
e3c07b19 |
48 | |
49 | $page->inc_string; |
d50fc84a |
50 | is( $page->string, 'c', '... got the incremented value (again)' ); |
e3c07b19 |
51 | |
52 | $page->append_string("foo$/"); |
d50fc84a |
53 | is( $page->string, "cfoo$/", 'appended to string' ); |
e3c07b19 |
54 | |
55 | $page->chomp_string; |
d50fc84a |
56 | is( $page->string, "cfoo", 'chomped string' ); |
e3c07b19 |
57 | |
58 | $page->chomp_string; |
d50fc84a |
59 | is( $page->string, "cfoo", 'chomped is noop' ); |
e3c07b19 |
60 | |
61 | $page->chop_string; |
d50fc84a |
62 | is( $page->string, "cfo", 'chopped string' ); |
e3c07b19 |
63 | |
64 | $page->prepend_string("bar"); |
d50fc84a |
65 | is( $page->string, 'barcfo', 'prepended to string' ); |
e3c07b19 |
66 | |
d50fc84a |
67 | is_deeply( [ $page->match_string(qr/([ao])/) ], ["a"], "match" ); |
e3c07b19 |
68 | |
d50fc84a |
69 | $page->replace_string( qr/([ao])/, sub { uc($1) } ); |
70 | is( $page->string, 'bArcfo', "substitution" ); |
8a1e47bc |
71 | is( $page->length_string, 6, 'right length' ); |
e3c07b19 |
72 | |
59de9de4 |
73 | $page->exclaim; |
d50fc84a |
74 | is( $page->string, 'bArcfo!', 'exclaim!' ); |
59de9de4 |
75 | |
59de9de4 |
76 | $page->string('Moosex'); |
77 | $page->capitalize_last; |
d50fc84a |
78 | is( $page->string, 'MooseX', 'capitalize last' ); |
59de9de4 |
79 | |
80 | $page->string('1234'); |
d50fc84a |
81 | ok( !$page->invalid_number, 'string "isn\'t an invalid number' ); |
59de9de4 |
82 | |
83 | $page->string('one two three four'); |
d50fc84a |
84 | ok( $page->invalid_number, 'string an invalid number' ); |
59de9de4 |
85 | |
e3c07b19 |
86 | $page->clear_string; |
d50fc84a |
87 | is( $page->string, '', "clear" ); |
e3c07b19 |
88 | |
89 | # check the meta .. |
90 | |
91 | my $string = $page->meta->get_attribute('string'); |
c466e58f |
92 | does_ok( $string, 'Moose::Meta::Attribute::Native::Trait::String' ); |
d50fc84a |
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', |
8a1e47bc |
110 | length_string => 'length', |
d50fc84a |
111 | exclaim => [ append => ['!'] ], |
112 | capitalize_last => [ replace => [ qr/(.)$/, $uc ] ], |
113 | invalid_number => [ match => [qr/\D/] ], |
114 | }, |
115 | '... got the right handles methods' |
116 | ); |
e3c07b19 |
117 | |