File: 206_trait_bag.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 (77 lines) | stat: -rw-r--r-- 1,934 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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 20;
use Test::Exception;
use Test::Moose 'does_ok';

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

{
    package Stuff;
    use Moose;
    use MooseX::AttributeHelpers;

    has 'word_histogram' => (
        traits    => [qw/MooseX::AttributeHelpers::Trait::Collection::Bag/],
        is        => 'ro',
        provides  => {
            'add'    => 'add_word',
            'get'    => 'get_count_for',            
            'empty'  => 'has_any_words',
            'count'  => 'num_words',
            'delete' => 'delete_word',
        }
    );
}

my $stuff = Stuff->new();
isa_ok($stuff, 'Stuff');

can_ok($stuff, $_) for qw[
    add_word
    get_count_for
    has_any_words
    num_words
    delete_word
];

ok(!$stuff->has_any_words, '... we have no words');
is($stuff->num_words, 0, '... we have no words');

lives_ok {
    $stuff->add_word('bar');
} '... set the words okay';

ok($stuff->has_any_words, '... we have words');
is($stuff->num_words, 1, '... we have 1 word(s)');
is($stuff->get_count_for('bar'), 1, '... got words now');

lives_ok {
    $stuff->add_word('foo');                
    $stuff->add_word('bar') for 0 .. 3;            
    $stuff->add_word('baz') for 0 .. 10;                
} '... set the words okay';

is($stuff->num_words, 3, '... we still have 1 word(s)');
is($stuff->get_count_for('foo'), 1, '... got words now');
is($stuff->get_count_for('bar'), 5, '... got words now');
is($stuff->get_count_for('baz'), 11, '... got words now');

## test the meta

my $words = $stuff->meta->get_attribute('word_histogram');
does_ok($words, 'MooseX::AttributeHelpers::Trait::Collection::Bag');

is_deeply($words->provides, {
    'add'    => 'add_word',
    'get'    => 'get_count_for',            
    'empty'  => 'has_any_words',
    'count'  => 'num_words',
    'delete' => 'delete_word',
}, '... got the right provides mapping');