2018-04-24 00:59:00 +02:00
|
|
|
set_global('blueslip', global.make_zblueslip());
|
2013-09-10 23:33:46 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('basic', () => {
|
2013-08-07 22:28:45 +02:00
|
|
|
var d = new Dict();
|
|
|
|
|
2017-04-21 18:05:50 +02:00
|
|
|
assert(d.is_empty());
|
|
|
|
|
2013-08-07 22:28:45 +02:00
|
|
|
assert.deepEqual(d.keys(), []);
|
|
|
|
|
|
|
|
d.set('foo', 'bar');
|
|
|
|
assert.equal(d.get('foo'), 'bar');
|
2017-04-21 18:05:50 +02:00
|
|
|
assert(!d.is_empty());
|
2013-08-07 22:28:45 +02:00
|
|
|
|
|
|
|
d.set('foo', 'baz');
|
|
|
|
assert.equal(d.get('foo'), 'baz');
|
|
|
|
|
|
|
|
d.set('bar', 'qux');
|
|
|
|
assert.equal(d.get('foo'), 'baz');
|
|
|
|
assert.equal(d.get('bar'), 'qux');
|
|
|
|
|
|
|
|
assert.equal(d.has('bar'), true);
|
|
|
|
assert.equal(d.has('baz'), false);
|
|
|
|
|
|
|
|
assert.deepEqual(d.keys(), ['foo', 'bar']);
|
|
|
|
assert.deepEqual(d.values(), ['baz', 'qux']);
|
|
|
|
assert.deepEqual(d.items(), [['foo', 'baz'], ['bar', 'qux']]);
|
|
|
|
|
|
|
|
d.del('bar');
|
|
|
|
assert.equal(d.has('bar'), false);
|
|
|
|
assert.strictEqual(d.get('bar'), undefined);
|
|
|
|
|
|
|
|
assert.deepEqual(d.keys(), ['foo']);
|
2013-08-20 22:34:36 +02:00
|
|
|
|
|
|
|
var val = ['foo'];
|
|
|
|
var res = d.set('abc', val);
|
|
|
|
assert.equal(val, res);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-07 22:28:45 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('fold_case', () => {
|
2013-09-10 23:14:08 +02:00
|
|
|
var d = new Dict({fold_case: true});
|
|
|
|
|
|
|
|
assert.deepEqual(d.keys(), []);
|
|
|
|
|
|
|
|
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'));
|
|
|
|
|
|
|
|
assert.deepEqual(d.keys(), ['fOO']);
|
|
|
|
|
|
|
|
d.del('Foo');
|
|
|
|
assert.equal(d.has('foo'), false);
|
|
|
|
|
|
|
|
assert.deepEqual(d.keys(), []);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-09-10 23:14:08 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('undefined_keys', () => {
|
2018-04-24 00:59:00 +02:00
|
|
|
blueslip.set_test_data('error', 'Tried to call a Dict method with an undefined key.');
|
2013-09-10 23:33:46 +02:00
|
|
|
|
2013-09-10 23:00:54 +02:00
|
|
|
var d = new Dict();
|
|
|
|
|
|
|
|
assert.equal(d.has(undefined), false);
|
|
|
|
assert.strictEqual(d.get(undefined), undefined);
|
2013-09-10 23:06:56 +02:00
|
|
|
|
|
|
|
d = new Dict({fold_case: true});
|
|
|
|
|
|
|
|
assert.equal(d.has(undefined), false);
|
|
|
|
assert.strictEqual(d.get(undefined), undefined);
|
2018-04-24 00:59:00 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 4);
|
|
|
|
|
|
|
|
blueslip.clear_test_data();
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-09-10 23:00:54 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('restricted_keys', () => {
|
2013-08-07 22:28:45 +02:00
|
|
|
var d = new Dict();
|
|
|
|
|
|
|
|
assert.equal(d.has('__proto__'), false);
|
|
|
|
assert.equal(d.has('hasOwnProperty'), false);
|
|
|
|
assert.equal(d.has('toString'), false);
|
|
|
|
|
|
|
|
assert.strictEqual(d.get('__proto__'), undefined);
|
|
|
|
assert.strictEqual(d.get('hasOwnProperty'), undefined);
|
|
|
|
assert.strictEqual(d.get('toString'), undefined);
|
|
|
|
|
|
|
|
d.set('hasOwnProperty', function () {return true;});
|
|
|
|
assert.equal(d.has('blah'), false);
|
|
|
|
|
|
|
|
d.set('__proto__', 'foo');
|
|
|
|
d.set('foo', 'bar');
|
|
|
|
assert.equal(d.get('foo'), 'bar');
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-07 22:28:45 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('construction', () => {
|
2013-08-07 22:28:45 +02:00
|
|
|
var d1 = new Dict();
|
|
|
|
|
|
|
|
assert.deepEqual(d1.items(), []);
|
|
|
|
|
2013-08-19 20:46:27 +02:00
|
|
|
var d2 = Dict.from({foo: 'bar', baz: 'qux'});
|
2013-08-07 22:28:45 +02:00
|
|
|
assert.deepEqual(d2.items(), [['foo', 'bar'], ['baz', 'qux']]);
|
|
|
|
|
2013-08-20 21:32:55 +02:00
|
|
|
var d3 = d2.clone();
|
2013-08-07 22:28:45 +02:00
|
|
|
d3.del('foo');
|
|
|
|
assert.deepEqual(d2.items(), [['foo', 'bar'], ['baz', 'qux']]);
|
|
|
|
assert.deepEqual(d3.items(), [['baz', 'qux']]);
|
2013-08-23 05:00:27 +02:00
|
|
|
|
2013-09-07 02:44:42 +02:00
|
|
|
var d4 = Dict.from_array(['foo', 'bar']);
|
|
|
|
assert.deepEqual(d4.items(), [['foo', true], ['bar', true]]);
|
|
|
|
|
2013-08-23 05:00:27 +02:00
|
|
|
var caught;
|
|
|
|
try {
|
|
|
|
Dict.from('bogus');
|
|
|
|
} catch (e) {
|
|
|
|
caught = true;
|
|
|
|
assert.equal(e.toString(), 'TypeError: Cannot convert argument to Dict');
|
|
|
|
}
|
|
|
|
assert(caught);
|
2013-09-07 02:44:42 +02:00
|
|
|
|
|
|
|
caught = undefined;
|
|
|
|
try {
|
2016-12-03 03:08:47 +01:00
|
|
|
Dict.from_array({bogus: true});
|
2013-09-07 02:44:42 +02:00
|
|
|
} catch (e2) {
|
|
|
|
caught = true;
|
|
|
|
assert.equal(e2.toString(), 'TypeError: Argument is not an array');
|
|
|
|
}
|
|
|
|
assert(caught);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-14 19:08:03 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('each', () => {
|
2013-08-14 19:08:03 +02:00
|
|
|
var d = new Dict();
|
|
|
|
d.set('apple', 40);
|
|
|
|
d.set('banana', 50);
|
|
|
|
d.set('carrot', 60);
|
|
|
|
|
|
|
|
var unseen_keys = d.keys();
|
|
|
|
|
|
|
|
var cnt = 0;
|
|
|
|
d.each(function (v, k) {
|
|
|
|
assert.equal(v, d.get(k));
|
|
|
|
unseen_keys = _.without(unseen_keys, k);
|
|
|
|
cnt += 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(cnt, d.keys().length);
|
|
|
|
assert.equal(unseen_keys.length, 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-20 22:48:07 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('setdefault', () => {
|
2013-08-20 22:48:07 +02:00
|
|
|
var d = new Dict();
|
|
|
|
var val = ['foo'];
|
|
|
|
var res = d.setdefault('foo', val);
|
|
|
|
assert.equal(res, val);
|
|
|
|
assert.equal(d.has('foo'), true);
|
|
|
|
assert.equal(d.get('foo'), val);
|
|
|
|
|
|
|
|
var val2 = ['foo2'];
|
|
|
|
res = d.setdefault('foo', val2);
|
|
|
|
assert.equal(res, val);
|
|
|
|
assert.equal(d.get('foo'), val);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2013-08-23 02:54:47 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('num_items', () => {
|
2013-08-23 02:54:47 +02:00
|
|
|
var d = new Dict();
|
|
|
|
assert.equal(d.num_items(), 0);
|
2017-04-21 18:05:50 +02:00
|
|
|
assert(d.is_empty());
|
|
|
|
|
2013-08-23 02:54:47 +02:00
|
|
|
d.set('foo', 1);
|
|
|
|
assert.equal(d.num_items(), 1);
|
2017-04-21 18:05:50 +02:00
|
|
|
assert(!d.is_empty());
|
|
|
|
|
2013-08-23 02:54:47 +02:00
|
|
|
d.set('foo', 2);
|
|
|
|
assert.equal(d.num_items(), 1);
|
2017-04-21 18:05:50 +02:00
|
|
|
assert(!d.is_empty());
|
|
|
|
|
2013-08-23 02:54:47 +02:00
|
|
|
d.set('bar', 1);
|
|
|
|
assert.equal(d.num_items(), 2);
|
|
|
|
d.del('foo');
|
|
|
|
assert.equal(d.num_items(), 1);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-04-07 00:37:32 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('clear', () => {
|
2017-04-07 00:37:32 +02:00
|
|
|
var d = new Dict();
|
|
|
|
|
|
|
|
function populate() {
|
|
|
|
d.set('foo', 1);
|
|
|
|
assert.equal(d.get('foo'), 1);
|
|
|
|
d.set('bar', 2);
|
|
|
|
assert.equal(d.get('bar'), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
populate();
|
|
|
|
assert.equal(d.num_items(), 2);
|
2017-04-21 18:05:50 +02:00
|
|
|
assert(!d.is_empty());
|
2017-04-07 00:37:32 +02:00
|
|
|
|
|
|
|
d.clear();
|
|
|
|
assert.equal(d.get('foo'), undefined);
|
|
|
|
assert.equal(d.get('bar'), undefined);
|
|
|
|
assert.equal(d.num_items(), 0);
|
2017-04-21 18:05:50 +02:00
|
|
|
assert(d.is_empty());
|
2017-04-07 00:37:32 +02:00
|
|
|
|
|
|
|
// make sure it still works after clearing
|
|
|
|
populate();
|
|
|
|
assert.equal(d.num_items(), 2);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|