make the test style match the rest of the (modern) Moose tests
[gitmo/Moose.git] / t / 070_attribute_helpers / 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('Moose::AttributeHelpers');
12 }
13
14 {
15
16     package Stuff;
17     use Moose;
18     use Moose::AttributeHelpers;
19
20     has 'word_histogram' => (
21         traits  => ['Collection::Bag'],
22         is      => 'ro',
23         handles => {
24             'add_word'      => 'add',
25             'get_count_for' => 'get',
26             'has_any_words' => 'empty',
27             'num_words'     => 'count',
28             'delete_word'   => 'delete',
29         }
30     );
31 }
32
33 my $stuff = Stuff->new();
34 isa_ok( $stuff, 'Stuff' );
35
36 can_ok( $stuff, $_ ) for qw[
37     add_word
38     get_count_for
39     has_any_words
40     num_words
41     delete_word
42 ];
43
44 ok( !$stuff->has_any_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 }
50 '... set the words okay';
51
52 ok( $stuff->has_any_words, '... we have words' );
53 is( $stuff->num_words,            1, '... we have 1 word(s)' );
54 is( $stuff->get_count_for('bar'), 1, '... got words now' );
55
56 lives_ok {
57     $stuff->add_word('foo');
58     $stuff->add_word('bar') for 0 .. 3;
59     $stuff->add_word('baz') for 0 .. 10;
60 }
61 '... set the words okay';
62
63 is( $stuff->num_words,            3,  '... we still have 1 word(s)' );
64 is( $stuff->get_count_for('foo'), 1,  '... got words now' );
65 is( $stuff->get_count_for('bar'), 5,  '... got words now' );
66 is( $stuff->get_count_for('baz'), 11, '... got words now' );
67
68 ## test the meta
69
70 my $words = $stuff->meta->get_attribute('word_histogram');
71 does_ok( $words, 'Moose::AttributeHelpers::Trait::Collection::Bag' );
72
73 is_deeply(
74     $words->handles,
75     {
76         'add_word'      => 'add',
77         'get_count_for' => 'get',
78         'has_any_words' => 'empty',
79         'num_words'     => 'count',
80         'delete_word'   => 'delete',
81     },
82     '... got the right handles mapping'
83 );
84