Commit | Line | Data |
e3c07b19 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More tests => 17; |
7 | use Test::Moose 'does_ok'; |
8 | |
9 | BEGIN { |
10 | use_ok('Moose::AttributeHelpers'); |
11 | } |
12 | |
13 | { |
14 | package MyHomePage; |
15 | use Moose; |
16 | |
17 | has 'string' => ( |
18 | traits => [qw/String/], |
19 | is => 'rw', |
20 | isa => 'Str', |
21 | default => sub { '' }, |
22 | provides => { |
23 | inc => 'inc_string', |
24 | append => 'append_string', |
25 | prepend => 'prepend_string', |
26 | match => 'match_string', |
27 | replace => 'replace_string', |
28 | chop => 'chop_string', |
29 | chomp => 'chomp_string', |
30 | clear => 'clear_string', |
31 | } |
32 | ); |
33 | } |
34 | |
35 | my $page = MyHomePage->new(); |
36 | isa_ok($page, 'MyHomePage'); |
37 | |
38 | is($page->string, '', '... got the default value'); |
39 | |
40 | $page->string('a'); |
41 | |
42 | $page->inc_string; |
43 | is($page->string, 'b', '... got the incremented value'); |
44 | |
45 | $page->inc_string; |
46 | is($page->string, 'c', '... got the incremented value (again)'); |
47 | |
48 | $page->append_string("foo$/"); |
49 | is($page->string, "cfoo$/", 'appended to string'); |
50 | |
51 | $page->chomp_string; |
52 | is($page->string, "cfoo", 'chomped string'); |
53 | |
54 | $page->chomp_string; |
55 | is($page->string, "cfoo", 'chomped is noop'); |
56 | |
57 | $page->chop_string; |
58 | is($page->string, "cfo", 'chopped string'); |
59 | |
60 | $page->prepend_string("bar"); |
61 | is($page->string, 'barcfo', 'prepended to string'); |
62 | |
63 | is_deeply( [ $page->match_string(qr/([ao])/) ], [ "a" ], "match" ); |
64 | |
65 | $page->replace_string(qr/([ao])/, sub { uc($1) }); |
66 | is($page->string, 'bArcfo', "substitution"); |
67 | |
68 | $page->clear_string; |
69 | is($page->string, '', "clear"); |
70 | |
71 | # check the meta .. |
72 | |
73 | my $string = $page->meta->get_attribute('string'); |
74 | does_ok($string, 'Moose::AttributeHelpers::Trait::String'); |
75 | |
76 | is($string->helper_type, 'Str', '... got the expected helper type'); |
77 | |
78 | is($string->type_constraint->name, 'Str', '... got the expected type constraint'); |
79 | |
80 | is_deeply($string->provides, { |
81 | inc => 'inc_string', |
82 | append => 'append_string', |
83 | prepend => 'prepend_string', |
84 | match => 'match_string', |
85 | replace => 'replace_string', |
86 | chop => 'chop_string', |
87 | chomp => 'chomp_string', |
88 | clear => 'clear_string', |
89 | }, '... got the right provides methods'); |
90 | |