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