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