1
exercism fetch ecmascript list-ops

list-ops.spec.js

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
import List from './list-ops';


describe('append entries to a list and return the new list', () => {
  test('empty lists', () => {
    const list1 = new List();
    const list2 = new List();
    expect(list1.append(list2)).toEqual(new List());
  });

  xtest('empty list to list', () => {
    const list1 = new List([1, 2, 3, 4]);
    const list2 = new List();
    expect(list1.append(list2)).toEqual(list1);
  });

  xtest('non-empty lists', () => {
    const list1 = new List([1, 2]);
    const list2 = new List([2, 3, 4, 5]);
    expect(list1.append(list2).values).toEqual([1, 2, 2, 3, 4, 5]);
  });
});


describe('concat lists and lists of lists into new list', () => {
  xtest('empty list', () => {
    const list1 = new List();
    const list2 = new List();
    expect(list1.concat(list2).values).toEqual([]);
  });

  xtest('list of lists', () => {
    const list1 = new List([1, 2]);
    const list2 = new List([3]);
    const list3 = new List([]);
    const list4 = new List([4, 5, 6]);
    expect(list1.concat(list2).concat(list3).concat(list4).values).toEqual([1, 2, 3, 4, 5, 6]);
  });
});


describe('filter list returning only values that satisfy the filter function', () => {
  xtest('empty list', () => {
    const list1 = new List([]);
    expect(list1.filter(el => el % 2 === 1).values).toEqual([]);
  });

  xtest('non empty list', () => {
    const list1 = new List([1, 2, 3, 5]);
    expect(list1.filter(el => el % 2 === 1).values).toEqual([1, 3, 5]);
  });
});


describe('returns the length of a list', () => {
  xtest('empty list', () => {
    const list1 = new List();
    expect(list1.length()).toEqual(0);
  });

  xtest('non-empty list', () => {
    const list1 = new List([1, 2, 3, 4]);
    expect(list1.length()).toEqual(4);
  });
});


describe('returns a list of elements whose values equal the list value transformed by the mapping function', () => {
  xtest('empty list', () => {
    const list1 = new List();
    expect(list1.map(el => ++el).values).toEqual([]);
  });

  xtest('non-empty list', () => {
    const list1 = new List([1, 3, 5, 7]);
    expect(list1.map(el => ++el).values).toEqual([2, 4, 6, 8]);
  });
});


describe('folds (reduces) the given list from the left with a function', () => {
  xtest('empty list', () => {
    const list1 = new List();
    expect(list1.foldl((acc, el) => el / acc, 2)).toEqual(2);
  });

  xtest('division of integers', () => {
    const list1 = new List([1, 2, 3, 4]);
    expect(list1.foldl((acc, el) => el / acc, 24)).toEqual(64);
  });
});


describe('folds (reduces) the given list from the right with a function', () => {
  xtest('empty list', () => {
    const list1 = new List();
    expect(list1.foldr((acc, el) => el / acc, 2)).toEqual(2);
  });

  xtest('division of integers', () => {
    const list1 = new List([1, 2, 3, 4]);
    expect(list1.foldr((acc, el) => el / acc, 24)).toEqual(9);
  });
});


describe('reverse the elements of a list', () => {
  xtest('empty list', () => {
    const list1 = new List();
    expect(list1.reverse().values).toEqual([]);
  });

  xtest('non-empty list', () => {
    const list1 = new List([1, 3, 5, 7]);
    expect(list1.reverse().values).toEqual([7, 5, 3, 1]);
  });
});