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