Refine Entity and specs

This commit is contained in:
John Firebaugh
2012-12-04 13:19:35 -05:00
parent 40f6f55e72
commit 103678b36d
3 changed files with 84 additions and 16 deletions
+7 -12
View File
@@ -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'});
};
+76 -3
View File
@@ -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'});
});
});
+1 -1
View File
@@ -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);
});
});
});