renamed helper_type => _helper_type
[gitmo/Moose.git] / t / 070_attribute_helpers / 207_trait_string.t
CommitLineData
e3c07b19 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
2e069f5a 6use Test::More tests => 20;
e3c07b19 7use Test::Moose 'does_ok';
8
9BEGIN {
a41de825 10 use_ok('Moose::AttributeHelpers');
e3c07b19 11}
12
40ef30a5 13my $uc;
e3c07b19 14{
15 package MyHomePage;
16 use Moose;
17
18 has 'string' => (
a41de825 19 traits => [qw/String/],
e3c07b19 20 is => 'rw',
21 isa => 'Str',
22 default => sub { '' },
90f26642 23 handles => {
24 inc_string => 'inc',
25 append_string => 'append',
26 prepend_string => 'prepend',
27 match_string => 'match',
28 replace_string => 'replace',
29 chop_string => 'chop',
30 chomp_string => 'chomp',
31 clear_string => 'clear',
40ef30a5 32 exclaim => [ append => [ '!' ] ],
33 capitalize_last => [ replace => [ qr/(.)$/, $uc = sub { uc $1 } ] ],
34 invalid_number => [ match => [ qr/\D/ ] ],
90f26642 35 },
e3c07b19 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
59de9de4 72$page->exclaim;
73is($page->string, 'bArcfo!', 'exclaim!');
74
59de9de4 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
e3c07b19 85$page->clear_string;
86is($page->string, '', "clear");
87
88# check the meta ..
89
90my $string = $page->meta->get_attribute('string');
a41de825 91does_ok($string, 'Moose::AttributeHelpers::Trait::String');
e3c07b19 92
e3c07b19 93is($string->type_constraint->name, 'Str', '... got the expected type constraint');
94
90f26642 95is_deeply($string->handles, {
96 inc_string => 'inc',
97 append_string => 'append',
98 prepend_string => 'prepend',
99 match_string => 'match',
100 replace_string => 'replace',
101 chop_string => 'chop',
102 chomp_string => 'chomp',
103 clear_string => 'clear',
40ef30a5 104 exclaim => [ append => [ '!' ] ],
105 capitalize_last => [ replace => [ qr/(.)$/, $uc ] ],
106 invalid_number => [ match => [ qr/\D/ ] ],
c13596ce 107}, '... got the right handles methods');
e3c07b19 108