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