Bump to 0.14
[gitmo/MooseX-AttributeHelpers.git] / t / 006_basic_bag.t
CommitLineData
9a976497 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
35758b1c 6use Test::More tests => 20;
9a976497 7use Test::Exception;
8
9BEGIN {
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
31my $stuff = Stuff->new();
32isa_ok($stuff, 'Stuff');
33
34can_ok($stuff, $_) for qw[
35 add_word
36 get_count_for
37 has_any_words
38 num_words
39 delete_word
40];
41
42ok(!$stuff->has_any_words, '... we have no words');
43is($stuff->num_words, 0, '... we have no words');
44
45lives_ok {
46 $stuff->add_word('bar');
47} '... set the words okay';
48
49ok($stuff->has_any_words, '... we have words');
50is($stuff->num_words, 1, '... we have 1 word(s)');
51is($stuff->get_count_for('bar'), 1, '... got words now');
52
53lives_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
59is($stuff->num_words, 3, '... we still have 1 word(s)');
60is($stuff->get_count_for('foo'), 1, '... got words now');
61is($stuff->get_count_for('bar'), 5, '... got words now');
62is($stuff->get_count_for('baz'), 11, '... got words now');
63
35758b1c 64## test the meta
9a976497 65
35758b1c 66my $words = $stuff->meta->get_attribute('word_histogram');
67isa_ok($words, 'MooseX::AttributeHelpers::Collection::Bag');
68
69is_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');
9a976497 76