fix collection docs
[catagits/DOM-Tiny.git] / t / collection.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use DOM::Tiny::Collection 'c';
5 use JSON::Tiny 'encode_json';
6
7 # Array
8 is c(1, 2, 3)->[1], 2, 'right result';
9 is_deeply [@{c(3, 2, 1)}], [3, 2, 1], 'right result';
10 my $collection = c(1, 2);
11 push @$collection, 3, 4, 5;
12 is_deeply [@$collection], [1, 2, 3, 4, 5], 'right result';
13
14 # Tap into method chain
15 is_deeply c(1, 2, 3)->tap(sub { $_->[1] += 2 })->to_array, [1, 4, 3],
16   'right result';
17
18 # compact
19 is_deeply c(undef, 0, 1, '', 2, 3)->compact->to_array, [0, 1, 2, 3],
20   'right result';
21 is_deeply c(3, 2, 1)->compact->to_array, [3, 2, 1], 'right result';
22 is_deeply c()->compact->to_array, [], 'right result';
23
24 # flatten
25 is_deeply c(1, 2, [3, 4], 5, c(6, 7))->flatten->to_array,
26   [1, 2, 3, 4, 5, 6, 7], 'right result';
27 is_deeply c(undef, 1, [2, {}, [3, c(4, 5)]], undef, 6)->flatten->to_array,
28   [undef, 1, 2, {}, 3, 4, 5, undef, 6], 'right result';
29
30 # each
31 $collection = c(3, 2, 1);
32 is_deeply [$collection->each], [3, 2, 1], 'right elements';
33 $collection = c([3], [2], [1]);
34 my @results;
35 $collection->each(sub { push @results, $_->[0] });
36 is_deeply \@results, [3, 2, 1], 'right elements';
37 @results = ();
38 $collection->each(sub { push @results, shift->[0], shift });
39 is_deeply \@results, [3, 1, 2, 2, 1, 3], 'right elements';
40
41 # first
42 $collection = c(5, 4, [3, 2], 1);
43 is $collection->first, 5, 'right result';
44 is_deeply $collection->first(sub { ref $_ eq 'ARRAY' }), [3, 2], 'right result';
45 is $collection->first(sub { shift() < 5 }), 4, 'right result';
46 is $collection->first(qr/[1-4]/), 4, 'right result';
47 is $collection->first(sub { ref $_ eq 'CODE' }), undef, 'no result';
48 $collection = c(c(1, 2, 3), c(4, 5, 6), c(7, 8, 9));
49 is_deeply $collection->first(first => sub { $_ == 5 })->to_array, [4, 5, 6],
50   'right result';
51 $collection = c();
52 is $collection->first, undef, 'no result';
53 is $collection->first(sub {defined}), undef, 'no result';
54
55 # last
56 is c(5, 4, 3)->last, 3, 'right result';
57 is c(5, 4, 3)->reverse->last, 5, 'right result';
58 is c()->last, undef, 'no result';
59
60 # grep
61 $collection = c(1, 2, 3, 4, 5, 6, 7, 8, 9);
62 is_deeply $collection->grep(qr/[6-9]/)->to_array, [6, 7, 8, 9],
63   'right elements';
64 is_deeply $collection->grep(sub {/[6-9]/})->to_array, [6, 7, 8, 9],
65   'right elements';
66 is_deeply $collection->grep(sub { $_ > 5 })->to_array, [6, 7, 8, 9],
67   'right elements';
68 is_deeply $collection->grep(sub { $_ < 5 })->to_array, [1, 2, 3, 4],
69   'right elements';
70 is_deeply $collection->grep(sub { shift == 5 })->to_array, [5],
71   'right elements';
72 is_deeply $collection->grep(sub { $_ < 1 })->to_array, [], 'no elements';
73 is_deeply $collection->grep(sub { $_ > 9 })->to_array, [], 'no elements';
74 $collection = c(c(1, 2, 3), c(4, 5, 6), c(7, 8, 9));
75 is_deeply $collection->grep(first => sub { $_ >= 5 })->flatten->to_array,
76   [4, 5, 6, 7, 8, 9], 'right result';
77
78 # join
79 $collection = c(1, 2, 3);
80 is $collection->join, '123', 'right result';
81 is $collection->join(''),    '123',       'right result';
82 is $collection->join('---'), '1---2---3', 'right result';
83 is $collection->join("\n"),  "1\n2\n3",   'right result';
84 #is $collection->join('/')->url_escape, '1%2F2%2F3', 'right result'; # no bytestream object
85
86 # map
87 $collection = c(1, 2, 3);
88 is $collection->map(sub { $_ + 1 })->join(''), '234', 'right result';
89 is_deeply [@$collection], [1, 2, 3], 'right elements';
90 is $collection->map(sub { shift() + 2 })->join(''), '345', 'right result';
91 is_deeply [@$collection], [1, 2, 3], 'right elements';
92 $collection = c(c(1, 2, 3), c(4, 5, 6), c(7, 8, 9));
93 is $collection->map('reverse')->map(join => "\n")->join("\n"),
94   "3\n2\n1\n6\n5\n4\n9\n8\n7", 'right result';
95 is $collection->map(join => '-')->join("\n"), "1-2-3\n4-5-6\n7-8-9",
96   'right result';
97
98 # reverse
99 $collection = c(3, 2, 1);
100 is_deeply $collection->reverse->to_array, [1, 2, 3], 'right order';
101 $collection = c(3);
102 is_deeply $collection->reverse->to_array, [3], 'right order';
103 $collection = c();
104 is_deeply $collection->reverse->to_array, [], 'no elements';
105
106 # shuffle
107 $collection = c(0 .. 10000);
108 my $random = $collection->shuffle;
109 is $collection->size, $random->size, 'same number of elements';
110 isnt "@$collection", "@$random", 'different order';
111 is_deeply c()->shuffle->to_array, [], 'no elements';
112
113 # size
114 $collection = c();
115 is $collection->size, 0, 'right size';
116 $collection = c(undef);
117 is $collection->size, 1, 'right size';
118 $collection = c(23);
119 is $collection->size, 1, 'right size';
120 $collection = c([2, 3]);
121 is $collection->size, 1, 'right size';
122 $collection = c(5, 4, 3, 2, 1);
123 is $collection->size, 5, 'right size';
124
125 # reduce
126 $collection = c(2, 5, 4, 1);
127 is $collection->reduce(sub { $a + $b }), 12, 'right result';
128 is $collection->reduce(sub { $a + $b }, 5), 17, 'right result';
129 is c()->reduce(sub { $a + $b }), undef, 'no result';
130
131 # sort
132 $collection = c(2, 5, 4, 1);
133 is_deeply $collection->sort->to_array, [1, 2, 4, 5], 'right order';
134 is_deeply $collection->sort(sub { $b cmp $a })->to_array, [5, 4, 2, 1],
135   'right order';
136 is_deeply $collection->sort(sub { $_[1] cmp $_[0] })->to_array, [5, 4, 2, 1],
137   'right order';
138 $collection = c(qw(Test perl DOM));
139 is_deeply $collection->sort(sub { uc(shift) cmp uc(shift) })->to_array,
140   [qw(DOM perl Test)], 'right order';
141 $collection = c();
142 is_deeply $collection->sort->to_array, [], 'no elements';
143 is_deeply $collection->sort(sub { $a cmp $b })->to_array, [], 'no elements';
144
145 # slice
146 $collection = c(1, 2, 3, 4, 5, 6, 7, 10, 9, 8);
147 is_deeply $collection->slice(0)->to_array,  [1], 'right result';
148 is_deeply $collection->slice(1)->to_array,  [2], 'right result';
149 is_deeply $collection->slice(2)->to_array,  [3], 'right result';
150 is_deeply $collection->slice(-1)->to_array, [8], 'right result';
151 is_deeply $collection->slice(-3, -5)->to_array, [10, 6], 'right result';
152 is_deeply $collection->slice(1, 2, 3)->to_array, [2, 3, 4], 'right result';
153 is_deeply $collection->slice(6, 1, 4)->to_array, [7, 2, 5], 'right result';
154 is_deeply $collection->slice(6 .. 9)->to_array, [7, 10, 9, 8], 'right result';
155
156 # uniq
157 $collection = c(1, 2, 3, 2, 3, 4, 5, 4);
158 is_deeply $collection->uniq->to_array, [1, 2, 3, 4, 5], 'right result';
159 is_deeply $collection->uniq->reverse->uniq->to_array, [5, 4, 3, 2, 1],
160   'right result';
161 $collection = c([1, 2, 3], [3, 2, 1], [3, 1, 2]);
162 is_deeply $collection->uniq(sub { $_->[1] }), [[1, 2, 3], [3, 1, 2]],
163   'right result';
164 $collection = c(c(1, 2), c(1, 2), c(2, 1));
165 is_deeply $collection->uniq(join => ',')->flatten->to_array, [1, 2, 2, 1],
166   'right result';
167
168 # TO_JSON
169 is encode_json(c(1, 2, 3)), '[1,2,3]', 'right result';
170
171 done_testing();