Skip to content

Commit 7e0a0b5

Browse files
authored
Merge pull request #1 from makeey/dev
Added spec test
2 parents cb2f3f3 + 15285a5 commit 7e0a0b5

File tree

1 file changed

+385
-0
lines changed

1 file changed

+385
-0
lines changed

spec/ArrayListSpec.php

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

0 commit comments

Comments
 (0)