Commit | Line | Data |
190b1c02 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
786dbc3d |
6 | use Test::More tests => 16; |
190b1c02 |
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 | ); |
32 | } |
33 | |
34 | my $page = MyHomePage->new(); |
35 | isa_ok($page, 'MyHomePage'); |
36 | |
37 | is($page->string, '', '... got the default value'); |
38 | |
39 | $page->string('a'); |
40 | |
41 | $page->inc_string; |
42 | is($page->string, 'b', '... got the incremented value'); |
43 | |
44 | $page->inc_string; |
45 | is($page->string, 'c', '... got the incremented value (again)'); |
46 | |
47 | $page->append_string("foo$/"); |
48 | is($page->string, "cfoo$/", 'appended to string'); |
49 | |
50 | $page->chomp_string; |
51 | is($page->string, "cfoo", 'chomped string'); |
52 | |
53 | $page->chomp_string; |
54 | is($page->string, "cfoo", 'chomped is noop'); |
55 | |
56 | $page->chop_string; |
57 | is($page->string, "cfo", 'chopped string'); |
58 | |
59 | $page->prepend_string("bar"); |
60 | is($page->string, 'barcfo', 'prepended to string'); |
61 | |
62 | is_deeply( [ $page->match_string(qr/([ao])/) ], [ "a" ], "match" ); |
63 | |
64 | $page->replace_string(qr/([ao])/, sub { uc($1) }); |
65 | is($page->string, 'bArcfo', "substitution"); |
66 | |
67 | $page->clear_string; |
68 | is($page->string, '', "clear"); |
69 | |
70 | # check the meta .. |
71 | |
72 | my $string = $page->meta->get_attribute('string'); |
73 | isa_ok($string, 'MooseX::AttributeHelpers::String'); |
74 | |
190b1c02 |
75 | is($string->type_constraint->name, 'Str', '... got the expected type constraint'); |
76 | |
77 | is_deeply($string->provides, { |
78 | inc => 'inc_string', |
79 | append => 'append_string', |
80 | prepend => 'prepend_string', |
81 | match => 'match_string', |
82 | replace => 'replace_string', |
83 | chop => 'chop_string', |
84 | chomp => 'chomp_string', |
85 | clear => 'clear_string', |
86 | }, '... got the right provides methods'); |
87 | |