-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathassertArrays.js
111 lines (94 loc) · 3.06 KB
/
assertArrays.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
const doesContain = function(array, element) {
return array.indexOf(element) !== -1;
};
const doesContainAll = function(array, values) {
return values.every((element) => doesContain(array, element));
};
const doesContainAny = function(array, values) {
return values.some((element) => doesContain(array, element));
};
const compare = function(actual, expected) {
return actual.length === expected.length && actual.every((value, index) => value === expected[index]);
};
const isSorted = function(array, fn) {
return compare(array, array.slice().sort(fn));
};
module.exports = (chai) => {
chai.Assertion.addMethod('containing', function(value) {
this.assert(
doesContain(this._obj, value),
`expected #{this} to be containing ${value}`,
`expected #{this} not to be containing ${value}`
);
});
chai.Assertion.addMethod('containingAllOf', function(values) {
this.assert(
doesContainAll(this._obj, values),
`expected #{this} to be containing all of [${values}]`,
`expected #{this} not to be containing all of [${values}]`
);
});
chai.Assertion.addMethod('containingAnyOf', function(values) {
this.assert(
doesContainAny(this._obj, values),
`expected #{this} to be containing any of [${values}]`,
`expected #{this} not to be containing any of [${values}]`
);
});
chai.Assertion.addMethod('equalTo', function(values) {
this.assert(
compare(this._obj, values),
`expected #{this} to be equal to [${values}]`,
`expected #{this} not to be equal to [${values}]`
);
});
chai.Assertion.addMethod('ofSize', function(length) {
this.assert(
this._obj.length === length,
`expected #{this} to be of size ${length}`,
`expected #{this} not to be of size ${length}`
);
});
chai.Assertion.addMethod('array', function() {
this.assert(
this._obj instanceof Array,
'expected #{this} to be an Array',
'expected #{this} not to be an Array'
);
});
chai.Assertion.addMethod('Uint8Array', function() {
this.assert(
this._obj instanceof Uint8Array,
'expected #{this} to be an Uint8Array',
'expected #{this} not to be an Uint8Array'
);
});
chai.Assertion.addMethod('Uint16Array', function() {
this.assert(
this._obj instanceof Uint16Array,
'expected #{this} to be an Uint16Array',
'expected #{this} not to be an Uint16Array'
);
});
chai.Assertion.addMethod('Uint32Array', function() {
this.assert(
this._obj instanceof Uint32Array,
'expected #{this} to be an Uint32Array',
'expected #{this} not to be an Uint32Array'
);
});
chai.Assertion.addMethod('Uint8ClampedArray', function() {
this.assert(
this._obj instanceof Uint8ClampedArray,
'expected #{this} to be an Uint8ClampedArray',
'expected #{this} not to be an Uint8ClampedArray'
);
});
chai.Assertion.addMethod('sorted', function(sortfn) {
this.assert(
isSorted(this._obj, sortfn),
'expected #{this} to be sorted',
'expected #{this} not to be sorted'
);
});
};