Commit | Line | Data |
e3c07b19 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a5209c26 |
6 | use Test::More tests => 19; |
e3c07b19 |
7 | use Test::Exception; |
8 | use Test::Moose 'does_ok'; |
9 | |
e3c07b19 |
10 | { |
d50fc84a |
11 | |
e3c07b19 |
12 | package Stuff; |
13 | use Moose; |
a41de825 |
14 | use Moose::AttributeHelpers; |
e3c07b19 |
15 | |
16 | has 'word_histogram' => ( |
d50fc84a |
17 | traits => ['Collection::Bag'], |
18 | is => 'ro', |
19 | handles => { |
20 | 'add_word' => 'add', |
21 | 'get_count_for' => 'get', |
22 | 'has_any_words' => 'empty', |
23 | 'num_words' => 'count', |
24 | 'delete_word' => 'delete', |
e3c07b19 |
25 | } |
26 | ); |
27 | } |
28 | |
29 | my $stuff = Stuff->new(); |
d50fc84a |
30 | isa_ok( $stuff, 'Stuff' ); |
e3c07b19 |
31 | |
d50fc84a |
32 | can_ok( $stuff, $_ ) for qw[ |
e3c07b19 |
33 | add_word |
34 | get_count_for |
35 | has_any_words |
36 | num_words |
37 | delete_word |
38 | ]; |
39 | |
d50fc84a |
40 | ok( !$stuff->has_any_words, '... we have no words' ); |
41 | is( $stuff->num_words, 0, '... we have no words' ); |
e3c07b19 |
42 | |
43 | lives_ok { |
44 | $stuff->add_word('bar'); |
d50fc84a |
45 | } |
46 | '... set the words okay'; |
e3c07b19 |
47 | |
d50fc84a |
48 | ok( $stuff->has_any_words, '... we have words' ); |
49 | is( $stuff->num_words, 1, '... we have 1 word(s)' ); |
50 | is( $stuff->get_count_for('bar'), 1, '... got words now' ); |
e3c07b19 |
51 | |
52 | lives_ok { |
53 | $stuff->add_word('foo'); |
54 | $stuff->add_word('bar') for 0 .. 3; |
55 | $stuff->add_word('baz') for 0 .. 10; |
d50fc84a |
56 | } |
57 | '... set the words okay'; |
e3c07b19 |
58 | |
d50fc84a |
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' ); |
e3c07b19 |
63 | |
64 | ## test the meta |
65 | |
66 | my $words = $stuff->meta->get_attribute('word_histogram'); |
d50fc84a |
67 | does_ok( $words, 'Moose::AttributeHelpers::Trait::Collection::Bag' ); |
68 | |
69 | is_deeply( |
70 | $words->handles, |
71 | { |
72 | 'add_word' => 'add', |
73 | 'get_count_for' => 'get', |
74 | 'has_any_words' => 'empty', |
75 | 'num_words' => 'count', |
76 | 'delete_word' => 'delete', |
77 | }, |
78 | '... got the right handles mapping' |
79 | ); |
e3c07b19 |
80 | |