2019-12-26 15:34:17 +01:00
|
|
|
const FoldDict = zrequire('fold_dict').FoldDict;
|
|
|
|
|
|
|
|
run_test('basic', () => {
|
|
|
|
const d = new FoldDict();
|
|
|
|
|
2020-02-03 08:04:28 +01:00
|
|
|
assert.equal(d.size, 0);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
assert.deepEqual(Array.from(d.keys()), []);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
|
|
|
d.set('foo', 'bar');
|
|
|
|
assert.equal(d.get('foo'), 'bar');
|
2020-02-03 08:04:28 +01:00
|
|
|
assert.notEqual(d.size, 0);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
|
|
|
d.set('foo', 'baz');
|
|
|
|
assert.equal(d.get('foo'), 'baz');
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 1);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
|
|
|
d.set('bar', 'qux');
|
|
|
|
assert.equal(d.get('foo'), 'baz');
|
|
|
|
assert.equal(d.get('bar'), 'qux');
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 2);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
|
|
|
assert.equal(d.has('bar'), true);
|
|
|
|
assert.equal(d.has('baz'), false);
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
assert.deepEqual(Array.from(d.keys()), ['foo', 'bar']);
|
|
|
|
assert.deepEqual(Array.from(d.values()), ['baz', 'qux']);
|
|
|
|
assert.deepEqual(Array.from(d), [['foo', 'baz'], ['bar', 'qux']]);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2020-02-03 07:41:38 +01:00
|
|
|
d.delete('bar');
|
2019-12-26 15:34:17 +01:00
|
|
|
assert.equal(d.has('bar'), false);
|
|
|
|
assert.strictEqual(d.get('bar'), undefined);
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
assert.deepEqual(Array.from(d.keys()), ['foo']);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
|
|
|
const val = ['foo'];
|
|
|
|
const res = d.set('abc', val);
|
2020-02-03 07:18:13 +01:00
|
|
|
assert.strictEqual(res, d);
|
2019-12-26 15:34:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
run_test('case insensitivity', () => {
|
|
|
|
const d = new FoldDict();
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
assert.deepEqual(Array.from(d.keys()), []);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
|
|
|
assert(!d.has('foo'));
|
|
|
|
d.set('fOO', 'Hello World');
|
|
|
|
assert.equal(d.get('foo'), 'Hello World');
|
|
|
|
assert(d.has('foo'));
|
|
|
|
assert(d.has('FOO'));
|
|
|
|
assert(!d.has('not_a_key'));
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
assert.deepEqual(Array.from(d.keys()), ['fOO']);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
2020-02-03 07:41:38 +01:00
|
|
|
d.delete('Foo');
|
2019-12-26 15:34:17 +01:00
|
|
|
assert.equal(d.has('foo'), false);
|
|
|
|
|
2020-02-04 23:46:56 +01:00
|
|
|
assert.deepEqual(Array.from(d.keys()), []);
|
2019-12-26 15:34:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
run_test('clear', () => {
|
|
|
|
const d = new FoldDict();
|
|
|
|
|
|
|
|
function populate() {
|
|
|
|
d.set('fOO', 1);
|
|
|
|
assert.equal(d.get('foo'), 1);
|
|
|
|
d.set('bAR', 2);
|
|
|
|
assert.equal(d.get('bar'), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
populate();
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 2);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
|
|
|
d.clear();
|
|
|
|
assert.equal(d.get('fOO'), undefined);
|
|
|
|
assert.equal(d.get('bAR'), undefined);
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 0);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
|
|
|
// make sure it still works after clearing
|
|
|
|
populate();
|
2020-02-03 07:58:50 +01:00
|
|
|
assert.equal(d.size, 2);
|
2019-12-26 15:34:17 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
run_test('undefined_keys', () => {
|
2020-04-20 12:38:24 +02:00
|
|
|
blueslip.expect('error', 'Tried to call a FoldDict method with an undefined key.', 2);
|
2019-12-26 15:34:17 +01:00
|
|
|
|
|
|
|
const d = new FoldDict();
|
|
|
|
|
|
|
|
assert.equal(d.has(undefined), false);
|
|
|
|
assert.strictEqual(d.get(undefined), undefined);
|
|
|
|
});
|
|
|
|
|