import changes from MXAH repo
[gitmo/Moose.git] / t / 070_attribute_helpers / 207_trait_string.t
CommitLineData
e3c07b19 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
59de9de4 6use Test::More tests => 27;
e3c07b19 7use Test::Moose 'does_ok';
8
9BEGIN {
59de9de4 10 use_ok('MooseX::AttributeHelpers');
e3c07b19 11}
12
13{
14 package MyHomePage;
15 use Moose;
16
17 has 'string' => (
59de9de4 18 traits => [qw/MooseX::AttributeHelpers::Trait::String/],
e3c07b19 19 is => 'rw',
20 isa => 'Str',
21 default => sub { '' },
90f26642 22 handles => {
23 inc_string => 'inc',
24 append_string => 'append',
25 prepend_string => 'prepend',
26 match_string => 'match',
27 replace_string => 'replace',
28 chop_string => 'chop',
29 chomp_string => 'chomp',
30 clear_string => 'clear',
59de9de4 31 exclaim => { append => [ '!' ]},
32 capitalize_last => { replace => [ qr/(.)$/, sub { uc $1 } ]},
33 invalid_number => { match => [ qr/\D/ ]},
34 shift_chars => { substr => sub { $_[1]->($_[0], 0, $_[2], '') } },
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
75is($page->sub_string(2), 'rcfo!', 'substr(offset)');
76is($page->sub_string(2, 2), 'rc', 'substr(offset, length)');
77is($page->sub_string(2, 2, ''), 'rc', 'substr(offset, length, replacement)');
78is($page->string, 'bAfo!', 'replacement got inserted');
79
80is($page->shift_chars(2), 'bA', 'curried substr');
81is($page->string, 'fo!', 'replacement got inserted');
82
83$page->string('Moosex');
84$page->capitalize_last;
85is($page->string, 'MooseX', 'capitalize last');
86
87$page->string('1234');
88ok(!$page->invalid_number, 'string "isn\'t an invalid number');
89
90$page->string('one two three four');
91ok($page->invalid_number, 'string an invalid number');
92
e3c07b19 93$page->clear_string;
94is($page->string, '', "clear");
95
96# check the meta ..
97
98my $string = $page->meta->get_attribute('string');
59de9de4 99does_ok($string, 'MooseX::AttributeHelpers::Trait::String');
e3c07b19 100
101is($string->helper_type, 'Str', '... got the expected helper type');
102
103is($string->type_constraint->name, 'Str', '... got the expected type constraint');
104
90f26642 105is_deeply($string->handles, {
106 inc_string => 'inc',
107 append_string => 'append',
108 prepend_string => 'prepend',
109 match_string => 'match',
110 replace_string => 'replace',
111 chop_string => 'chop',
112 chomp_string => 'chomp',
113 clear_string => 'clear',
e3c07b19 114}, '... got the right provides methods');
115