Merge pull request #217 from tschaub/safer-object

Allow ol.Object to be used with arbitrary keys.  Add a getKeys method.
This commit is contained in:
Tim Schaub
2013-02-20 07:56:07 -08:00
4 changed files with 135 additions and 23 deletions

View File

@@ -34,6 +34,54 @@ describe('ol.Object', function() {
});
});
describe('#get()', function() {
it('does not return values that are not explicitly set', function() {
var o = new ol.Object();
expect(o.get('constructor')).toBeUndefined();
expect(o.get('hasOwnProperty')).toBeUndefined();
expect(o.get('isPrototypeOf')).toBeUndefined();
expect(o.get('propertyIsEnumerable')).toBeUndefined();
expect(o.get('toLocaleString')).toBeUndefined();
expect(o.get('toString')).toBeUndefined();
expect(o.get('valueOf')).toBeUndefined();
});
});
describe('#set()', function() {
it('can be used with arbitrary names', function() {
var o = new ol.Object();
o.set('set', 'sat');
expect(o.get('set')).toBe('sat');
o.set('get', 'got');
expect(o.get('get')).toBe('got');
o.set('toString', 'string');
expect(o.get('toString')).toBe('string');
expect(typeof o.toString).toBe('function');
});
});
describe('#getKeys()', function() {
it('returns property names set at construction', function() {
var o = new ol.Object({
prop1: 'val1',
prop2: 'val2',
toString: 'string',
get: 'foo'
});
var keys = o.getKeys();
expect(keys.length).toBe(4);
expect(keys.sort()).toEqual(['get', 'prop1', 'prop2', 'toString']);
});
});
describe('setValues', function() {
it('sets multiple values at once', function() {
@@ -43,6 +91,9 @@ describe('ol.Object', function() {
});
expect(o.get('k1')).toEqual(1);
expect(o.get('k2')).toEqual(2);
var keys = o.getKeys().sort();
expect(keys).toEqual(['k1', 'k2']);
});
});
@@ -99,6 +150,9 @@ describe('ol.Object', function() {
it('dispatches events to object', function() {
o.set('k', 1);
expect(listener1).toHaveBeenCalled();
expect(o.getKeys()).toEqual(['k']);
expect(o2.getKeys()).toEqual(['k']);
});
it('dispatches generic change events to object', function() {
@@ -114,6 +168,9 @@ describe('ol.Object', function() {
it('dispatches events to object bound to', function() {
o2.set('k', 2);
expect(listener1).toHaveBeenCalled();
expect(o.getKeys()).toEqual(['k']);
expect(o2.getKeys()).toEqual(['k']);
});
it('dispatches generic change events to object bound to', function() {
@@ -137,6 +194,9 @@ describe('ol.Object', function() {
o2.bindTo('k', o);
expect(o.get('k')).toEqual(1);
expect(o2.get('k')).toEqual(1);
expect(o.getKeys()).toEqual(['k']);
expect(o2.getKeys()).toEqual(['k']);
});
});
@@ -147,6 +207,9 @@ describe('ol.Object', function() {
o.set('k', 1);
expect(o.get('k')).toEqual(1);
expect(o2.get('k')).toEqual(1);
expect(o.getKeys()).toEqual(['k']);
expect(o2.getKeys()).toEqual(['k']);
});
});
@@ -239,6 +302,9 @@ describe('ol.Object', function() {
expect(o2.get('k1')).toBeUndefined();
expect(listener1).toHaveBeenCalled();
expect(listener2).toHaveBeenCalled();
expect(o.getKeys()).toEqual(['k1']);
expect(o2.getKeys()).toEqual(['k2']);
});
});
@@ -257,6 +323,10 @@ describe('ol.Object', function() {
expect(o.get('k1')).toEqual(1);
expect(o2.get('k2')).toEqual(1);
expect(o3.get('k3')).toEqual(1);
expect(o.getKeys()).toEqual(['k1']);
expect(o2.getKeys()).toEqual(['k2']);
expect(o3.getKeys()).toEqual(['k3']);
});
describe('backward', function() {
@@ -266,6 +336,10 @@ describe('ol.Object', function() {
expect(o.get('k1')).toEqual(1);
expect(o2.get('k2')).toEqual(1);
expect(o3.get('k3')).toEqual(1);
expect(o.getKeys()).toEqual(['k1']);
expect(o2.getKeys()).toEqual(['k2']);
expect(o3.getKeys()).toEqual(['k3']);
});
});
});
@@ -309,7 +383,7 @@ describe('ol.Object', function() {
describe('setter', function() {
beforeEach(function() {
o.setX = function(x) {
this.x = x;
this.set('x', x);
};
spyOn(o, 'setX').andCallThrough();
});
@@ -319,6 +393,8 @@ describe('ol.Object', function() {
o.set('x', 1);
expect(o.get('x')).toEqual(1);
expect(o.setX).not.toHaveBeenCalled();
expect(o.getKeys()).toEqual(['x']);
});
});
@@ -327,8 +403,11 @@ describe('ol.Object', function() {
var o2 = new ol.Object();
o2.bindTo('x', o);
o2.set('x', 1);
expect(o.get('x')).toEqual(1);
expect(o.setX).toHaveBeenCalled();
expect(o.get('x')).toEqual(1);
expect(o.getKeys()).toEqual(['x']);
expect(o2.getKeys()).toEqual(['x']);
});
});
});
@@ -354,6 +433,9 @@ describe('ol.Object', function() {
o2.bindTo('x', o);
expect(o2.get('x')).toEqual(1);
expect(o.getX).toHaveBeenCalled();
expect(o.getKeys()).toEqual([]);
expect(o2.getKeys()).toEqual(['x']);
});
});
});
@@ -368,6 +450,8 @@ describe('ol.Object', function() {
it('sets the property', function() {
var o = new ol.Object({k: 1});
expect(o.get('k')).toEqual(1);
expect(o.getKeys()).toEqual(['k']);
});
});
@@ -385,6 +469,8 @@ describe('ol.Object', function() {
o.set('K', 1);
expect(listener1).toHaveBeenCalled();
expect(listener2).not.toHaveBeenCalled();
expect(o.getKeys()).toEqual(['K']);
});
});
});