Credit me
[gitmo/MooseX-AttributeHelpers.git] / t / 006_basic_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
9 BEGIN {
10     use_ok('MooseX::AttributeHelpers');   
11 }
12
13 {
14     package Stuff;
15     use Moose;
16     use MooseX::AttributeHelpers;
17
18     has 'word_histogram' => (
19         metaclass => 'Collection::Bag',
20         is        => 'ro',
21         provides  => {
22             'add'    => 'add_word',
23             'get'    => 'get_count_for',            
24             'empty'  => 'has_any_words',
25             'count'  => 'num_words',
26             'delete' => 'delete_word',
27         }
28     );
29 }
30
31 my $stuff = Stuff->new();
32 isa_ok($stuff, 'Stuff');
33
34 can_ok($stuff, $_) for qw[
35     add_word
36     get_count_for
37     has_any_words
38     num_words
39     delete_word
40 ];
41
42 ok(!$stuff->has_any_words, '... we have no words');
43 is($stuff->num_words, 0, '... we have no words');
44
45 lives_ok {
46     $stuff->add_word('bar');
47 } '... set the words okay';
48
49 ok($stuff->has_any_words, '... we have words');
50 is($stuff->num_words, 1, '... we have 1 word(s)');
51 is($stuff->get_count_for('bar'), 1, '... got words now');
52
53 lives_ok {
54     $stuff->add_word('foo');                
55     $stuff->add_word('bar') for 0 .. 3;            
56     $stuff->add_word('baz') for 0 .. 10;                
57 } '... set the words okay';
58
59 is($stuff->num_words, 3, '... we still have 1 word(s)');
60 is($stuff->get_count_for('foo'), 1, '... got words now');
61 is($stuff->get_count_for('bar'), 5, '... got words now');
62 is($stuff->get_count_for('baz'), 11, '... got words now');
63
64 ## test the meta
65
66 my $words = $stuff->meta->get_attribute('word_histogram');
67 isa_ok($words, 'MooseX::AttributeHelpers::Collection::Bag');
68
69 is_deeply($words->provides, {
70     'add'    => 'add_word',
71     'get'    => 'get_count_for',            
72     'empty'  => 'has_any_words',
73     'count'  => 'num_words',
74     'delete' => 'delete_word',
75 }, '... got the right provides mapping');
76