File: 007_basic_string.t

package info (click to toggle)
libmoosex-attributehelpers-perl 0.23-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 584 kB
  • ctags: 329
  • sloc: perl: 3,890; makefile: 5
file content (123 lines) | stat: -rw-r--r-- 3,439 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 30;

BEGIN {
    use_ok('MooseX::AttributeHelpers');
}

{
    package MyHomePage;
    use Moose;

    has 'string' => (
        metaclass => 'String',
        is        => 'rw',
        isa       => 'Str',
        default   => sub { '' },
        provides => {
            inc     => 'inc_string',
            append  => 'append_string',
            prepend => 'prepend_string',
            match   => 'match_string',
            replace => 'replace_string',
            chop    => 'chop_string',
            chomp   => 'chomp_string',
            clear   => 'clear_string',
            substr  => 'sub_string',
            length  => 'length_string',
        },
        curries  => {
            append  => {exclaim         => [ '!' ]},
            replace => {capitalize_last => [ qr/(.)$/, sub { uc $1 } ]},
            match   => {invalid_number  => [ qr/\D/ ]},
            substr  => {shift_chars     => sub { $_[1]->($_[0], 0, $_[2], '') } },
        }
    );
}

my $page = MyHomePage->new();
isa_ok($page, 'MyHomePage');

is($page->string, '', '... got the default value');
is($page->length_string, 0, '... length is zero');

$page->string('a');
is($page->length_string, 1, '... new string has length of one');

$page->inc_string;
is($page->string, 'b', '... got the incremented value');

$page->inc_string;
is($page->string, 'c', '... got the incremented value (again)');

$page->append_string("foo$/");
is($page->string, "cfoo$/", 'appended to string');

$page->chomp_string;
is($page->string, "cfoo", 'chomped string');

$page->chomp_string;
is($page->string, "cfoo", 'chomped is noop');

$page->chop_string;
is($page->string, "cfo", 'chopped string');

$page->prepend_string("bar");
is($page->string, 'barcfo', 'prepended to string');

is_deeply( [ $page->match_string(qr/([ao])/) ], [ "a" ], "match" );

$page->replace_string(qr/([ao])/, sub { uc($1) });
is($page->string, 'bArcfo', "substitution");
is($page->length_string, 6, 'right length');

$page->exclaim;
is($page->string, 'bArcfo!', 'exclaim!');

is($page->sub_string(2), 'rcfo!', 'substr(offset)');
is($page->sub_string(2, 2), 'rc', 'substr(offset, length)');
is($page->sub_string(2, 2, ''), 'rc', 'substr(offset, length, replacement)');
is($page->string, 'bAfo!', 'replacement got inserted');

is($page->shift_chars(2), 'bA', 'curried substr');
is($page->string, 'fo!', 'replacement got inserted');

$page->string('Moosex');
$page->capitalize_last;
is($page->string, 'MooseX', 'capitalize last');

$page->string('1234');
ok(!$page->invalid_number, 'string "isn\'t an invalid number');

$page->string('one two three four');
ok($page->invalid_number, 'string an invalid number');

$page->clear_string;
is($page->string, '', "clear");

# check the meta ..

my $string = $page->meta->get_attribute('string');
isa_ok($string, 'MooseX::AttributeHelpers::String');

is($string->helper_type, 'Str', '... got the expected helper type');

is($string->type_constraint->name, 'Str', '... got the expected type constraint');

is_deeply($string->provides, {
    inc     => 'inc_string',
    append  => 'append_string',
    prepend => 'prepend_string',
    match   => 'match_string',
    replace => 'replace_string',
    chop    => 'chop_string',
    chomp   => 'chomp_string',
    clear   => 'clear_string',
    substr  => 'sub_string',
    length  => 'length_string',
}, '... got the right provides methods');