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.

add match spec

+90
+90
examples/spec/match.js
··· 1 + import { test, summary } from './helpers.js'; 2 + 3 + console.log('Ant.match Tests\n'); 4 + 5 + test('match is function', typeof Ant.match, 'function'); 6 + 7 + test('exact match number', Ant.match(200, { 8 + 200: 'ok', 9 + 404: 'not found', 10 + }), 'ok'); 11 + 12 + test('exact match string', Ant.match('hello', { 13 + hello: 'world', 14 + foo: 'bar', 15 + }), 'world'); 16 + 17 + test('no match returns undefined', Ant.match(999, { 18 + 200: 'ok', 19 + 404: 'not found', 20 + }), undefined); 21 + 22 + test('Symbol.default fallback', Ant.match(999, { 23 + 200: 'ok', 24 + [Symbol.default]: 'fallback', 25 + }), 'fallback'); 26 + 27 + test('Symbol.default as function', Ant.match(999, { 28 + 200: 'ok', 29 + [Symbol.default]: v => `unknown ${v}`, 30 + }), 'unknown 999'); 31 + 32 + test('function arm called with value', Ant.match(200, { 33 + 200: v => `got ${v}`, 34 + }), 'got 200'); 35 + 36 + test('guard arm true', Ant.match(503, { 37 + 200: 'ok', 38 + [503 >= 500]: 'server error', 39 + }), 'server error'); 40 + 41 + test('guard arm false skipped', Ant.match(503, { 42 + [503 < 400]: 'client error', 43 + [Symbol.default]: 'fallback', 44 + }), 'fallback'); 45 + 46 + test('exact match takes priority over guard', Ant.match(200, { 47 + [true]: 'guard', 48 + 200: 'exact', 49 + }), 'exact'); 50 + 51 + test('arrow function form with $', Ant.match(503, $ => ({ 52 + 200: 'ok', 53 + 404: 'not found', 54 + [$ >= 500]: 'server error', 55 + })), 'server error'); 56 + 57 + test('arrow $ guard false', Ant.match(200, $ => ({ 58 + [$ >= 500]: 'server error', 59 + [Symbol.default]: 'other', 60 + })), 'other'); 61 + 62 + test('compiler sugar with $', Ant.match(404, { 63 + 200: 'ok', 64 + [$ === 404]: 'not found', 65 + [Symbol.default]: 'unknown', 66 + }), 'not found'); 67 + 68 + test('compiler sugar $ receives value', Ant.match(42, { 69 + [$ > 100]: 'big', 70 + [$ > 0]: 'positive', 71 + [Symbol.default]: 'other', 72 + }), 'positive'); 73 + 74 + test('plain values not called', Ant.match(1, { 75 + 1: 'one', 76 + 2: 'two', 77 + }), 'one'); 78 + 79 + test('mixed plain and function arms', Ant.match(2, { 80 + 1: 'one', 81 + 2: v => `two-${v}`, 82 + }), 'two-2'); 83 + 84 + test('empty arms returns undefined', Ant.match(1, {}), undefined); 85 + 86 + test('Symbol.default function receives value', Ant.match(42, { 87 + [Symbol.default]: v => v * 2, 88 + }), 84); 89 + 90 + summary();