Add length to String.
[gitmo/MooseX-AttributeHelpers.git] / t / 207_trait_string.t
CommitLineData
0951a229 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6c321470 6use Test::More tests => 30;
0951a229 7use Test::Moose 'does_ok';
8
9BEGIN {
6c321470 10 use_ok('MooseX::AttributeHelpers');
0951a229 11}
12
13{
14 package MyHomePage;
15 use Moose;
16
17 has 'string' => (
c1984b5c 18 traits => [qw/MooseX::AttributeHelpers::Trait::String/],
0951a229 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',
301da83f 31 substr => 'sub_string',
6c321470 32 length => 'length_string',
301da83f 33 },
34 curries => {
35 append => {exclaim => [ '!' ]},
36 replace => {capitalize_last => [ qr/(.)$/, sub { uc $1 } ]},
37 match => {invalid_number => [ qr/\D/ ]},
38 substr => {shift_chars => sub { $_[1]->($_[0], 0, $_[2], '') } },
0951a229 39 }
40 );
41}
42
43my $page = MyHomePage->new();
44isa_ok($page, 'MyHomePage');
45
46is($page->string, '', '... got the default value');
6c321470 47is($page->length_string, 0, '... length is zero');
0951a229 48
49$page->string('a');
6c321470 50is($page->length_string, 1, '... new string has length of one');
0951a229 51
6c321470 52$page->inc_string;
0951a229 53is($page->string, 'b', '... got the incremented value');
54
6c321470 55$page->inc_string;
0951a229 56is($page->string, 'c', '... got the incremented value (again)');
57
58$page->append_string("foo$/");
59is($page->string, "cfoo$/", 'appended to string');
60
61$page->chomp_string;
62is($page->string, "cfoo", 'chomped string');
63
64$page->chomp_string;
65is($page->string, "cfoo", 'chomped is noop');
66
67$page->chop_string;
68is($page->string, "cfo", 'chopped string');
69
70$page->prepend_string("bar");
71is($page->string, 'barcfo', 'prepended to string');
6c321470 72is($page->length_string, 6, 'right length');
0951a229 73
74is_deeply( [ $page->match_string(qr/([ao])/) ], [ "a" ], "match" );
75
76$page->replace_string(qr/([ao])/, sub { uc($1) });
77is($page->string, 'bArcfo', "substitution");
78
301da83f 79$page->exclaim;
80is($page->string, 'bArcfo!', 'exclaim!');
81
82is($page->sub_string(2), 'rcfo!', 'substr(offset)');
83is($page->sub_string(2, 2), 'rc', 'substr(offset, length)');
84is($page->sub_string(2, 2, ''), 'rc', 'substr(offset, length, replacement)');
85is($page->string, 'bAfo!', 'replacement got inserted');
86
87is($page->shift_chars(2), 'bA', 'curried substr');
88is($page->string, 'fo!', 'replacement got inserted');
89
90$page->string('Moosex');
91$page->capitalize_last;
92is($page->string, 'MooseX', 'capitalize last');
93
94$page->string('1234');
95ok(!$page->invalid_number, 'string "isn\'t an invalid number');
96
97$page->string('one two three four');
98ok($page->invalid_number, 'string an invalid number');
99
0951a229 100$page->clear_string;
101is($page->string, '', "clear");
102
103# check the meta ..
104
105my $string = $page->meta->get_attribute('string');
106does_ok($string, 'MooseX::AttributeHelpers::Trait::String');
107
108is($string->helper_type, 'Str', '... got the expected helper type');
109
110is($string->type_constraint->name, 'Str', '... got the expected type constraint');
111
6c321470 112is_deeply($string->provides, {
0951a229 113 inc => 'inc_string',
114 append => 'append_string',
115 prepend => 'prepend_string',
116 match => 'match_string',
117 replace => 'replace_string',
118 chop => 'chop_string',
119 chomp => 'chomp_string',
120 clear => 'clear_string',
301da83f 121 substr => 'sub_string',
6c321470 122 length => 'length_string',
0951a229 123}, '... got the right provides methods');
124