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