6 use Test::More tests => 30;
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',
32 length => 'length_string',
35 append => {exclaim => [ '!' ]},
36 replace => {capitalize_last => [ qr/(.)$/, sub { uc $1 } ]},
37 match => {invalid_number => [ qr/\D/ ]},
38 substr => {shift_chars => sub { $_[1]->($_[0], 0, $_[2], '') } },
43 my $page = MyHomePage->new();
44 isa_ok($page, 'MyHomePage');
46 is($page->string, '', '... got the default value');
47 is($page->length_string, 0, '... length is zero');
50 is($page->length_string, 1, '... new string has length of one');
53 is($page->string, 'b', '... got the incremented value');
56 is($page->string, 'c', '... got the incremented value (again)');
58 $page->append_string("foo$/");
59 is($page->string, "cfoo$/", 'appended to string');
62 is($page->string, "cfoo", 'chomped string');
65 is($page->string, "cfoo", 'chomped is noop');
68 is($page->string, "cfo", 'chopped string');
70 $page->prepend_string("bar");
71 is($page->string, 'barcfo', 'prepended to string');
72 is($page->length_string, 6, 'right length');
74 is_deeply( [ $page->match_string(qr/([ao])/) ], [ "a" ], "match" );
76 $page->replace_string(qr/([ao])/, sub { uc($1) });
77 is($page->string, 'bArcfo', "substitution");
80 is($page->string, 'bArcfo!', 'exclaim!');
82 is($page->sub_string(2), 'rcfo!', 'substr(offset)');
83 is($page->sub_string(2, 2), 'rc', 'substr(offset, length)');
84 is($page->sub_string(2, 2, ''), 'rc', 'substr(offset, length, replacement)');
85 is($page->string, 'bAfo!', 'replacement got inserted');
87 is($page->shift_chars(2), 'bA', 'curried substr');
88 is($page->string, 'fo!', 'replacement got inserted');
90 $page->string('Moosex');
91 $page->capitalize_last;
92 is($page->string, 'MooseX', 'capitalize last');
94 $page->string('1234');
95 ok(!$page->invalid_number, 'string "isn\'t an invalid number');
97 $page->string('one two three four');
98 ok($page->invalid_number, 'string an invalid number');
101 is($page->string, '', "clear");
105 my $string = $page->meta->get_attribute('string');
106 does_ok($string, 'MooseX::AttributeHelpers::Trait::String');
108 is($string->helper_type, 'Str', '... got the expected helper type');
110 is($string->type_constraint->name, 'Str', '... got the expected type constraint');
112 is_deeply($string->provides, {
114 append => 'append_string',
115 prepend => 'prepend_string',
116 match => 'match_string',
117 replace => 'replace_string',
118 chop => 'chop_string',
119 chomp => 'chomp_string',
120 clear => 'clear_string',
121 substr => 'sub_string',
122 length => 'length_string',
123 }, '... got the right provides methods');