All unit tests passing with refactored stuff, documentation updated significantly.
[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 => 19;
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             'has_items' => 'has_any_words',
25             'is_empty'  => 'has_no_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     has_no_words
40     num_words
41     delete_word
42 ];
43
44 ok($stuff->has_no_words, '... we have no words');
45 is($stuff->num_words, 0, '... we have no words');
46
47 lives_ok {
48     $stuff->add_word('bar');
49 } '... set the words okay';
50
51 ok($stuff->has_any_words, '... we have words');
52 is($stuff->num_words, 1, '... we have 1 word(s)');
53 is($stuff->get_count_for('bar'), 1, '... got words now');
54
55 lives_ok {
56     $stuff->add_word('foo');                
57     $stuff->add_word('bar') for 0 .. 3;            
58     $stuff->add_word('baz') for 0 .. 10;                
59 } '... set the words okay';
60
61 is($stuff->num_words, 3, '... we still have 1 word(s)');
62 is($stuff->get_count_for('foo'), 1, '... got words now');
63 is($stuff->get_count_for('bar'), 5, '... got words now');
64 is($stuff->get_count_for('baz'), 11, '... got words now');
65
66
67