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 => { |
c25a396f |
23 | 'set' => 'set_option', |
24 | 'get' => 'get_option', |
25 | 'empty' => 'has_options', |
26 | 'count' => 'num_options', |
d26633fc |
27 | } |
28 | ); |
29 | } |
30 | |
31 | my $stuff = Stuff->new(); |
32 | isa_ok($stuff, 'Stuff'); |
33 | |
c25a396f |
34 | can_ok($stuff, $_) for qw[ |
35 | set_option |
36 | get_option |
37 | has_options |
38 | num_options |
39 | ]; |
40 | |
41 | ok(!$stuff->has_options, '... we have no options'); |
42 | is($stuff->num_options, 0, '... we have no options'); |
43 | |
d26633fc |
44 | is_deeply($stuff->options, {}, '... no options yet'); |
45 | |
69dde336 |
46 | lives_ok { |
47 | $stuff->set_option(foo => 'bar'); |
48 | } '... set the option okay'; |
c25a396f |
49 | |
50 | ok($stuff->has_options, '... we have options'); |
51 | is($stuff->num_options, 1, '... we have 1 option(s)'); |
d26633fc |
52 | is_deeply($stuff->options, { foo => 'bar' }, '... got options now'); |
53 | |
69dde336 |
54 | lives_ok { |
55 | $stuff->set_option(bar => 'baz'); |
56 | } '... set the option okay'; |
c25a396f |
57 | |
58 | is($stuff->num_options, 2, '... we have 2 option(s)'); |
d26633fc |
59 | is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now'); |
60 | |
61 | is($stuff->get_option('foo'), 'bar', '... got the right option'); |
62 | |
69dde336 |
63 | ## check some errors |
64 | |
65 | dies_ok { |
66 | $stuff->set_option(bar => {}); |
67 | } '... could not add a hash ref where an string is expected'; |
d26633fc |
68 | |
69 | |