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