MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

improve array spec test

+165 -14
+165 -14
examples/spec/arrays.js
··· 33 33 test('pop returns last', popped, 6); 34 34 test('pop decreases length', arr.length, 5); 35 35 36 - let nested = [[1, 2], [3, 4]]; 36 + let nested = [ 37 + [1, 2], 38 + [3, 4] 39 + ]; 37 40 test('nested array access', nested[0][0], 1); 38 41 test('nested array access deep', nested[1][1], 4); 39 42 ··· 44 47 test('Array with elements', arr3.length, 3); 45 48 test('Array with elements value', arr3[1], 20); 46 49 47 - test('instanceof Array true', [1,2] instanceof Array, true); 48 - test('instanceof Array false for object', ({}) instanceof Array, false); 50 + test('instanceof Array true', [1, 2] instanceof Array, true); 51 + test('instanceof Array false for object', {} instanceof Array, false); 49 52 50 53 let obj = [1, 2, 3]; 51 54 obj['foo'] = 'bar'; ··· 75 78 test('unshift adds to front', unshifted[0], 1); 76 79 test('unshift increases length', unshifted.length, 3); 77 80 78 - testDeep('map', [1, 2, 3].map(x => x * 2), [2, 4, 6]); 79 - testDeep('filter', [1, 2, 3, 4].filter(x => x % 2 === 0), [2, 4]); 80 - test('reduce sum', [1, 2, 3].reduce((a, b) => a + b, 0), 6); 81 - test('find', [1, 2, 3].find(x => x > 1), 2); 82 - test('findIndex', [1, 2, 3].findIndex(x => x > 1), 1); 83 - test('every true', [2, 4, 6].every(x => x % 2 === 0), true); 84 - test('every false', [2, 3, 6].every(x => x % 2 === 0), false); 85 - test('some true', [1, 2, 3].some(x => x > 2), true); 86 - test('some false', [1, 2, 3].some(x => x > 5), false); 81 + testDeep( 82 + 'map', 83 + [1, 2, 3].map(x => x * 2), 84 + [2, 4, 6] 85 + ); 86 + testDeep( 87 + 'filter', 88 + [1, 2, 3, 4].filter(x => x % 2 === 0), 89 + [2, 4] 90 + ); 91 + test( 92 + 'reduce sum', 93 + [1, 2, 3].reduce((a, b) => a + b, 0), 94 + 6 95 + ); 96 + test( 97 + 'find', 98 + [1, 2, 3].find(x => x > 1), 99 + 2 100 + ); 101 + test( 102 + 'findIndex', 103 + [1, 2, 3].findIndex(x => x > 1), 104 + 1 105 + ); 106 + test( 107 + 'every true', 108 + [2, 4, 6].every(x => x % 2 === 0), 109 + true 110 + ); 111 + test( 112 + 'every false', 113 + [2, 3, 6].every(x => x % 2 === 0), 114 + false 115 + ); 116 + test( 117 + 'some true', 118 + [1, 2, 3].some(x => x > 2), 119 + true 120 + ); 121 + test( 122 + 'some false', 123 + [1, 2, 3].some(x => x > 5), 124 + false 125 + ); 87 126 88 127 testDeep('Array.from string', Array.from('abc'), ['a', 'b', 'c']); 89 128 testDeep('Array.of', Array.of(1, 2, 3), [1, 2, 3]); 90 129 test('instanceof Array true', [] instanceof Array, true); 91 - test('instanceof Array false', ({}) instanceof Array, false); 130 + test('instanceof Array false', {} instanceof Array, false); 92 131 93 132 testDeep('flat', [1, [2, 3]].flat(), [1, 2, 3]); 94 - testDeep('flatMap', [1, 2].flatMap(x => [x, x * 2]), [1, 2, 2, 4]); 133 + testDeep( 134 + 'flatMap', 135 + [1, 2].flatMap(x => [x, x * 2]), 136 + [1, 2, 2, 4] 137 + ); 95 138 96 139 let filled = new Array(3).fill(0); 97 140 testDeep('fill', filled, [0, 0, 0]); ··· 101 144 102 145 let sortedDesc = [1, 2, 3].sort((a, b) => b - a); 103 146 testDeep('sort descending', sortedDesc, [3, 2, 1]); 147 + 148 + let orig = [3, 1, 4, 1, 5]; 149 + testDeep('toSorted returns sorted copy', orig.toSorted(), [1, 1, 3, 4, 5]); 150 + testDeep('toSorted does not mutate', orig, [3, 1, 4, 1, 5]); 151 + testDeep( 152 + 'toSorted with compareFn', 153 + [3, 1, 2].toSorted((a, b) => b - a), 154 + [3, 2, 1] 155 + ); 156 + 157 + testDeep('toReversed returns reversed copy', [1, 2, 3].toReversed(), [3, 2, 1]); 158 + let orig2 = [1, 2, 3]; 159 + orig2.toReversed(); 160 + testDeep('toReversed does not mutate', orig2, [1, 2, 3]); 161 + 162 + testDeep('toSpliced removes elements', [1, 2, 3, 4, 5].toSpliced(1, 2), [1, 4, 5]); 163 + testDeep('toSpliced inserts elements', [1, 2, 3].toSpliced(1, 0, 99), [1, 99, 2, 3]); 164 + testDeep('toSpliced replaces elements', [1, 2, 3, 4, 5].toSpliced(1, 2, 99), [1, 99, 4, 5]); 165 + let orig3 = [1, 2, 3]; 166 + orig3.toSpliced(1, 1); 167 + testDeep('toSpliced does not mutate', orig3, [1, 2, 3]); 168 + 169 + testDeep('with replaces element', [1, 2, 3].with(1, 99), [1, 99, 3]); 170 + testDeep('with negative index', [1, 2, 3].with(-1, 99), [1, 2, 99]); 171 + let orig4 = [1, 2, 3]; 172 + orig4.with(0, 99); 173 + testDeep('with does not mutate', orig4, [1, 2, 3]); 174 + 175 + test( 176 + 'findLast', 177 + [1, 2, 3, 2, 1].findLast(x => x === 2), 178 + 2 179 + ); 180 + test( 181 + 'findLast returns last match', 182 + [1, 2, 3, 4, 5].findLast(x => x > 2), 183 + 5 184 + ); 185 + test( 186 + 'findLast not found', 187 + [1, 2, 3].findLast(x => x > 5), 188 + undefined 189 + ); 190 + test( 191 + 'findLastIndex', 192 + [1, 2, 3, 2, 1].findLastIndex(x => x === 2), 193 + 3 194 + ); 195 + test( 196 + 'findLastIndex not found', 197 + [1, 2, 3].findLastIndex(x => x > 5), 198 + -1 199 + ); 200 + 201 + test('at positive', [1, 2, 3].at(1), 2); 202 + test('at negative', [1, 2, 3].at(-1), 3); 203 + test('at out of bounds', [1, 2, 3].at(5), undefined); 204 + 205 + let forEachResult = []; 206 + [1, 2, 3].forEach(x => forEachResult.push(x * 2)); 207 + testDeep('forEach', forEachResult, [2, 4, 6]); 208 + 209 + test( 210 + 'reduceRight', 211 + [1, 2, 3].reduceRight((acc, x) => acc + x, 0), 212 + 6 213 + ); 214 + test( 215 + 'reduceRight order', 216 + ['a', 'b', 'c'].reduceRight((acc, x) => acc + x, ''), 217 + 'cba' 218 + ); 219 + 220 + test('lastIndexOf found', [1, 2, 3, 2, 1].lastIndexOf(2), 3); 221 + test('lastIndexOf not found', [1, 2, 3].lastIndexOf(5), -1); 222 + 223 + testDeep('copyWithin', [1, 2, 3, 4, 5].copyWithin(0, 3), [4, 5, 3, 4, 5]); 224 + testDeep('copyWithin with end', [1, 2, 3, 4, 5].copyWithin(1, 3, 4), [1, 4, 3, 4, 5]); 225 + 226 + let spliceArr = [1, 2, 3, 4, 5]; 227 + let removed = spliceArr.splice(1, 2); 228 + testDeep('splice returns removed', removed, [2, 3]); 229 + testDeep('splice mutates array', spliceArr, [1, 4, 5]); 230 + 231 + let spliceArr2 = [1, 2, 3]; 232 + spliceArr2.splice(1, 0, 99, 100); 233 + testDeep('splice insert', spliceArr2, [1, 99, 100, 2, 3]); 234 + 235 + let entriesArr = ['a', 'b', 'c']; 236 + let entriesResult = []; 237 + for (let [i, v] of entriesArr.entries()) entriesResult.push([i, v]); 238 + testDeep('entries', entriesResult, [ 239 + [0, 'a'], 240 + [1, 'b'], 241 + [2, 'c'] 242 + ]); 243 + 244 + let keysResult = []; 245 + for (let k of ['a', 'b', 'c'].keys()) keysResult.push(k); 246 + testDeep('keys', keysResult, [0, 1, 2]); 247 + 248 + let valuesResult = []; 249 + for (let v of ['a', 'b', 'c'].values()) valuesResult.push(v); 250 + testDeep('values', valuesResult, ['a', 'b', 'c']); 251 + 252 + test('Array.isArray true', Array.isArray([1, 2, 3]), true); 253 + test('Array.isArray false object', Array.isArray({}), false); 254 + test('Array.isArray false string', Array.isArray('abc'), false); 104 255 105 256 summary();