add method provider currying support
[gitmo/MooseX-AttributeHelpers.git] / t / 002_basic_array.t
CommitLineData
22d869ff 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
c43a2317 6use Test::More tests => 52;
69dde336 7use Test::Exception;
22d869ff 8
9BEGIN {
10 use_ok('MooseX::AttributeHelpers');
11}
12
13{
14 package Stuff;
15 use Moose;
16
17 has 'options' => (
d26633fc 18 metaclass => 'Collection::Array',
22d869ff 19 is => 'ro',
c43a2317 20 isa => 'ArrayRef[Str]',
22d869ff 21 default => sub { [] },
22 provides => {
8c651099 23 'push' => 'add_options',
24 'pop' => 'remove_last_option',
25 'shift' => 'remove_first_option',
26 'unshift' => 'insert_options',
27 'get' => 'get_option_at',
28 'set' => 'set_option_at',
29 'count' => 'num_options',
30 'empty' => 'has_options',
8cf40f80 31 'clear' => 'clear_options',
c43a2317 32 },
33 curries => {
34 'push' => ['add_options_with_speed', 'funrolls', 'funbuns'],
35 'unshift' => ['prepend_prerequisites_along_with', 'first', 'second']
22d869ff 36 }
37 );
38}
39
77d02b8b 40my $stuff = Stuff->new(options => [ 10, 12 ]);
22d869ff 41isa_ok($stuff, 'Stuff');
42
8c651099 43can_ok($stuff, $_) for qw[
44 add_options
45 remove_last_option
46 remove_first_option
47 insert_options
48 get_option_at
49 set_option_at
50 num_options
8cf40f80 51 clear_options
8c651099 52 has_options
53];
54
77d02b8b 55is_deeply($stuff->options, [10, 12], '... got options');
56
57ok($stuff->has_options, '... we have options');
58is($stuff->num_options, 2, '... got 2 options');
59
60is($stuff->remove_last_option, 12, '... removed the last option');
61is($stuff->remove_first_option, 10, '... removed the last option');
62
63is_deeply($stuff->options, [], '... no options anymore');
22d869ff 64
8c651099 65ok(!$stuff->has_options, '... no options');
66is($stuff->num_options, 0, '... got no options');
67
69dde336 68lives_ok {
69 $stuff->add_options(1, 2, 3);
70} '... set the option okay';
71
22d869ff 72is_deeply($stuff->options, [1, 2, 3], '... got options now');
73
8c651099 74ok($stuff->has_options, '... no options');
75is($stuff->num_options, 3, '... got 3 options');
76
77is($stuff->get_option_at(0), 1, '... get option at index 0');
78is($stuff->get_option_at(1), 2, '... get option at index 1');
79is($stuff->get_option_at(2), 3, '... get option at index 2');
80
69dde336 81lives_ok {
82 $stuff->set_option_at(1, 100);
83} '... set the option okay';
8c651099 84
85is($stuff->get_option_at(1), 100, '... get option at index 1');
86
69dde336 87lives_ok {
88 $stuff->add_options(10, 15);
89} '... set the option okay';
90
8c651099 91is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
92
93is($stuff->num_options, 5, '... got 5 options');
22d869ff 94
95is($stuff->remove_last_option, 15, '... removed the last option');
96
8c651099 97is($stuff->num_options, 4, '... got 4 options');
98is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
99
69dde336 100lives_ok {
101 $stuff->insert_options(10, 20);
102} '... set the option okay';
8c651099 103
104is($stuff->num_options, 6, '... got 6 options');
105is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
106
107is($stuff->get_option_at(0), 10, '... get option at index 0');
108is($stuff->get_option_at(1), 20, '... get option at index 1');
109is($stuff->get_option_at(3), 100, '... get option at index 3');
110
111is($stuff->remove_first_option, 10, '... getting the first option');
112
113is($stuff->num_options, 5, '... got 5 options');
114is($stuff->get_option_at(0), 20, '... get option at index 0');
115
8cf40f80 116$stuff->clear_options;
117is_deeply( $stuff->options, [], "... clear options" );
118
c43a2317 119lives_ok {
120 $stuff->add_options('tree');
121} '... set the options okay';
69dde336 122
c43a2317 123lives_ok {
124 $stuff->add_options_with_speed('compatible', 'safe');
125} '... add options with speed okay';
69dde336 126
c43a2317 127is_deeply($stuff->options, [qw/tree funrolls funbuns compatible safe/]);
69dde336 128
c43a2317 129lives_ok {
130 $stuff->prepend_prerequisites_along_with();
131} '... add prerequisite options okay';
132
133## check some errors
134
135#dies_ok {
136# $stuff->insert_options(undef);
137#} '... could not add an undef where a string is expected';
138#
139#dies_ok {
140# $stuff->set_option(5, {});
141#} '... could not add a hash ref where a string is expected';
69dde336 142
77d02b8b 143dies_ok {
c43a2317 144 Stuff->new(options => [ undef, 10, undef, 20 ]);
77d02b8b 145} '... bad constructor params';
146
8c651099 147## test the meta
148
149my $options = $stuff->meta->get_attribute('options');
150isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array');
22d869ff 151
8c651099 152is_deeply($options->provides, {
153 'push' => 'add_options',
154 'pop' => 'remove_last_option',
155 'shift' => 'remove_first_option',
156 'unshift' => 'insert_options',
157 'get' => 'get_option_at',
158 'set' => 'set_option_at',
159 'count' => 'num_options',
160 'empty' => 'has_options',
8cf40f80 161 'clear' => 'clear_options',
8c651099 162}, '... got the right provies mapping');
22d869ff 163
c43a2317 164is($options->type_constraint->type_parameter, 'Str', '... got the right container type');