Commit | Line | Data |
e3c07b19 |
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 { |
59de9de4 |
11 | use_ok('MooseX::AttributeHelpers'); |
e3c07b19 |
12 | } |
13 | |
14 | { |
15 | package Stuff; |
16 | use Moose; |
59de9de4 |
17 | use MooseX::AttributeHelpers; |
e3c07b19 |
18 | |
19 | has 'word_histogram' => ( |
59de9de4 |
20 | traits => [qw/MooseX::AttributeHelpers::Trait::Collection::Bag/], |
e3c07b19 |
21 | is => 'ro', |
83cd6eb1 |
22 | handles => { |
23 | 'add_word' => 'add', |
24 | 'get_count_for' => 'get', |
25 | 'has_any_words' => 'empty', |
26 | 'num_words' => 'count', |
27 | 'delete_word' => 'delete', |
e3c07b19 |
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 | num_words |
40 | delete_word |
41 | ]; |
42 | |
43 | ok(!$stuff->has_any_words, '... we have no words'); |
44 | is($stuff->num_words, 0, '... we have no words'); |
45 | |
46 | lives_ok { |
47 | $stuff->add_word('bar'); |
48 | } '... set the words okay'; |
49 | |
50 | ok($stuff->has_any_words, '... we have words'); |
51 | is($stuff->num_words, 1, '... we have 1 word(s)'); |
52 | is($stuff->get_count_for('bar'), 1, '... got words now'); |
53 | |
54 | lives_ok { |
55 | $stuff->add_word('foo'); |
56 | $stuff->add_word('bar') for 0 .. 3; |
57 | $stuff->add_word('baz') for 0 .. 10; |
58 | } '... set the words okay'; |
59 | |
60 | is($stuff->num_words, 3, '... we still have 1 word(s)'); |
61 | is($stuff->get_count_for('foo'), 1, '... got words now'); |
62 | is($stuff->get_count_for('bar'), 5, '... got words now'); |
63 | is($stuff->get_count_for('baz'), 11, '... got words now'); |
64 | |
65 | ## test the meta |
66 | |
67 | my $words = $stuff->meta->get_attribute('word_histogram'); |
59de9de4 |
68 | does_ok($words, 'MooseX::AttributeHelpers::Trait::Collection::Bag'); |
e3c07b19 |
69 | |
83cd6eb1 |
70 | is_deeply($words->handles, { |
71 | 'add_word' => 'add', |
72 | 'get_count_for' => 'get', |
73 | 'has_any_words' => 'empty', |
74 | 'num_words' => 'count', |
75 | 'delete_word' => 'delete', |
76 | }, '... got the right handles mapping'); |
e3c07b19 |
77 | |