Introduce real Entity subclasses

This commit is contained in:
John Firebaugh
2012-12-28 18:42:21 -08:00
parent 167556ab02
commit 5fe22be7a0
9 changed files with 237 additions and 197 deletions
+3 -1
View File
@@ -111,8 +111,10 @@
<script src="spec/graph/graph.js"></script>
<script src="spec/graph/entity.js"></script>
<script src="spec/graph/history.js"></script>
<script src="spec/graph/node.js"></script>
<script src="spec/graph/way.js"></script>
<script src="spec/graph/relation.js"></script>
<script src="spec/graph/history.js"></script>
<script src="spec/modes/add_point.js"></script>
+3 -1
View File
@@ -43,8 +43,10 @@
<script src="spec/graph/graph.js"></script>
<script src="spec/graph/entity.js"></script>
<script src="spec/graph/history.js"></script>
<script src="spec/graph/node.js"></script>
<script src="spec/graph/way.js"></script>
<script src="spec/graph/relation.js"></script>
<script src="spec/graph/history.js"></script>
<script src="spec/modes/add_point.js"></script>
+11 -123
View File
@@ -1,4 +1,10 @@
describe('iD.Entity', function () {
it("returns a subclass of the appropriate type", function () {
expect(iD.Entity({type: 'way'})).be.an.instanceOf(iD.Way);
expect(iD.Entity({type: 'node'})).be.an.instanceOf(iD.Node);
expect(iD.Entity({type: 'relation'})).be.an.instanceOf(iD.Relation);
});
if (iD.debug) {
it("is frozen", function () {
expect(Object.isFrozen(iD.Entity())).to.be.true;
@@ -41,6 +47,11 @@ describe('iD.Entity', function () {
expect(e.tags).to.equal(tags);
});
it("preserves existing attributes", function () {
var e = iD.Entity({id: 'w1'}).update({});
expect(e.id).to.equal('w1');
});
it("tags the entity as updated", function () {
var tags = {foo: 'bar'},
e = iD.Entity().update({tags: tags});
@@ -112,126 +123,3 @@ describe('iD.Entity', function () {
});
});
});
describe('iD.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.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("#intersects", function () {
it("returns true for a node within the given extent", function () {
expect(iD.Node({loc: [0, 0]}).intersects([[-180, 90], [180, -90]])).to.equal(true);
});
it("returns false for a node outside the given extend", function () {
expect(iD.Node({loc: [0, 0]}).intersects([[100, 90], [180, -90]])).to.equal(false);
});
});
});
describe('iD.Way', function () {
if (iD.debug) {
it("freezes nodes", function () {
expect(Object.isFrozen(iD.Way().nodes)).to.be.true;
});
}
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.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("#intersects", function () {
it("returns true for a way with a node within the given extent", function () {
var node = iD.Node({loc: [0, 0]}),
way = iD.Way({nodes: [node.id]}),
graph = iD.Graph([node, way]);
expect(way.intersects([[-180, 90], [180, -90]], graph)).to.equal(true);
});
it("returns false for way with no nodes within the given extent", function () {
var node = iD.Node({loc: [0, 0]}),
way = iD.Way({nodes: [node.id]}),
graph = iD.Graph([node, way]);
expect(way.intersects([[100, 90], [180, -90]], graph)).to.equal(false);
});
});
});
describe('iD.Relation', function () {
if (iD.debug) {
it("freezes nodes", function () {
expect(Object.isFrozen(iD.Relation().members)).to.be.true;
});
}
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.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 members to an empty array", function () {
expect(iD.Relation().members).to.eql([]);
});
it("sets members as specified", function () {
expect(iD.Relation({members: ["n-1"]}).members).to.eql(["n-1"]);
});
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'});
});
});
+33
View File
@@ -0,0 +1,33 @@
describe('iD.Node', function () {
it("returns a node", function () {
expect(iD.Node()).to.be.an.instanceOf(iD.Node);
expect(iD.Node().type).to.equal("node");
});
it("returns a created Entity if no ID is specified", function () {
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("#intersects", function () {
it("returns true for a node within the given extent", function () {
expect(iD.Node({loc: [0, 0]}).intersects([[-180, 90], [180, -90]])).to.equal(true);
});
it("returns false for a node outside the given extend", function () {
expect(iD.Node({loc: [0, 0]}).intersects([[100, 90], [180, -90]])).to.equal(false);
});
});
});
+37
View File
@@ -0,0 +1,37 @@
describe('iD.Relation', function () {
if (iD.debug) {
it("freezes nodes", function () {
expect(Object.isFrozen(iD.Relation().members)).to.be.true;
});
}
it("returns a relation", function () {
expect(iD.Relation()).to.be.an.instanceOf(iD.Relation);
expect(iD.Relation().type).to.equal("relation");
});
it("returns a created Entity if no ID is specified", function () {
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 members to an empty array", function () {
expect(iD.Relation().members).to.eql([]);
});
it("sets members as specified", function () {
expect(iD.Relation({members: ["n-1"]}).members).to.eql(["n-1"]);
});
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'});
});
});
+67 -14
View File
@@ -1,24 +1,77 @@
describe('Way', function() {
describe('#isClosed', function() {
it('is not closed with two distinct nodes', function() {
var open_way = { type: 'way', nodes: [{id: 'n1'}, {id: 'n2'}] };
expect(iD.Way.isClosed(open_way)).to.equal(false);
describe('iD.Way', function() {
if (iD.debug) {
it("freezes nodes", function () {
expect(Object.isFrozen(iD.Way().nodes)).to.be.true;
});
it('is not closed with a node loop', function() {
var closed_way = { type: 'way', nodes: [{id: 'n1'}, {id: 'n2'}, {id: 'n1'}] };
expect(iD.Way.isClosed(closed_way)).to.equal(true);
}
it("returns a way", function () {
expect(iD.Way()).to.be.an.instanceOf(iD.Way);
expect(iD.Way().type).to.equal("way");
});
it("returns a created Entity if no ID is specified", function () {
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("#intersects", function () {
it("returns true for a way with a node within the given extent", function () {
var node = iD.Node({loc: [0, 0]}),
way = iD.Way({nodes: [node.id]}),
graph = iD.Graph([node, way]);
expect(way.intersects([[-180, 90], [180, -90]], graph)).to.equal(true);
});
it("returns false for way with no nodes within the given extent", function () {
var node = iD.Node({loc: [0, 0]}),
way = iD.Way({nodes: [node.id]}),
graph = iD.Graph([node, way]);
expect(way.intersects([[100, 90], [180, -90]], graph)).to.equal(false);
});
});
describe('#isClosed', function() {
it('returns false when the way ends are not equal', function() {
expect(iD.Way({nodes: ['n1', 'n2']}).isClosed()).to.equal(false);
});
it('returns true when the way ends are equal', function() {
expect(iD.Way({nodes: ['n1', 'n2', 'n1']}).isClosed()).to.equal(true);
});
});
describe('#isOneWay', function() {
it('is not oneway without any tags', function() {
expect(iD.Way.isOneWay(iD.Way())).to.eql(false);
it('returns false when the way has no tags', function() {
expect(iD.Way().isOneWay()).to.eql(false);
});
it('is not oneway oneway=no', function() {
expect(iD.Way.isOneWay(iD.Way({ tags: { oneway: 'no' } }))).to.eql(false);
it('returns false when the way has tag oneway=no', function() {
expect(iD.Way({tags: { oneway: 'no' }}).isOneWay()).to.equal(false);
});
it('is oneway oneway=yes', function() {
expect(iD.Way.isOneWay(iD.Way({ tags: { oneway: 'yes' } }))).to.eql(true);
it('returns true when the way has tag oneway=yes', function() {
expect(iD.Way({tags: { oneway: 'yes' }}).isOneWay()).to.equal(true);
});
});
});