File: 001_basic_counter.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 (78 lines) | stat: -rw-r--r-- 1,712 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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 18;

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

{
    package MyHomePage;
    use Moose;

    has 'counter' => (
        metaclass => 'Counter',
        is        => 'ro',
        isa       => 'Int',
        default   => sub { 0 },
        provides  => {
            inc   => 'inc_counter',
            dec   => 'dec_counter',
            reset => 'reset_counter',
            set   => 'set_counter'
        }
    );
}

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

can_ok($page, $_) for qw[
    dec_counter 
    inc_counter
    reset_counter
    set_counter
];

is($page->counter, 0, '... got the default value');

$page->inc_counter; 
is($page->counter, 1, '... got the incremented value');

$page->inc_counter; 
is($page->counter, 2, '... got the incremented value (again)');

$page->dec_counter; 
is($page->counter, 1, '... got the decremented value');

$page->reset_counter;
is($page->counter, 0, '... got the original value');

$page->set_counter(5);
is($page->counter, 5, '... set the value');

$page->inc_counter(2);
is($page->counter, 7, '... increment by arg');

$page->dec_counter(5);
is($page->counter, 2, '... decrement by arg');

# check the meta ..

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

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

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

is_deeply($counter->provides, { 
    inc   => 'inc_counter',
    dec   => 'dec_counter',
    reset => 'reset_counter',
    set   => 'set_counter'
}, '... got the right provides methods');