Ignore meta in a method provider's list of provided methods.
[gitmo/MooseX-AttributeHelpers.git] / t / 206_trait_bag.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 20;
7 use Test::Exception;
8 use Test::Moose 'does_ok';
9
10 BEGIN {
11     use_ok('MooseX::AttributeHelpers');   
12 }
13
14 {
15     package Stuff;
16     use Moose;
17     use MooseX::AttributeHelpers;
18
19     has 'word_histogram' => (
20         traits    => [qw/MooseX::AttributeHelpers::Trait::Collection::Bag/],
21         is        => 'ro',
22         provides  => {
23             'add'    => 'add_word',
24             'get'    => 'get_count_for',            
25             'empty'  => 'has_any_words',
26             'count'  => 'num_words',
27             'delete' => 'delete_word',
28         }
29     );
30 }
31
32 my $stuff = Stuff->new();
33 isa_ok($stuff, 'Stuff');
34
35 can_ok($stuff, $_) for qw[
36     add_word
37     get_count_for
38     has_any_words
39     num_words
40     delete_word
41 ];
42
43 ok(!$stuff->has_any_words, '... we have no words');
44 is($stuff->num_words, 0, '... we have no words');
45
46 lives_ok {
47     $stuff->add_word('bar');
48 } '... set the words okay';
49
50 ok($stuff->has_any_words, '... we have words');
51 is($stuff->num_words, 1, '... we have 1 word(s)');
52 is($stuff->get_count_for('bar'), 1, '... got words now');
53
54 lives_ok {
55     $stuff->add_word('foo');                
56     $stuff->add_word('bar') for 0 .. 3;            
57     $stuff->add_word('baz') for 0 .. 10;                
58 } '... set the words okay';
59
60 is($stuff->num_words, 3, '... we still have 1 word(s)');
61 is($stuff->get_count_for('foo'), 1, '... got words now');
62 is($stuff->get_count_for('bar'), 5, '... got words now');
63 is($stuff->get_count_for('baz'), 11, '... got words now');
64
65 ## test the meta
66
67 my $words = $stuff->meta->get_attribute('word_histogram');
68 does_ok($words, 'MooseX::AttributeHelpers::Trait::Collection::Bag');
69
70 is_deeply($words->provides, {
71     'add'    => 'add_word',
72     'get'    => 'get_count_for',            
73     'empty'  => 'has_any_words',
74     'count'  => 'num_words',
75     'delete' => 'delete_word',
76 }, '... got the right provides mapping');
77