From 103678b36d2a655ae479fe54d47622ad65bb7120 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 4 Dec 2012 13:19:35 -0500 Subject: [PATCH] Refine Entity and specs --- js/id/graph/entity.js | 19 ++++------ test/spec/graph/entity.js | 79 +++++++++++++++++++++++++++++++++++++-- test/spec/graph/way.js | 2 +- 3 files changed, 84 insertions(+), 16 deletions(-) diff --git a/js/id/graph/entity.js b/js/id/graph/entity.js index e484458c0..ffcfb5eea 100644 --- a/js/id/graph/entity.js +++ b/js/id/graph/entity.js @@ -1,11 +1,7 @@ -iD.Entity = function(a, b) { - if (!(this instanceof iD.Entity)) return new iD.Entity(a, b); +iD.Entity = function(a, b, c) { + if (!(this instanceof iD.Entity)) return new iD.Entity(a, b, c); - _.extend(this, {tags: {}}, a, b); - - if (b) { - this._updated = true; - } + _.extend(this, {tags: {}}, a, b, c); if (!this.id) { this.id = iD.util.id(this.type); @@ -20,8 +16,7 @@ iD.Entity = function(a, b) { iD.Entity.prototype = { update: function(attrs) { - attrs._updated = true; - return iD.Entity(this, attrs); + return iD.Entity(this, attrs, {_updated: true}); }, created: function() { @@ -34,11 +29,11 @@ iD.Entity.prototype = { }; iD.Node = function(attrs) { - return iD.Entity(_.extend({}, attrs || {}, {type: 'node', tags: {}})); + return iD.Entity({tags: {}}, attrs || {}, {type: 'node'}); }; iD.Way = function(attrs) { - return iD.Entity(_.extend({}, attrs || {}, {type: 'way', nodes: [], tags: {}})); + return iD.Entity({tags: {}, nodes: []}, attrs || {}, {type: 'way'}); }; iD.Way.isOneWay = function(d) { @@ -54,5 +49,5 @@ iD.Way.isArea = function(d) { }; iD.Relation = function(attrs) { - return iD.Entity(_.extend({}, attrs || {}, {type: 'relation'})); + return iD.Entity({tags: {}}, attrs || {}, {type: 'relation'}); }; diff --git a/test/spec/graph/entity.js b/test/spec/graph/entity.js index d8e15e1f7..baa15cc4f 100644 --- a/test/spec/graph/entity.js +++ b/test/spec/graph/entity.js @@ -18,9 +18,19 @@ describe('Entity', function () { e = iD.Entity().update({tags: tags}); expect(e._updated).to.to.be.true; }); + + it("doesn't modify the input", function () { + var attrs = {tags: {foo: 'bar'}}, + e = iD.Entity().update(attrs); + expect(attrs).to.eql({tags: {foo: 'bar'}}); + }) }); describe("#created", function () { + it("returns falsy by default", function () { + expect(iD.Entity({id: 'w1234'}).created()).not.to.be.ok; + }); + it("returns falsy for an unmodified Entity", function () { expect(iD.Entity({id: 'w1234'}).created()).not.to.be.ok; }); @@ -35,6 +45,10 @@ describe('Entity', function () { }); describe("#modified", function () { + it("returns falsy by default", function () { + expect(iD.Entity({id: 'w1234'}).modified()).not.to.be.ok; + }); + it("returns falsy for an unmodified Entity", function () { expect(iD.Entity({id: 'w1234'}).modified()).not.to.be.ok; }); @@ -50,19 +64,78 @@ describe('Entity', function () { }); describe('Node', function () { + it("returns a node", function () { + expect(iD.Node().type).to.equal("node"); + }); + it("returns a created Entity if no ID is specified", function () { - expect(iD.Node().created()).to.be.true; + expect(iD.Node().created()).to.be.ok; + }); + + it("returns an unmodified Entity if ID is specified", function () { + expect(iD.Node({id: 'n1234'}).created()).not.to.be.ok; + expect(iD.Node({id: 'n1234'}).modified()).not.to.be.ok; + }); + + it("defaults tags to an empty object", function () { + expect(iD.Node().tags).to.eql({}); + }); + + it("sets tags as specified", function () { + expect(iD.Node({tags: {foo: 'bar'}}).tags).to.eql({foo: 'bar'}); }); }); describe('Way', function () { + it("returns a way", function () { + expect(iD.Way().type).to.equal("way"); + }); + it("returns a created Entity if no ID is specified", function () { - expect(iD.Way().created()).to.be.true; + expect(iD.Way().created()).to.be.ok; + }); + + it("returns an unmodified Entity if ID is specified", function () { + expect(iD.Way({id: 'w1234'}).created()).not.to.be.ok; + expect(iD.Way({id: 'w1234'}).modified()).not.to.be.ok; + }); + + it("defaults nodes to an empty array", function () { + expect(iD.Way().nodes).to.eql([]); + }); + + it("sets nodes as specified", function () { + expect(iD.Way({nodes: ["n-1"]}).nodes).to.eql(["n-1"]); + }); + + it("defaults tags to an empty object", function () { + expect(iD.Way().tags).to.eql({}); + }); + + it("sets tags as specified", function () { + expect(iD.Way({tags: {foo: 'bar'}}).tags).to.eql({foo: 'bar'}); }); }); describe('Relation', function () { + it("returns a relation", function () { + expect(iD.Relation().type).to.equal("relation"); + }); + it("returns a created Entity if no ID is specified", function () { - expect(iD.Relation().created()).to.be.true; + expect(iD.Relation().created()).to.be.ok; + }); + + it("returns an unmodified Entity if ID is specified", function () { + expect(iD.Relation({id: 'r1234'}).created()).not.to.be.ok; + expect(iD.Relation({id: 'r1234'}).modified()).not.to.be.ok; + }); + + it("defaults tags to an empty object", function () { + expect(iD.Relation().tags).to.eql({}); + }); + + it("sets tags as specified", function () { + expect(iD.Relation({tags: {foo: 'bar'}}).tags).to.eql({foo: 'bar'}); }); }); diff --git a/test/spec/graph/way.js b/test/spec/graph/way.js index 13f9f5d49..1293c5b50 100644 --- a/test/spec/graph/way.js +++ b/test/spec/graph/way.js @@ -18,7 +18,7 @@ describe('Way', function() { expect(iD.Way.isOneWay(iD.Way({ tags: { oneway: 'no' } }))).to.eql(false); }); it('is oneway oneway=yes', function() { - expect(iD.Way.isOneWay(iD.Way({ tags: { oneway: 'yes' } }))).to.eql(false); + expect(iD.Way.isOneWay(iD.Way({ tags: { oneway: 'yes' } }))).to.eql(true); }); }); });