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