MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1import { test, testDeep, summary } from './helpers.js';
2
3console.log('Spread Tests\n');
4
5const arr1 = [1, 2, 3];
6const arr2 = [...arr1, 4, 5];
7testDeep('spread array', arr2, [1, 2, 3, 4, 5]);
8
9const arr3 = [0, ...arr1];
10testDeep('spread at start', arr3, [0, 1, 2, 3]);
11
12const arr4 = [0, ...arr1, 4];
13testDeep('spread in middle', arr4, [0, 1, 2, 3, 4]);
14
15const copy = [...arr1];
16testDeep('array copy', copy, [1, 2, 3]);
17test('array copy is new ref', copy !== arr1, true);
18
19const merged = [...[1, 2], ...[3, 4]];
20testDeep('merge arrays', merged, [1, 2, 3, 4]);
21
22const obj1 = { a: 1, b: 2 };
23const obj2 = { ...obj1, c: 3 };
24test('spread object a', obj2.a, 1);
25test('spread object c', obj2.c, 3);
26
27const obj3 = { ...obj1, b: 10 };
28test('spread override', obj3.b, 10);
29
30const objCopy = { ...obj1 };
31test('object copy a', objCopy.a, 1);
32test('object copy is new ref', objCopy !== obj1, true);
33
34const mergedObj = { ...{ x: 1 }, ...{ y: 2 } };
35test('merge objects x', mergedObj.x, 1);
36test('merge objects y', mergedObj.y, 2);
37
38function sum(a, b, c) {
39 return a + b + c;
40}
41test('spread in call', sum(...[1, 2, 3]), 6);
42
43const chars = [...'hello'];
44testDeep('spread string', chars, ['h', 'e', 'l', 'l', 'o']);
45
46summary();