Merge branch 'master' into attribute_helpers
[gitmo/Moose.git] / t / 070_attribute_traits / 207_trait_string.t
CommitLineData
e3c07b19 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a5209c26 6use Test::More tests => 19;
e3c07b19 7use Test::Moose 'does_ok';
8
40ef30a5 9my $uc;
e3c07b19 10{
11 package MyHomePage;
12 use Moose;
13
14 has 'string' => (
0f93f5d2 15 traits => ['String'],
d50fc84a 16 is => 'rw',
17 isa => 'Str',
18 default => sub {''},
90f26642 19 handles => {
d50fc84a 20 inc_string => 'inc',
21 append_string => 'append',
22 prepend_string => 'prepend',
23 match_string => 'match',
24 replace_string => 'replace',
25 chop_string => 'chop',
26 chomp_string => 'chomp',
27 clear_string => 'clear',
28 exclaim => [ append => ['!'] ],
29 capitalize_last =>
30 [ replace => [ qr/(.)$/, $uc = sub { uc $1 } ] ],
31 invalid_number => [ match => [qr/\D/] ],
32 },
e3c07b19 33 );
34}
35
36my $page = MyHomePage->new();
d50fc84a 37isa_ok( $page, 'MyHomePage' );
e3c07b19 38
d50fc84a 39is( $page->string, '', '... got the default value' );
e3c07b19 40
41$page->string('a');
42
43$page->inc_string;
d50fc84a 44is( $page->string, 'b', '... got the incremented value' );
e3c07b19 45
46$page->inc_string;
d50fc84a 47is( $page->string, 'c', '... got the incremented value (again)' );
e3c07b19 48
49$page->append_string("foo$/");
d50fc84a 50is( $page->string, "cfoo$/", 'appended to string' );
e3c07b19 51
52$page->chomp_string;
d50fc84a 53is( $page->string, "cfoo", 'chomped string' );
e3c07b19 54
55$page->chomp_string;
d50fc84a 56is( $page->string, "cfoo", 'chomped is noop' );
e3c07b19 57
58$page->chop_string;
d50fc84a 59is( $page->string, "cfo", 'chopped string' );
e3c07b19 60
61$page->prepend_string("bar");
d50fc84a 62is( $page->string, 'barcfo', 'prepended to string' );
e3c07b19 63
d50fc84a 64is_deeply( [ $page->match_string(qr/([ao])/) ], ["a"], "match" );
e3c07b19 65
d50fc84a 66$page->replace_string( qr/([ao])/, sub { uc($1) } );
67is( $page->string, 'bArcfo', "substitution" );
e3c07b19 68
59de9de4 69$page->exclaim;
d50fc84a 70is( $page->string, 'bArcfo!', 'exclaim!' );
59de9de4 71
59de9de4 72$page->string('Moosex');
73$page->capitalize_last;
d50fc84a 74is( $page->string, 'MooseX', 'capitalize last' );
59de9de4 75
76$page->string('1234');
d50fc84a 77ok( !$page->invalid_number, 'string "isn\'t an invalid number' );
59de9de4 78
79$page->string('one two three four');
d50fc84a 80ok( $page->invalid_number, 'string an invalid number' );
59de9de4 81
e3c07b19 82$page->clear_string;
d50fc84a 83is( $page->string, '', "clear" );
e3c07b19 84
85# check the meta ..
86
87my $string = $page->meta->get_attribute('string');
a40b446a 88does_ok( $string, 'Moose::Meta::Attribute::Trait::Native::String' );
d50fc84a 89
90is(
91 $string->type_constraint->name, 'Str',
92 '... got the expected type constraint'
93);
94
95is_deeply(
96 $string->handles,
97 {
98 inc_string => 'inc',
99 append_string => 'append',
100 prepend_string => 'prepend',
101 match_string => 'match',
102 replace_string => 'replace',
103 chop_string => 'chop',
104 chomp_string => 'chomp',
105 clear_string => 'clear',
106 exclaim => [ append => ['!'] ],
107 capitalize_last => [ replace => [ qr/(.)$/, $uc ] ],
108 invalid_number => [ match => [qr/\D/] ],
109 },
110 '... got the right handles methods'
111);
e3c07b19 112