@@ -135,6 +135,86 @@ defmodule AccessTest do
135
135
end
136
136
end
137
137
138
+ describe "slice/1" do
139
+ @ test_list [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
140
+
141
+ test "retrieves a range from the start of the list" do
142
+ assert [ 2 , 3 ] == get_in ( @ test_list , [ Access . slice ( 1 .. 2 ) ] )
143
+ end
144
+
145
+ test "retrieves a range from the end of the list" do
146
+ assert [ 6 , 7 ] == get_in ( @ test_list , [ Access . slice ( - 2 .. - 1 ) ] )
147
+ end
148
+
149
+ test "retrieves a range from positive first and negative last" do
150
+ assert [ 2 , 3 , 4 , 5 , 6 ] == get_in ( @ test_list , [ Access . slice ( 1 .. - 2 // 1 ) ] )
151
+ end
152
+
153
+ test "retrieves a range from negative first and positive last" do
154
+ assert [ 6 , 7 ] == get_in ( @ test_list , [ Access . slice ( - 2 .. 7 // 1 ) ] )
155
+ end
156
+
157
+ test "retrieves a range with steps" do
158
+ assert [ 1 , 3 ] == get_in ( @ test_list , [ Access . slice ( 0 .. 2 // 2 ) ] )
159
+ assert [ 2 , 5 ] == get_in ( @ test_list , [ Access . slice ( 1 .. 4 // 3 ) ] )
160
+ assert [ 2 ] == get_in ( @ test_list , [ Access . slice ( 1 .. 2 // 3 ) ] )
161
+ assert [ 1 , 3 , 5 , 7 ] == get_in ( @ test_list , [ Access . slice ( 0 .. 6 // 2 ) ] )
162
+ end
163
+
164
+ test "pops a range from the start of the list" do
165
+ assert { [ 2 , 3 ] , [ 1 , 4 , 5 , 6 , 7 ] } == pop_in ( @ test_list , [ Access . slice ( 1 .. 2 ) ] )
166
+ end
167
+
168
+ test "pops a range from the end of the list" do
169
+ assert { [ 6 , 7 ] , [ 1 , 2 , 3 , 4 , 5 ] } == pop_in ( @ test_list , [ Access . slice ( - 2 .. - 1 ) ] )
170
+ end
171
+
172
+ test "pops a range from positive first and negative last" do
173
+ assert { [ 2 , 3 , 4 , 5 , 6 ] , [ 1 , 7 ] } == pop_in ( @ test_list , [ Access . slice ( 1 .. - 2 // 1 ) ] )
174
+ end
175
+
176
+ test "pops a range from negative first and positive last" do
177
+ assert { [ 6 , 7 ] , [ 1 , 2 , 3 , 4 , 5 ] } == pop_in ( @ test_list , [ Access . slice ( - 2 .. 7 // 1 ) ] )
178
+ end
179
+
180
+ test "pops a range with steps" do
181
+ assert { [ 1 , 3 , 5 ] , [ 2 , 4 , 6 , 7 ] } == pop_in ( @ test_list , [ Access . slice ( 0 .. 4 // 2 ) ] )
182
+ assert { [ 2 ] , [ 1 , 3 , 4 , 5 , 6 , 7 ] } == pop_in ( @ test_list , [ Access . slice ( 1 .. 2 // 2 ) ] )
183
+ assert { [ 1 , 4 ] , [ 1 , 2 , 5 , 6 , 7 ] } == pop_in ( [ 1 , 2 , 1 , 4 , 5 , 6 , 7 ] , [ Access . slice ( 2 .. 3 ) ] )
184
+ end
185
+
186
+ test "updates range from the start of the list" do
187
+ assert [ - 1 , 2 , 3 , 4 , 5 , 6 , 7 ] == update_in ( @ test_list , [ Access . slice ( 0 .. 0 ) ] , & ( & 1 * - 1 ) )
188
+
189
+ assert [ 1 , - 2 , - 3 , 4 , 5 , 6 , 7 ] == update_in ( @ test_list , [ Access . slice ( 1 .. 2 ) ] , & ( & 1 * - 1 ) )
190
+ end
191
+
192
+ test "updates range from the end of the list" do
193
+ assert [ 1 , 2 , 3 , 4 , 5 , - 6 , - 7 ] == update_in ( @ test_list , [ Access . slice ( - 2 .. - 1 ) ] , & ( & 1 * - 1 ) )
194
+
195
+ assert [ - 1 , - 2 , 3 , 4 , 5 , 6 , 7 ] == update_in ( @ test_list , [ Access . slice ( - 7 .. - 6 ) ] , & ( & 1 * - 1 ) )
196
+ end
197
+
198
+ test "updates a range from positive first and negative last" do
199
+ assert [ 1 , - 2 , - 3 , - 4 , - 5 , - 6 , 7 ] ==
200
+ update_in ( @ test_list , [ Access . slice ( 1 .. - 2 // 1 ) ] , & ( & 1 * - 1 ) )
201
+ end
202
+
203
+ test "updates a range from negative first and positive last" do
204
+ assert [ 1 , 2 , 3 , 4 , 5 , - 6 , - 7 ] ==
205
+ update_in ( @ test_list , [ Access . slice ( - 2 .. 7 // 1 ) ] , & ( & 1 * - 1 ) )
206
+ end
207
+
208
+ test "updates a range with steps" do
209
+ assert [ - 1 , 2 , - 3 , 4 , - 5 , 6 , 7 ] ==
210
+ update_in ( @ test_list , [ Access . slice ( 0 .. 4 // 2 ) ] , & ( & 1 * - 1 ) )
211
+ end
212
+
213
+ test "returns empty when the start of the range is greater than the end" do
214
+ assert [ ] == get_in ( @ test_list , [ Access . slice ( 2 .. 1 // 1 ) ] )
215
+ end
216
+ end
217
+
138
218
describe "at/1" do
139
219
@ test_list [ 1 , 2 , 3 , 4 , 5 , 6 ]
140
220
0 commit comments