Commit | Line | Data |
d26633fc |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
05f7da43 |
6 | use Test::More tests => 32; |
69dde336 |
7 | use Test::Exception; |
d26633fc |
8 | |
9 | BEGIN { |
10 | use_ok('MooseX::AttributeHelpers'); |
11 | } |
12 | |
13 | { |
14 | package Stuff; |
15 | use Moose; |
5431dff2 |
16 | use MooseX::AttributeHelpers; |
d26633fc |
17 | |
18 | has 'options' => ( |
19 | metaclass => 'Collection::Hash', |
20 | is => 'ro', |
c25a396f |
21 | isa => 'HashRef[Str]', |
d26633fc |
22 | default => sub { {} }, |
23 | provides => { |
158d8e20 |
24 | 'set' => 'set_option', |
25 | 'get' => 'get_option', |
26 | 'empty' => 'has_options', |
27 | 'count' => 'num_options', |
8cf40f80 |
28 | 'clear' => 'clear_options', |
158d8e20 |
29 | 'delete' => 'delete_option', |
d26633fc |
30 | } |
31 | ); |
32 | } |
33 | |
34 | my $stuff = Stuff->new(); |
35 | isa_ok($stuff, 'Stuff'); |
36 | |
c25a396f |
37 | can_ok($stuff, $_) for qw[ |
38 | set_option |
39 | get_option |
40 | has_options |
41 | num_options |
158d8e20 |
42 | delete_option |
8cf40f80 |
43 | clear_options |
c25a396f |
44 | ]; |
45 | |
46 | ok(!$stuff->has_options, '... we have no options'); |
47 | is($stuff->num_options, 0, '... we have no options'); |
48 | |
d26633fc |
49 | is_deeply($stuff->options, {}, '... no options yet'); |
50 | |
69dde336 |
51 | lives_ok { |
52 | $stuff->set_option(foo => 'bar'); |
53 | } '... set the option okay'; |
c25a396f |
54 | |
55 | ok($stuff->has_options, '... we have options'); |
56 | is($stuff->num_options, 1, '... we have 1 option(s)'); |
d26633fc |
57 | is_deeply($stuff->options, { foo => 'bar' }, '... got options now'); |
58 | |
69dde336 |
59 | lives_ok { |
60 | $stuff->set_option(bar => 'baz'); |
61 | } '... set the option okay'; |
c25a396f |
62 | |
63 | is($stuff->num_options, 2, '... we have 2 option(s)'); |
d26633fc |
64 | is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now'); |
65 | |
66 | is($stuff->get_option('foo'), 'bar', '... got the right option'); |
67 | |
05f7da43 |
68 | is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once"); |
69 | |
70 | lives_ok { |
71 | $stuff->set_option(oink => "blah", xxy => "flop"); |
72 | } '... set the option okay'; |
73 | |
74 | is($stuff->num_options, 4, "4 options"); |
75 | is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once"); |
76 | |
77d02b8b |
77 | lives_ok { |
158d8e20 |
78 | $stuff->delete_option('bar'); |
79 | } '... deleted the option okay'; |
80 | |
05f7da43 |
81 | lives_ok { |
82 | $stuff->delete_option('oink'); |
83 | } '... deleted the option okay'; |
84 | |
85 | lives_ok { |
86 | $stuff->delete_option('xxy'); |
87 | } '... deleted the option okay'; |
88 | |
158d8e20 |
89 | is($stuff->num_options, 1, '... we have 1 option(s)'); |
90 | is_deeply($stuff->options, { foo => 'bar' }, '... got more options now'); |
91 | |
8cf40f80 |
92 | $stuff->clear_options; |
93 | |
94 | is_deeply($stuff->options, { }, "... cleared options" ); |
95 | |
158d8e20 |
96 | lives_ok { |
77d02b8b |
97 | Stuff->new(options => { foo => 'BAR' }); |
98 | } '... good constructor params'; |
99 | |
69dde336 |
100 | ## check some errors |
101 | |
102 | dies_ok { |
103 | $stuff->set_option(bar => {}); |
104 | } '... could not add a hash ref where an string is expected'; |
d26633fc |
105 | |
77d02b8b |
106 | dies_ok { |
107 | Stuff->new(options => { foo => [] }); |
108 | } '... bad constructor params'; |
109 | |
d26633fc |
110 | |