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; |
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', |
28 | 'delete' => 'delete_option', |
d26633fc |
29 | } |
30 | ); |
31 | } |
32 | |
33 | my $stuff = Stuff->new(); |
34 | isa_ok($stuff, 'Stuff'); |
35 | |
c25a396f |
36 | can_ok($stuff, $_) for qw[ |
37 | set_option |
38 | get_option |
39 | has_options |
40 | num_options |
158d8e20 |
41 | delete_option |
c25a396f |
42 | ]; |
43 | |
44 | ok(!$stuff->has_options, '... we have no options'); |
45 | is($stuff->num_options, 0, '... we have no options'); |
46 | |
d26633fc |
47 | is_deeply($stuff->options, {}, '... no options yet'); |
48 | |
69dde336 |
49 | lives_ok { |
50 | $stuff->set_option(foo => 'bar'); |
51 | } '... set the option okay'; |
c25a396f |
52 | |
53 | ok($stuff->has_options, '... we have options'); |
54 | is($stuff->num_options, 1, '... we have 1 option(s)'); |
d26633fc |
55 | is_deeply($stuff->options, { foo => 'bar' }, '... got options now'); |
56 | |
69dde336 |
57 | lives_ok { |
58 | $stuff->set_option(bar => 'baz'); |
59 | } '... set the option okay'; |
c25a396f |
60 | |
61 | is($stuff->num_options, 2, '... we have 2 option(s)'); |
d26633fc |
62 | is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now'); |
63 | |
64 | is($stuff->get_option('foo'), 'bar', '... got the right option'); |
65 | |
77d02b8b |
66 | lives_ok { |
158d8e20 |
67 | $stuff->delete_option('bar'); |
68 | } '... deleted the option okay'; |
69 | |
70 | is($stuff->num_options, 1, '... we have 1 option(s)'); |
71 | is_deeply($stuff->options, { foo => 'bar' }, '... got more options now'); |
72 | |
73 | lives_ok { |
77d02b8b |
74 | Stuff->new(options => { foo => 'BAR' }); |
75 | } '... good constructor params'; |
76 | |
69dde336 |
77 | ## check some errors |
78 | |
79 | dies_ok { |
80 | $stuff->set_option(bar => {}); |
81 | } '... could not add a hash ref where an string is expected'; |
d26633fc |
82 | |
77d02b8b |
83 | dies_ok { |
84 | Stuff->new(options => { foo => [] }); |
85 | } '... bad constructor params'; |
86 | |
d26633fc |
87 | |