6 use Test::More tests => 27;
7 use Test::Moose 'does_ok';
10 use_ok('MooseX::AttributeHelpers');
18 traits => [qw/MooseX::AttributeHelpers::Trait::String/],
21 default => sub { '' },
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',
31 substr => 'sub_string',
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], '') } },
42 my $page = MyHomePage->new();
43 isa_ok($page, 'MyHomePage');
45 is($page->string, '', '... got the default value');
50 is($page->string, 'b', '... got the incremented value');
53 is($page->string, 'c', '... got the incremented value (again)');
55 $page->append_string("foo$/");
56 is($page->string, "cfoo$/", 'appended to string');
59 is($page->string, "cfoo", 'chomped string');
62 is($page->string, "cfoo", 'chomped is noop');
65 is($page->string, "cfo", 'chopped string');
67 $page->prepend_string("bar");
68 is($page->string, 'barcfo', 'prepended to string');
70 is_deeply( [ $page->match_string(qr/([ao])/) ], [ "a" ], "match" );
72 $page->replace_string(qr/([ao])/, sub { uc($1) });
73 is($page->string, 'bArcfo', "substitution");
76 is($page->string, 'bArcfo!', 'exclaim!');
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');
83 is($page->shift_chars(2), 'bA', 'curried substr');
84 is($page->string, 'fo!', 'replacement got inserted');
86 $page->string('Moosex');
87 $page->capitalize_last;
88 is($page->string, 'MooseX', 'capitalize last');
90 $page->string('1234');
91 ok(!$page->invalid_number, 'string "isn\'t an invalid number');
93 $page->string('one two three four');
94 ok($page->invalid_number, 'string an invalid number');
97 is($page->string, '', "clear");
101 my $string = $page->meta->get_attribute('string');
102 does_ok($string, 'MooseX::AttributeHelpers::Trait::String');
104 is($string->helper_type, 'Str', '... got the expected helper type');
106 is($string->type_constraint->name, 'Str', '... got the expected type constraint');
108 is_deeply($string->provides, {
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',
117 substr => 'sub_string',
118 }, '... got the right provides methods');