33d36a14d46d6394d4053327b2a292e9ec6a4aa5
[aai/esr-gui.git] /
1
2 var sliced = require('../')
3 var assert = require('assert')
4
5 describe('sliced', function(){
6   it('exports a function', function(){
7     assert.equal('function', typeof sliced);
8   })
9   describe('with 1 arg', function(){
10     it('returns an array of the arg', function(){
11       var o = [3, "4", {}];
12       var r = sliced(o);
13       assert.equal(3, r.length);
14       assert.equal(o[0], r[0]);
15       assert.equal(o[1], r[1]);
16       assert.equal(o[1], r[1]);
17     })
18   })
19   describe('with 2 args', function(){
20     it('returns an array of the arg starting at the 2nd arg', function(){
21       var o = [3, "4", 5, null];
22       var r = sliced(o, 2);
23       assert.equal(2, r.length);
24       assert.equal(o[2], r[0]);
25       assert.equal(o[3], r[1]);
26     })
27   })
28   describe('with 3 args', function(){
29     it('returns an array of the arg from the 2nd to the 3rd arg', function(){
30       var o = [3, "4", 5, null];
31       var r = sliced(o, 1, 2);
32       assert.equal(1, r.length);
33       assert.equal(o[1], r[0]);
34     })
35   })
36   describe('with negative start and no end', function(){
37     it('begins at an offset from the end and includes all following elements', function(){
38       var o = [3, "4", 5, null];
39       var r = sliced(o, -2);
40       assert.equal(2, r.length);
41       assert.equal(o[2], r[0]);
42       assert.equal(o[3], r[1]);
43
44       var r = sliced(o, -12);
45       assert.equal(4, r.length);
46       assert.equal(o[0], r[0]);
47       assert.equal(o[1], r[1]);
48     })
49   })
50   describe('with negative start and positive end', function(){
51     it('begins at an offset from the end and includes `end` elements', function(){
52       var o = [3, "4", {x:1}, null];
53
54       var r = sliced(o, -2, 1);
55       assert.equal(0, r.length);
56
57       var r = sliced(o, -2, 2);
58       assert.equal(0, r.length);
59
60       var r = sliced(o, -2, 3);
61       assert.equal(1, r.length);
62       assert.equal(o[2], r[0]);
63     })
64   })
65   describe('with negative start and negative end', function(){
66     it('begins at `start` offset from the end and includes all elements up to `end` offset from the end', function(){
67       var o = [3, "4", {x:1}, null];
68       var r = sliced(o, -3, -1);
69       assert.equal(2, r.length);
70       assert.equal(o[1], r[0]);
71       assert.equal(o[2], r[1]);
72
73       var r = sliced(o, -3, -3);
74       assert.equal(0, r.length);
75
76       var r = sliced(o, -3, -4);
77       assert.equal(0, r.length);
78     })
79   })
80 })