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