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.

at master 134 lines 4.3 kB view raw
1import { test, summary } from './helpers.js'; 2 3console.log('Arrow Function Tests\n'); 4 5const basic = (a, b) => a + b; 6test('basic arrow function', basic(2, 3), 5); 7 8const single = x => x * 2; 9test('arrow single param no parens', single(5), 10); 10 11const singleParen = x => x * 3; 12test('arrow single param with parens', singleParen(4), 12); 13 14const noParams = () => 42; 15test('arrow no params', noParams(), 42); 16 17const implicit = (a, b) => a - b; 18test('arrow implicit return', implicit(10, 3), 7); 19 20const trailingSingle = a => a + 1; 21test('arrow trailing comma single param', trailingSingle(4), 5); 22 23const trailingMulti = (a, b) => a * b; 24test('arrow trailing comma multi param', trailingMulti(4, 5), 20); 25 26const trailingDestructure = ({ value }, extra) => value + extra; 27test('arrow trailing comma destructuring param', trailingDestructure({ value: 7 }, 3), 10); 28 29const block = (a, b) => { 30 const sum = a + b; 31 return sum * 2; 32}; 33test('arrow block body', block(2, 3), 10); 34 35const withDefault = (a, b = 10) => a + b; 36test('arrow default param used', withDefault(5), 15); 37test('arrow default param overridden', withDefault(5, 3), 8); 38 39const defaultExpr = (x = 5 * 2) => x + 1; 40test('arrow default param expression', defaultExpr(), 11); 41test('arrow default param override expr', defaultExpr(7), 8); 42 43const multiDefault = (a = 1, b = 2, c = 3) => a + b + c; 44test('arrow multiple defaults all used', multiDefault(), 6); 45test('arrow multiple defaults partial', multiDefault(10), 15); 46test('arrow multiple defaults override all', multiDefault(10, 20, 30), 60); 47 48const defaultCall = (x = Math.random()) => x >= 0 && x <= 1; 49test('arrow default with call', defaultCall(), true); 50 51const assignmentDefault = (u = 42) => u * 2; 52test('arrow assignment in default', assignmentDefault(), 84); 53 54const parenDefault = (x = 5) => x + 1; 55test('arrow parenthesized default', parenDefault(), 6); 56 57const complexDefault = (x = (2 + 3) * 4) => x; 58test('arrow complex expression default', complexDefault(), 20); 59 60const rest = (first, ...others) => first + others.length; 61test('arrow rest params', rest(1, 2, 3, 4), 4); 62 63const restWithDefault = (a = 10, ...rest) => a + rest.length; 64test('arrow rest with default', restWithDefault(5, 1, 2), 7); 65 66const objReturn = (a, b) => ({ x: a, y: b }); 67test('arrow object return x', objReturn(1, 2).x, 1); 68test('arrow object return y', objReturn(1, 2).y, 2); 69 70const add = x => y => x + y; 71test('arrow currying', add(5)(3), 8); 72 73const fns = [x => x, x => x * 2, x => x * 3]; 74test('arrow in array [0]', fns[0](5), 5); 75test('arrow in array [1]', fns[1](5), 10); 76test('arrow in array [2]', fns[2](5), 15); 77 78const obj = { 79 double: x => x * 2, 80 triple: x => x * 3 81}; 82test('arrow in object double', obj.double(5), 10); 83test('arrow in object triple', obj.triple(5), 15); 84 85const context = { 86 value: 100, 87 getValue: function () { 88 return (() => this.value)(); 89 } 90}; 91test('arrow this binding', context.getValue(), 100); 92 93const mapped = [1, 2, 3].map(x => x * 2); 94test('arrow in map [0]', mapped[0], 2); 95test('arrow in map [1]', mapped[1], 4); 96test('arrow in map [2]', mapped[2], 6); 97 98const filtered = [1, 2, 3, 4, 5].filter(x => x > 2); 99test('arrow in filter length', filtered.length, 3); 100test('arrow in filter [0]', filtered[0], 3); 101 102const sum = [1, 2, 3, 4].reduce((acc, x) => acc + x, 0); 103test('arrow in reduce', sum, 10); 104 105const iife = (x => x * 2)(5); 106test('arrow iife', iife, 10); 107 108const iifeTwoLevel = ((u = 10) => u * 2)(); 109test('arrow iife with default', iifeTwoLevel, 20); 110 111const nested = a => b => c => a + b + c; 112test('arrow nested 1', nested(1)(2)(3), 6); 113 114const conditional = x => (x > 0 ? x : -x); 115test('arrow conditional positive', conditional(5), 5); 116test('arrow conditional negative', conditional(-5), 5); 117 118const logical = (a, b) => (a && b ? a + b : 0); 119test('arrow logical true', logical(3, 4), 7); 120test('arrow logical false', logical(0, 4), 0); 121 122const template = name => `Hello, ${name}!`; 123test('arrow template literal', template('World'), 'Hello, World!'); 124 125const makeMultiplier = n => x => x * n; 126const double = makeMultiplier(2); 127const triple = makeMultiplier(3); 128test('arrow returning arrow double', double(5), 10); 129test('arrow returning arrow triple', triple(5), 15); 130 131const spreadArrow = (a, b, c) => a + b + c; 132test('arrow with spread call', spreadArrow(...[1, 2, 3]), 6); 133 134summary();