6 use Test::More tests => 27;
9 use_ok('Moose::AttributeHelpers');
17 metaclass => 'String',
20 default => sub { '' },
23 append => 'append_string',
24 prepend => 'prepend_string',
25 match => 'match_string',
26 replace => 'replace_string',
27 chop => 'chop_string',
28 chomp => 'chomp_string',
29 clear => 'clear_string',
30 substr => 'sub_string',
33 append => {exclaim => [ '!' ]},
34 replace => {capitalize_last => [ qr/(.)$/, sub { uc $1 } ]},
35 match => {invalid_number => [ qr/\D/ ]},
36 substr => {shift_chars => sub { $_[1]->($_[0], 0, $_[2], '') } },
41 my $page = MyHomePage->new();
42 isa_ok($page, 'MyHomePage');
44 is($page->string, '', '... got the default value');
49 is($page->string, 'b', '... got the incremented value');
52 is($page->string, 'c', '... got the incremented value (again)');
54 $page->append_string("foo$/");
55 is($page->string, "cfoo$/", 'appended to string');
58 is($page->string, "cfoo", 'chomped string');
61 is($page->string, "cfoo", 'chomped is noop');
64 is($page->string, "cfo", 'chopped string');
66 $page->prepend_string("bar");
67 is($page->string, 'barcfo', 'prepended to string');
69 is_deeply( [ $page->match_string(qr/([ao])/) ], [ "a" ], "match" );
71 $page->replace_string(qr/([ao])/, sub { uc($1) });
72 is($page->string, 'bArcfo', "substitution");
75 is($page->string, 'bArcfo!', 'exclaim!');
77 is($page->sub_string(2), 'rcfo!', 'substr(offset)');
78 is($page->sub_string(2, 2), 'rc', 'substr(offset, length)');
79 is($page->sub_string(2, 2, ''), 'rc', 'substr(offset, length, replacement)');
80 is($page->string, 'bAfo!', 'replacement got inserted');
82 is($page->shift_chars(2), 'bA', 'curried substr');
83 is($page->string, 'fo!', 'replacement got inserted');
85 $page->string('Moosex');
86 $page->capitalize_last;
87 is($page->string, 'MooseX', 'capitalize last');
89 $page->string('1234');
90 ok(!$page->invalid_number, 'string "isn\'t an invalid number');
92 $page->string('one two three four');
93 ok($page->invalid_number, 'string an invalid number');
96 is($page->string, '', "clear");
100 my $string = $page->meta->get_attribute('string');
101 isa_ok($string, 'Moose::AttributeHelpers::String');
103 is($string->helper_type, 'Str', '... got the expected helper type');
105 is($string->type_constraint->name, 'Str', '... got the expected type constraint');
107 is_deeply($string->provides, {
109 append => 'append_string',
110 prepend => 'prepend_string',
111 match => 'match_string',
112 replace => 'replace_string',
113 chop => 'chop_string',
114 chomp => 'chomp_string',
115 clear => 'clear_string',
116 substr => 'sub_string',
117 }, '... got the right provides methods');