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