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