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 115 lines 2.7 kB view raw
1import { test, summary } from './helpers.js'; 2 3console.log('Class Computed Property Key Tests\n'); 4 5const key = 'x'; 6class BasicComputed { 7 [key] = 10; 8} 9test('basic computed field', new BasicComputed().x, 10); 10 11class ComputedExpr { 12 ['a' + 'b'] = 42; 13} 14test('computed field with concat', new ComputedExpr().ab, 42); 15 16let counter = 0; 17function makeKey() { 18 counter++; 19 return 'field' + counter; 20} 21 22class SideEffectKey { 23 [makeKey()] = 'val'; 24} 25const seInstance = new SideEffectKey(); 26test('side-effect key evaluated (field name)', seInstance.field1, 'val'); 27test('side-effect key eval count after class def', counter, 1); 28 29const before = counter; 30const se2 = new SideEffectKey(); 31test('side-effect key not re-evaluated on new instance', counter, before); 32test('second instance has same key', se2.field1, 'val'); 33 34let order = []; 35function track(name) { 36 order.push(name); 37 return name; 38} 39 40class MultiComputed { 41 [track('a')] = 1; 42 [track('b')] = 2; 43 [track('c')] = 3; 44} 45const mc = new MultiComputed(); 46test('multi computed field a', mc.a, 1); 47test('multi computed field b', mc.b, 2); 48test('multi computed field c', mc.c, 3); 49test('computed key evaluation order', JSON.stringify(order), JSON.stringify(['a', 'b', 'c'])); 50 51class ComputedMethod { 52 ['say' + 'Hi']() { 53 return 'hello'; 54 } 55} 56test('computed method', new ComputedMethod().sayHi(), 'hello'); 57 58const prefix = 'get'; 59const suffix = 'Name'; 60class ComputedVarExpr { 61 [prefix + suffix]() { 62 return 'Alice'; 63 } 64} 65test('computed method from vars', new ComputedVarExpr().getName(), 'Alice'); 66 67class StaticComputed { 68 static ['s' + 'val'] = 99; 69} 70test('static computed field', StaticComputed.sval, 99); 71 72let staticCounter = 0; 73function staticKey() { 74 staticCounter++; 75 return 'sk' + staticCounter; 76} 77 78class StaticSideEffect { 79 static [staticKey()] = 'static_val'; 80} 81test('static side-effect key', StaticSideEffect.sk1, 'static_val'); 82test('static side-effect count', staticCounter, 1); 83 84class NumericKey { 85 [2 + 3] = 'five'; 86} 87test('numeric computed key', new NumericKey()[5], 'five'); 88 89const dynKey = Symbol('dyn'); 90class SymbolKey { 91 [dynKey] = 'symbol_val'; 92} 93test('symbol computed key', new SymbolKey()[dynKey], 'symbol_val'); 94 95const flag = true; 96class TernaryKey { 97 [flag ? 'yes' : 'no'] = 100; 98} 99test('ternary computed key true', new TernaryKey().yes, 100); 100 101const flag2 = false; 102class TernaryKey2 { 103 [flag2 ? 'yes' : 'no'] = 200; 104} 105test('ternary computed key false', new TernaryKey2().no, 200); 106 107function base() { 108 return 'item'; 109} 110class CallArithKey { 111 [base() + '_' + (1 + 2)] = 'combined'; 112} 113test('call+arith computed key', new CallArithKey().item_3, 'combined'); 114 115summary();