| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
const funky = function () {}; |
| 5 |
const arry = []; |
| 6 |
|
| 7 |
QUnit.module( 'mw.Map' ); |
| 8 |
|
| 9 |
QUnit.test( 'Store simple string key', ( assert ) => { |
| 10 |
const conf = new mw.Map(); |
| 11 |
|
| 12 |
assert.true( conf.set( 'foo', 'Bar' ), 'set' ); |
| 13 |
assert.strictEqual( conf.get( 'foo' ), 'Bar', 'get' ); |
| 14 |
} ); |
| 15 |
|
| 16 |
QUnit.test( 'Store number-like key', ( assert ) => { |
| 17 |
const conf = new mw.Map(); |
| 18 |
|
| 19 |
assert.true( conf.set( '42', 'X' ), 'set' ); |
| 20 |
assert.strictEqual( conf.get( '42' ), 'X', 'get' ); |
| 21 |
} ); |
| 22 |
|
| 23 |
QUnit.test( 'get()', ( assert ) => { |
| 24 |
let conf = new mw.Map(); |
| 25 |
|
| 26 |
assert.strictEqual( conf.get( 'example' ), null, 'default fallback' ); |
| 27 |
assert.strictEqual( conf.get( 'example', arry ), arry, 'array fallback' ); |
| 28 |
assert.strictEqual( conf.get( 'example', funky ), funky, 'function fallback' ); |
| 29 |
assert.strictEqual( conf.get( 'example', undefined ), undefined, 'undefined fallback' ); |
| 30 |
|
| 31 |
|
| 32 |
assert.true( conf.set( '7', 'I used to be a number' ) ); |
| 33 |
assert.strictEqual( conf.get( 7 ), null, 'ignore number key (single)' ); |
| 34 |
assert.deepEqual( conf.get( [ 7 ] ), {}, 'ignore number key (multiple)' ); |
| 35 |
assert.strictEqual( conf.get( 7, 42 ), 42, 'ignore number key (fallback)' ); |
| 36 |
|
| 37 |
|
| 38 |
assert.true( conf.set( String( funky ), 'I used to be a function' ) ); |
| 39 |
assert.strictEqual( conf.get( funky ), null, 'ignore function key' ); |
| 40 |
|
| 41 |
conf = new mw.Map(); |
| 42 |
conf.set( 'foo', 'bar' ); |
| 43 |
conf.set( 'x', [ 'y', 'z' ] ); |
| 44 |
conf.set( 'num', 7 ); |
| 45 |
conf.set( 'num2', 42 ); |
| 46 |
assert.deepEqual( |
| 47 |
conf.get( [ 'foo', 'x', 'num' ] ), |
| 48 |
{ foo: 'bar', x: [ 'y', 'z' ], num: 7 }, |
| 49 |
'get multiple' |
| 50 |
); |
| 51 |
assert.deepEqual( |
| 52 |
conf.get( [ 'foo', 'bar', 'x', 'y' ] ), |
| 53 |
{ foo: 'bar', bar: null, x: [ 'y', 'z' ], y: null }, |
| 54 |
'get multiple with some unknown' |
| 55 |
); |
| 56 |
|
| 57 |
assert.propEqual( |
| 58 |
conf.get(), |
| 59 |
{ foo: 'bar', x: [ 'y', 'z' ], num: 7, num2: 42 }, |
| 60 |
'get all values' |
| 61 |
); |
| 62 |
} ); |
| 63 |
|
| 64 |
|
| 65 |
QUnit.test( 'values', ( assert ) => { |
| 66 |
const conf = new mw.Map(); |
| 67 |
conf.set( { num: 7, num2: 42 } ); |
| 68 |
conf.set( 'foo', 'bar' ); |
| 69 |
|
| 70 |
assert.propEqual( conf.values, { num: 7, num2: 42, foo: 'bar' } ); |
| 71 |
} ); |
| 72 |
|
| 73 |
QUnit.test( 'set()', ( assert ) => { |
| 74 |
const conf = new mw.Map(); |
| 75 |
|
| 76 |
|
| 77 |
assert.false( conf.set( 'no-value' ), 'reject without value argument' ); |
| 78 |
|
| 79 |
assert.false( conf.set( funky, 'Funky' ), 'reject Function key' ); |
| 80 |
assert.false( conf.set( arry, 'Arry' ), 'reject Array key' ); |
| 81 |
assert.false( conf.set( 7, 'Nummy' ), 'reject number key' ); |
| 82 |
assert.false( conf.set( null, 'Null' ), 'reject null key' ); |
| 83 |
assert.false( conf.set( {}, 'Object' ), 'reject plain object as key' ); |
| 84 |
|
| 85 |
|
| 86 |
assert.true( conf.set( 'example', undefined ), 'Store the undefined value' ); |
| 87 |
assert.strictEqual( conf.get( 'example' ), undefined, 'Get the undefined value' ); |
| 88 |
assert.strictEqual( conf.get( 'example', 42 ), undefined, 'Get the undefined value (ignore default)' ); |
| 89 |
|
| 90 |
assert.true( conf.set( { key1: { x: 'x' }, key2: [ 'y' ] } ), 'set multiple' ); |
| 91 |
assert.deepEqual( conf.get( 'key1' ), { x: 'x' } ); |
| 92 |
assert.deepEqual( conf.get( 'key2' ), [ 'y' ] ); |
| 93 |
} ); |
| 94 |
|
| 95 |
QUnit.test( 'exists()', ( assert ) => { |
| 96 |
const conf = new mw.Map(); |
| 97 |
|
| 98 |
assert.false( conf.exists( 'doesNotExist' ), 'unknown' ); |
| 99 |
|
| 100 |
assert.true( conf.set( 'undef', undefined ) ); |
| 101 |
assert.true( conf.exists( 'undef' ), 'known with undefined value' ); |
| 102 |
|
| 103 |
assert.true( conf.set( '7', 42 ) ); |
| 104 |
assert.false( conf.exists( 7 ), 'unknown, with known number-like key' ); |
| 105 |
} ); |
| 106 |
|
| 107 |
|
| 108 |
QUnit.test( 'Avoid prototype pollution', ( assert ) => { |
| 109 |
const conf = new mw.Map(); |
| 110 |
|
| 111 |
assert.strictEqual( conf.get( 'constructor' ), null, 'Get unknown "constructor"' ); |
| 112 |
assert.strictEqual( conf.get( 'hasOwnProperty' ), null, 'Get unkonwn "hasOwnProperty"' ); |
| 113 |
|
| 114 |
conf.set( |
| 115 |
'hasOwnProperty', |
| 116 |
() => true |
| 117 |
); |
| 118 |
assert.strictEqual( conf.get( 'example', 'missing' ), 'missing', 'Use original hasOwnProperty method (positive)' ); |
| 119 |
|
| 120 |
conf.set( 'example', 'Foo' ); |
| 121 |
conf.set( |
| 122 |
'hasOwnProperty', |
| 123 |
() => false |
| 124 |
); |
| 125 |
assert.strictEqual( conf.get( 'example' ), 'Foo', 'Use original hasOwnProperty method (negative)' ); |
| 126 |
|
| 127 |
assert.strictEqual( conf.set( 'constructor', 42 ), true, 'Set "constructor"' ); |
| 128 |
assert.strictEqual( conf.get( 'constructor' ), 42, 'Get "constructor"' ); |
| 129 |
} ); |
| 130 |
}() ); |