2013-08-21 20:27:14 +02:00
|
|
|
add_dependencies({
|
|
|
|
_: 'third/underscore/underscore.js'
|
|
|
|
});
|
|
|
|
|
|
|
|
var _ = global._;
|
2013-08-07 22:28:45 +02:00
|
|
|
var Dict = require('js/dict.js');
|
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
(function test_basic() {
|
|
|
|
var d = new Dict();
|
|
|
|
|
|
|
|
assert.deepEqual(d.keys(), []);
|
|
|
|
|
|
|
|
d.set('foo', 'bar');
|
|
|
|
assert.equal(d.get('foo'), 'bar');
|
|
|
|
|
|
|
|
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);
|
2013-08-07 22:28:45 +02:00
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_restricted_keys() {
|
|
|
|
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');
|
|
|
|
}());
|
|
|
|
|
|
|
|
(function test_construction() {
|
|
|
|
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-14 19:08:03 +02:00
|
|
|
|
|
|
|
(function test_each() {
|
|
|
|
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);
|
|
|
|
}());
|
2013-08-20 22:48:07 +02:00
|
|
|
|
|
|
|
(function test_setdefault() {
|
|
|
|
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);
|
2013-08-21 20:27:14 +02:00
|
|
|
}());
|
2013-08-23 02:54:47 +02:00
|
|
|
|
|
|
|
(function test_num_items() {
|
|
|
|
var d = new Dict();
|
|
|
|
assert.equal(d.num_items(), 0);
|
|
|
|
d.set('foo', 1);
|
|
|
|
assert.equal(d.num_items(), 1);
|
|
|
|
d.set('foo', 2);
|
|
|
|
assert.equal(d.num_items(), 1);
|
|
|
|
d.set('bar', 1);
|
|
|
|
assert.equal(d.num_items(), 2);
|
|
|
|
d.del('foo');
|
|
|
|
assert.equal(d.num_items(), 1);
|
|
|
|
}());
|
|
|
|
|
|
|
|
|