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