start replacing provides/curries with handles
[gitmo/Moose.git] / t / 070_attribute_helpers / 207_trait_string.t
CommitLineData
e3c07b19 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 17;
7use Test::Moose 'does_ok';
8
9BEGIN {
10 use_ok('Moose::AttributeHelpers');
11}
12
13{
14 package MyHomePage;
15 use Moose;
16
17 has 'string' => (
18 traits => [qw/String/],
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',
31 },
e3c07b19 32 );
33}
34
35my $page = MyHomePage->new();
36isa_ok($page, 'MyHomePage');
37
38is($page->string, '', '... got the default value');
39
40$page->string('a');
41
42$page->inc_string;
43is($page->string, 'b', '... got the incremented value');
44
45$page->inc_string;
46is($page->string, 'c', '... got the incremented value (again)');
47
48$page->append_string("foo$/");
49is($page->string, "cfoo$/", 'appended to string');
50
51$page->chomp_string;
52is($page->string, "cfoo", 'chomped string');
53
54$page->chomp_string;
55is($page->string, "cfoo", 'chomped is noop');
56
57$page->chop_string;
58is($page->string, "cfo", 'chopped string');
59
60$page->prepend_string("bar");
61is($page->string, 'barcfo', 'prepended to string');
62
63is_deeply( [ $page->match_string(qr/([ao])/) ], [ "a" ], "match" );
64
65$page->replace_string(qr/([ao])/, sub { uc($1) });
66is($page->string, 'bArcfo', "substitution");
67
68$page->clear_string;
69is($page->string, '', "clear");
70
71# check the meta ..
72
73my $string = $page->meta->get_attribute('string');
74does_ok($string, 'Moose::AttributeHelpers::Trait::String');
75
76is($string->helper_type, 'Str', '... got the expected helper type');
77
78is($string->type_constraint->name, 'Str', '... got the expected type constraint');
79
90f26642 80is_deeply($string->handles, {
81 inc_string => 'inc',
82 append_string => 'append',
83 prepend_string => 'prepend',
84 match_string => 'match',
85 replace_string => 'replace',
86 chop_string => 'chop',
87 chomp_string => 'chomp',
88 clear_string => 'clear',
e3c07b19 89}, '... got the right provides methods');
90