* added typed-ness to collections
[gitmo/MooseX-AttributeHelpers.git] / t / 002_basic_collection.t
CommitLineData
22d869ff 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More no_plan => 1;
7
8BEGIN {
9 use_ok('MooseX::AttributeHelpers');
10}
11
12{
13 package Stuff;
14 use Moose;
15
16 has 'options' => (
d26633fc 17 metaclass => 'Collection::Array',
22d869ff 18 is => 'ro',
8c651099 19 isa => 'ArrayRef[Int]',
22d869ff 20 default => sub { [] },
21 provides => {
8c651099 22 'push' => 'add_options',
23 'pop' => 'remove_last_option',
24 'shift' => 'remove_first_option',
25 'unshift' => 'insert_options',
26 'get' => 'get_option_at',
27 'set' => 'set_option_at',
28 'count' => 'num_options',
29 'empty' => 'has_options',
22d869ff 30 }
31 );
32}
33
34my $stuff = Stuff->new();
35isa_ok($stuff, 'Stuff');
36
8c651099 37can_ok($stuff, $_) for qw[
38 add_options
39 remove_last_option
40 remove_first_option
41 insert_options
42 get_option_at
43 set_option_at
44 num_options
45 has_options
46];
47
22d869ff 48is_deeply($stuff->options, [], '... no options yet');
49
8c651099 50ok(!$stuff->has_options, '... no options');
51is($stuff->num_options, 0, '... got no options');
52
22d869ff 53$stuff->add_options(1, 2, 3);
54is_deeply($stuff->options, [1, 2, 3], '... got options now');
55
8c651099 56ok($stuff->has_options, '... no options');
57is($stuff->num_options, 3, '... got 3 options');
58
59is($stuff->get_option_at(0), 1, '... get option at index 0');
60is($stuff->get_option_at(1), 2, '... get option at index 1');
61is($stuff->get_option_at(2), 3, '... get option at index 2');
62
63$stuff->set_option_at(1, 100);
64
65is($stuff->get_option_at(1), 100, '... get option at index 1');
66
22d869ff 67$stuff->add_options(10, 15);
8c651099 68is_deeply($stuff->options, [1, 100, 3, 10, 15], '... got more options now');
69
70is($stuff->num_options, 5, '... got 5 options');
22d869ff 71
72is($stuff->remove_last_option, 15, '... removed the last option');
73
8c651099 74is($stuff->num_options, 4, '... got 4 options');
75is_deeply($stuff->options, [1, 100, 3, 10], '... got diff options now');
76
77$stuff->insert_options(10, 20);
78
79is($stuff->num_options, 6, '... got 6 options');
80is_deeply($stuff->options, [10, 20, 1, 100, 3, 10], '... got diff options now');
81
82is($stuff->get_option_at(0), 10, '... get option at index 0');
83is($stuff->get_option_at(1), 20, '... get option at index 1');
84is($stuff->get_option_at(3), 100, '... get option at index 3');
85
86is($stuff->remove_first_option, 10, '... getting the first option');
87
88is($stuff->num_options, 5, '... got 5 options');
89is($stuff->get_option_at(0), 20, '... get option at index 0');
90
91## test the meta
92
93my $options = $stuff->meta->get_attribute('options');
94isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array');
22d869ff 95
8c651099 96is_deeply($options->provides, {
97 'push' => 'add_options',
98 'pop' => 'remove_last_option',
99 'shift' => 'remove_first_option',
100 'unshift' => 'insert_options',
101 'get' => 'get_option_at',
102 'set' => 'set_option_at',
103 'count' => 'num_options',
104 'empty' => 'has_options',
105}, '... got the right provies mapping');
22d869ff 106
107