forked from makeey/ArrayList
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListSpec.php
More file actions
381 lines (320 loc) · 15.7 KB
/
Copy pathArrayListSpec.php
File metadata and controls
381 lines (320 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
namespace ArrayList\Spec;
use ArrayList\ArrayList;
use ArrayList\ClassCastException;
use ArrayList\Collection;
use Sevavietl\OverloadedFunction\UnknownSignatureException;
describe('ArrayList', function () {
given('arrayList', function () {
return new ArrayList('string');
});
describe('instance of Collection test', function () {
it('return "Collection" instance', function () {
expect($this->arrayList)->toBeAnInstanceOf(Collection::class);
});
});
describe('Construct with array value', function () {
it('Construct with array value', function () {
$arrayListWithArray = new ArrayList('string', ['string1', 'string2', 'string3']);
expect($arrayListWithArray)->toBeAnInstanceOf(ArrayList::class);
expect($arrayListWithArray->toArray())->toBeA('array')->toBe(['string1', 'string2', 'string3']);
});
});
describe('Construct with Collection Value', function () {
it('Construct with Collection Value', function () {
$arrayListWithArray = new ArrayList('string', ['string1', 'string2', 'string3']);
$newArrayListWithCollectionInterface = new ArrayList('string', $arrayListWithArray);
expect($newArrayListWithCollectionInterface)->toBeAnInstanceOf(ArrayList::class);
expect($newArrayListWithCollectionInterface->toArray())->toBeA('array')->toBe(['string1', 'string2', 'string3']);
});
});
describe('Is empty must be true', function () {
it('is empty === true', function () {
expect($this->arrayList->isEmpty())->toEqual(true);
});
});
describe('It is add with wrong type arguments', function () {
it('it is must be a exception "UnknownSignatureException" ', function () {
$closure = function () {
$this->arrayList->add(2);
};
expect($closure)->toThrow(new UnknownSignatureException());
});
});
describe('Appends the specified element to the end of this list.', function () {
it('it is must be a return a true', function () {
expect($this->arrayList->add('element1'))->toBe(true);
});
it('is empty === false', function () {
$this->arrayList->add('string2');
expect($this->arrayList->isEmpty())->toEqual(false);
});
});
describe('It is add with index and correct type', function () {
$this->arrayList->add('string1');
$this->arrayList->add('string2');
$this->arrayList->add('string3');
$this->arrayList->add(1, 'string_from_add');
it('is empty === false', function () {
expect($this->arrayList->isEmpty())->toEqual(false);
});
it('it must be true', function () {
expect($this->arrayList->get(1))->toBe('string_from_add');
expect($this->arrayList->get(2))->toBe('string2');
});
});
describe('It is get elements by index', function () {
$this->arrayList->add(2, 'string2');
it('get index 0 must be have value "string2"', function () {
expect($this->arrayList->get(0))->toEqual('string2');
});
it('Must throw exception "OutOfBoundsException"', function () {
$closure = function () {
$this->arrayList->get(3);
};
expect($closure)->toThrow(new \OutOfBoundsException());
});
});
describe('Replaces the element at the specified position in this list with the specified element.', function () {
it('Must throw exception "OutOfBoundsException"', function () {
$closure = function () {
$this->arrayList->set(2, 'string2');
};
expect($closure)->toThrow(new \OutOfBoundsException());
});
it('Must throw exception InvalidArgumentException', function () {
$closure = function () {
$this->arrayList->add(2, 'string1');
$this->arrayList->set(0, 1);
};
expect($closure)->toThrow(new \InvalidArgumentException());
});
it('It is must be replace element', function () {
$this->arrayList->add('string1');
$this->arrayList->set(0, 'string2');
expect($this->arrayList->get(0))->toEqual('string2');
});
});
describe('Returns the number of elements in this list.', function () {
it('Must be 0', function () {
expect($this->arrayList->size())->toEqual(0);
});
it('Must be 2', function () {
$this->arrayList->add('string1');
$this->arrayList->add('string2');
expect($this->arrayList->size())->toEqual(2);
});
});
describe('Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).', function () {
it('Must be return false', function () {
expect($this->arrayList->contains('string1'))->toEqual(false);
});
it('Must be return true', function () {
$this->arrayList->add('string1');
expect($this->arrayList->contains('string1'))->toEqual(true);
});
});
describe('indexOf Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.',
function () {
it('Must be -1', function () {
expect($this->arrayList->indexOf('string1'))->toEqual(-1);
});
it('Must be 0', function () {
$this->arrayList->add('string1');
expect($this->arrayList->indexOf('string1'))->toEqual(0);
});
it('Must be 0 too', function () {
$this->arrayList->add('string1');
$this->arrayList->add('string1');
expect($this->arrayList->indexOf('string1'))->toEqual(0);
});
}
);
describe('lastIndexOf Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.',
function () {
it('Must be -1', function () {
expect($this->arrayList->lastIndexOf('string1'))->toEqual(-1);
});
it('Must be 0', function () {
$this->arrayList->add('string1');
expect($this->arrayList->lastIndexOf('string1'))->toEqual(0);
});
it('Must be 1', function () {
$this->arrayList->add('string1');
$this->arrayList->add('string1');
expect($this->arrayList->lastIndexOf('string1'))->toEqual(1);
});
}
);
describe('clear Removes all of the elements from this list. The list will be empty after this call returns.',
function () {
it('It is call method clear', function () {
$this->arrayList->add('string1');
$this->arrayList->add('string1');
expect($this->arrayList->isEmpty())->toEqual(false);
$this->arrayList->clear();
expect($this->arrayList->isEmpty())->toEqual(true);
});
}
);
describe('addAll Appends all of the elements in the
specified collection to the end of this list, in the order that they are returned by the specified collection\'s Iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the
behavior of this call is undefined if the specified collection is this list, and this list is nonempty.)',
function () {
it('It is add collection ', function () {
$this->arrayList->add('string1');
$tmp_array_list = new ArrayList('string');
$tmp_array_list->add('string2');
$this->arrayList->addAll($tmp_array_list);
expect($this->arrayList->isEmpty())->toEqual(false);
expect($this->arrayList->size())->toEqual(2);
expect($this->arrayList->get(0))->toEqual('string1');
expect($this->arrayList->get(1))->toEqual('string2');
});
it('It is add collection by index', function () {
$this->arrayList->add('string1');
$this->arrayList->add('string2');
$this->arrayList->add('string3');
$tmp_array_list = new ArrayList('string',['string']);
$this->arrayList->addAll(1,$tmp_array_list);
expect($this->arrayList->isEmpty())->toEqual(false);
expect($this->arrayList->get(0))->toEqual('string1');
expect($this->arrayList->get(1))->toEqual('string');
expect($this->arrayList->get(2))->toEqual('string2');
});
}
);
describe('contains All Returns true if this collection contains all of the elements in the specified collection.
This implementation iterates over the specified collection, checking each element returned by the iterator in turn to see if it\'s contained in this collection.
If all elements are so contained true is returned, otherwise false.',
function () {
it('containsAll test', function () {
$this->arrayList->add('string1');
$this->arrayList->add('string2');
$this->arrayList->add('string3');
$containsArrayList = new ArrayList('string');
$containsArrayList->add('string2');
$containsArrayList->add('string3');
expect($this->arrayList->containsAll($containsArrayList))->toBe(true);
});
it('containsAll test false', function () {
$this->arrayList->add('string1');
$this->arrayList->add('string2');
$this->arrayList->add('string3');
$containsArrayList = new ArrayList('string');
$containsArrayList->add('string2');
$containsArrayList->add('string5');
expect($this->arrayList->containsAll($containsArrayList))->toBe(false);
});
it('containsAll test with empty array', function () {
$this->arrayList->add('string1');
$this->arrayList->add('string2');
$this->arrayList->add('string3');
$closure = function () {
$containsArrayList = new ArrayList('string');
$this->arrayList->containsAll($containsArrayList);
};
expect($closure)->toThrow(new \InvalidArgumentException());
});
it('containsAll test with collection wrong type', function () {
$this->arrayList->add('string1');
$this->arrayList->add('string2');
$this->arrayList->add('string3');
$closure = function () {
$containsArrayList = new ArrayList('integer');
$containsArrayList->add(2);
$this->arrayList->containsAll($containsArrayList);
};
expect($closure)->toThrow(new ClassCastException());
});
}
);
describe('to array',
function () {
it('to array test', function () {
$this->arrayList->add('string1');
$this->arrayList->add('string2');
$this->arrayList->add('string3');
expect($this->arrayList->toArray())->toBeA('array')->toBe(['string1', 'string2', 'string3']);
});
}
);
describe('remove element by index and object',
function () {
it('to remove test', function () {
$this->arrayList->add('string1');
$this->arrayList->add('string2');
$this->arrayList->add('string3');
expect($this->arrayList->toArray())->toBeA('array')->toBe(['string1', 'string2', 'string3']);
$this->arrayList->remove(0);
expect($this->arrayList->toArray())->toBeA('array')->toBe(['string2', 'string3']);
expect($this->arrayList->get(0))->toBe('string2');
});
it('to remove test object', function () {
$this->arrayList->add('string1');
$this->arrayList->add('string2');
$this->arrayList->add('string3');
expect($this->arrayList->toArray())->toBeA('array')->toBe(['string1', 'string2', 'string3']);
$this->arrayList->remove('string1');
expect($this->arrayList->toArray())->toBeA('array')->toBe(['string2', 'string3']);
expect($this->arrayList->get(0))->toBe('string2');
expect($this->arrayList->size())->toBe(2);
});
}
);
describe('remove elements in collections',
function () {
it('to remove collection', function () {
$this->arrayList->add('string1');
$this->arrayList->add('string2');
$this->arrayList->add('string3');
$removeArrayList = new ArrayList('string',['string1','string2']);
expect($this->arrayList->toArray())->toBeA('array')->toBe(['string1', 'string2', 'string3']);
$this->arrayList->removeAll($removeArrayList);
expect($this->arrayList->toArray())->toBeA('array')->toBe(['string3']);
expect($this->arrayList->get(0))->toBe('string3');
expect($this->arrayList->size())->toBe(1);
});
it('to remove collection with wrong type', function () {
$this->arrayList->add('1');
$this->arrayList->add('2');
$this->arrayList->add('3');
expect($this->arrayList->toArray())->toBeA('array')->toBe(['1', '2', '3']);
$closure = function(){
$removeArrayList = new ArrayList('integer',[1,2]);
$this->arrayList->removeAll($removeArrayList);
};
expect($closure)->toThrow(new ClassCastException());
});
}
);
describe('remove range from collection',
function () {
it('to remove collection', function () {
$this->arrayList->add('string1');
$this->arrayList->add('string2');
$this->arrayList->add('string3');
$this->arrayList->add('string4');
$this->arrayList->removeRange(1,2);
expect($this->arrayList->toArray())->toBeA('array')->toBe(['string1','string4']);
expect($this->arrayList->get(0))->toBe('string1');
expect($this->arrayList->size())->toBe(2);
});
it('to remove range with wrong index', function () {
$this->arrayList->add('1');
$this->arrayList->add('2');
$this->arrayList->add('3');
expect($this->arrayList->toArray())->toBeA('array')->toBe(['1', '2', '3']);
$closure = function(){
$this->arrayList->removeRange(10,15);
};
expect($closure)->toThrow(new \OutOfBoundsException());
});
it('to remove range with wrong range', function () {
$this->arrayList->add('1');
$this->arrayList->removeRange(0,0);
expect($this->arrayList->size())->toBe(1);
});
}
);
});