Regenerate test files
[gitmo/Mouse.git] / t / 070_native_traits / 207_trait_string.t
CommitLineData
fde8e43f 1#!/usr/bin/perl
2# This is automatically generated by author/import-moose-test.pl.
3# DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4use t::lib::MooseCompat;
5
6use strict;
7use warnings;
8
9use Test::More;
10$TODO = q{Mouse is not yet completed};
11use Test::Mouse 'does_ok';
12
13my $uc;
14{
15 package MyHomePage;
16 use Mouse;
17
18 has 'string' => (
19 traits => ['String'],
20 is => 'rw',
21 isa => 'Str',
22 default => sub {''},
23 handles => {
24 inc_string => 'inc',
25 append_string => 'append',
26 prepend_string => 'prepend',
27 match_string => 'match',
28 replace_string => 'replace',
29 chop_string => 'chop',
30 chomp_string => 'chomp',
31 clear_string => 'clear',
32 length_string => 'length',
33 exclaim => [ append => '!' ],
34 capitalize_last => [ replace => qr/(.)$/, ($uc = sub { uc $1 }) ],
35 invalid_number => [ match => qr/\D/ ],
36 },
37 );
38}
39
40my $page = MyHomePage->new();
41isa_ok( $page, 'MyHomePage' );
42
43is( $page->string, '', '... got the default value' );
44is( $page->length_string, 0,'... length is zero' );
45
46$page->string('a');
47is( $page->length_string, 1,'... new string has length of one' );
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" );
74is( $page->length_string, 6, 'right length' );
75
76$page->exclaim;
77is( $page->string, 'bArcfo!', 'exclaim!' );
78
79$page->string('Moosex');
80$page->capitalize_last;
81is( $page->string, 'MooseX', 'capitalize last' );
82
83$page->string('1234');
84ok( !$page->invalid_number, 'string "isn\'t an invalid number' );
85
86$page->string('one two three four');
87ok( $page->invalid_number, 'string an invalid number' );
88
89$page->clear_string;
90is( $page->string, '', "clear" );
91
92# check the meta ..
93
94my $string = $page->meta->get_attribute('string');
95does_ok( $string, 'Mouse::Meta::Attribute::Native::Trait::String' );
96
97is(
98 $string->type_constraint->name, 'Str',
99 '... got the expected type constraint'
100);
101
102is_deeply(
103 $string->handles,
104 {
105 inc_string => 'inc',
106 append_string => 'append',
107 prepend_string => 'prepend',
108 match_string => 'match',
109 replace_string => 'replace',
110 chop_string => 'chop',
111 chomp_string => 'chomp',
112 clear_string => 'clear',
113 length_string => 'length',
114 exclaim => [ append => '!' ],
115 capitalize_last => [ replace => qr/(.)$/, $uc ],
116 invalid_number => [ match => qr/\D/ ],
117 },
118 '... got the right handles methods'
119);
120
121done_testing;