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