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