splice was totally broken, add tests and fix it
[gitmo/MooseX-AttributeHelpers.git] / t / 003_basic_hash.t
CommitLineData
d26633fc 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
57f29c32 6use Test::More tests => 40;
69dde336 7use Test::Exception;
d26633fc 8
9BEGIN {
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',
9bfd5e92 30 'exists' => 'has_option',
c43a2317 31 },
32 curries => {
3656a0d7 33 'set' => {
34 set_quantity => ['quantity']
35 },
d26633fc 36 }
37 );
38}
39
40my $stuff = Stuff->new();
41isa_ok($stuff, 'Stuff');
42
c25a396f 43can_ok($stuff, $_) for qw[
44 set_option
45 get_option
46 has_options
47 num_options
158d8e20 48 delete_option
8cf40f80 49 clear_options
9bfd5e92 50 has_option
c25a396f 51];
52
53ok(!$stuff->has_options, '... we have no options');
54is($stuff->num_options, 0, '... we have no options');
55
d26633fc 56is_deeply($stuff->options, {}, '... no options yet');
57f29c32 57ok(!$stuff->has_option('foo'), '... we have no foo option');
d26633fc 58
69dde336 59lives_ok {
60 $stuff->set_option(foo => 'bar');
61} '... set the option okay';
c25a396f 62
63ok($stuff->has_options, '... we have options');
64is($stuff->num_options, 1, '... we have 1 option(s)');
9bfd5e92 65ok($stuff->has_option('foo'), '... we have a foo option');
d26633fc 66is_deeply($stuff->options, { foo => 'bar' }, '... got options now');
67
69dde336 68lives_ok {
69 $stuff->set_option(bar => 'baz');
70} '... set the option okay';
c25a396f 71
72is($stuff->num_options, 2, '... we have 2 option(s)');
d26633fc 73is_deeply($stuff->options, { foo => 'bar', bar => 'baz' }, '... got more options now');
74
75is($stuff->get_option('foo'), 'bar', '... got the right option');
76
05f7da43 77is_deeply([ $stuff->get_option(qw(foo bar)) ], [qw(bar baz)], "get multiple options at once");
78
79lives_ok {
80 $stuff->set_option(oink => "blah", xxy => "flop");
81} '... set the option okay';
82
83is($stuff->num_options, 4, "4 options");
84is_deeply([ $stuff->get_option(qw(foo bar oink xxy)) ], [qw(bar baz blah flop)], "get multiple options at once");
85
77d02b8b 86lives_ok {
158d8e20 87 $stuff->delete_option('bar');
88} '... deleted the option okay';
89
05f7da43 90lives_ok {
91 $stuff->delete_option('oink');
92} '... deleted the option okay';
93
94lives_ok {
95 $stuff->delete_option('xxy');
96} '... deleted the option okay';
97
158d8e20 98is($stuff->num_options, 1, '... we have 1 option(s)');
99is_deeply($stuff->options, { foo => 'bar' }, '... got more options now');
100
8cf40f80 101$stuff->clear_options;
102
103is_deeply($stuff->options, { }, "... cleared options" );
104
158d8e20 105lives_ok {
3656a0d7 106 $stuff->set_quantity(4);
c43a2317 107} '... options added okay with defaults';
108
9bfd5e92 109is_deeply($stuff->options, {quantity => 4}, '... returns what we expect');
c43a2317 110
111lives_ok {
77d02b8b 112 Stuff->new(options => { foo => 'BAR' });
113} '... good constructor params';
114
69dde336 115## check some errors
116
117dies_ok {
118 $stuff->set_option(bar => {});
119} '... could not add a hash ref where an string is expected';
d26633fc 120
77d02b8b 121dies_ok {
122 Stuff->new(options => { foo => [] });
123} '... bad constructor params';
124
321ab9fe 125## test the meta
d26633fc 126
321ab9fe 127my $options = $stuff->meta->get_attribute('options');
128isa_ok($options, 'MooseX::AttributeHelpers::Collection::Hash');
129
130is_deeply($options->provides, {
131 'set' => 'set_option',
132 'get' => 'get_option',
133 'empty' => 'has_options',
134 'count' => 'num_options',
135 'clear' => 'clear_options',
136 'delete' => 'delete_option',
9bfd5e92 137 'exists' => 'has_option',
321ab9fe 138}, '... got the right provies mapping');
139
140is($options->type_constraint->type_parameter, 'Str', '... got the right container type');