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