Skip to content

Commit 3ee5608

Browse files
committed
Added unit tests for JSON, and fixed parsing issue for XML tests
Note that the original XML based unit tests relied on a strange parsing issue. Trailing slashes in <node /> tags were silently removed when assingning the XML file contents to innerHTML. As a result, all nodes were treated as a huge nested structure, similar to <node><node><node> ... </node></node></node>. getTags was then assigning all way tags to each single node, and the feature count was completely off as a result :( In order to avoid this situation, trailing slashes in fixure files have been replaced by <node></node> to preserve the original structure. Now the feature count in osm_test.js matches the expected number. This issue doesn't happen in a browser environment.
1 parent db76620 commit 3ee5608

File tree

1 file changed

+142
-132
lines changed

1 file changed

+142
-132
lines changed

test/osm_test.js

Lines changed: 142 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -67,174 +67,184 @@ describe("L.OSM.TracestrackTopo", function () {
6767
});
6868
});
6969

70-
describe("L.OSM.DataLayer", function () {
71-
function fixture(name) {
72-
var fs = require("fs"),
73-
data = document.createElement("div");
74-
data.innerHTML = fs.readFileSync(__dirname + "/fixtures/" + name + ".xml");
75-
return data;
76-
}
77-
78-
function layers(layerGroup) {
79-
var layers = [];
80-
layerGroup.eachLayer(function (layer) {
81-
layers.push(layer);
82-
});
83-
return layers;
84-
}
85-
86-
beforeEach(function () {
87-
this.map = L.map(document.createElement("div"));
88-
});
89-
90-
it("is can be added to the map", function () {
91-
(new L.OSM.DataLayer()).addTo(this.map);
92-
});
93-
94-
it("creates a Polyline for a way", function () {
95-
var osm = new L.OSM.DataLayer(fixture("way"));
96-
layers(osm).length.should.eq(21);
97-
layers(osm)[20].should.be.an.instanceof(L.Polyline);
98-
});
99-
100-
it("creates a Polygon for an area", function () {
101-
var osm = new L.OSM.DataLayer(fixture("area"));
102-
layers(osm).length.should.eq(15);
103-
layers(osm)[14].should.be.an.instanceof(L.Polygon);
104-
});
105-
106-
it("creates a CircleMarker for an interesting node", function () {
107-
var osm = new L.OSM.DataLayer(fixture("node"));
108-
layers(osm).length.should.eq(1);
109-
layers(osm)[0].should.be.an.instanceof(L.CircleMarker);
110-
});
111-
112-
it("creates a Rectangle for a changeset", function () {
113-
var osm = new L.OSM.DataLayer(fixture("changeset"));
114-
layers(osm).length.should.eq(1);
115-
layers(osm)[0].should.be.an.instanceof(L.Rectangle);
116-
});
117-
118-
it("sets the feature property on a layer", function () {
119-
var osm = new L.OSM.DataLayer(fixture("node"));
120-
layers(osm)[0].feature.should.have.property("type", "node");
121-
layers(osm)[0].feature.should.have.property("id", "356552551");
122-
});
123-
124-
it("sets a way's style", function () {
125-
var osm = new L.OSM.DataLayer(fixture("way"), {styles: {way: {color: "red"}}});
126-
layers(osm)[20].options.should.have.property("color", "red");
127-
});
128-
129-
it("sets an area's style", function () {
130-
var osm = new L.OSM.DataLayer(fixture("area"), {styles: {area: {color: "green"}}});
131-
layers(osm)[14].options.should.have.property("color", "green");
132-
});
133-
134-
it("sets a node's style", function () {
135-
var osm = new L.OSM.DataLayer(fixture("node"), {styles: {node: {color: "blue"}}});
136-
layers(osm)[0].options.should.have.property("color", "blue");
137-
});
70+
[ "xml", "json"].forEach(format => {
71+
72+
describe(`L.OSM.DataLayer (${format})`, function () {
73+
function fixture(name) {
74+
var fs = require("fs");
75+
var contents = fs.readFileSync(__dirname + "/fixtures/" + name + "." + format, "utf8");
76+
if (format === "xml") {
77+
var data = document.createElement("div");
78+
data.innerHTML = contents;
79+
return data;
80+
}
81+
else
82+
{
83+
return JSON.parse(contents);
84+
}
85+
}
13886

139-
describe("asynchronously", function() {
140-
function sleep(time = 0) {
141-
return new Promise(res => {
142-
setTimeout(() => res(), time);
87+
function layers(layerGroup) {
88+
var layers = [];
89+
layerGroup.eachLayer(function (layer) {
90+
layers.push(layer);
14391
});
92+
return layers;
14493
}
14594

146-
it("can be added to the map", function () {
147-
(new L.OSM.DataLayer(null, {asynchronous: true})).addTo(this.map);
95+
beforeEach(function () {
96+
this.map = L.map(document.createElement("div"));
14897
});
14998

150-
it("creates a Polyline for a way", async function () {
151-
var osm = new L.OSM.DataLayer(fixture("way"), {asynchronous: true});
152-
await sleep(1);
153-
layers(osm).length.should.eq(21);
154-
layers(osm)[20].should.be.an.instanceof(L.Polyline);
99+
it("is can be added to the map", function () {
100+
(new L.OSM.DataLayer()).addTo(this.map);
155101
});
156102

157-
it("creates a Polygon for an area", async function () {
158-
var osm = new L.OSM.DataLayer(fixture("area"), {asynchronous: true});
159-
await sleep(1);
160-
layers(osm).length.should.eq(15);
161-
layers(osm)[14].should.be.an.instanceof(L.Polygon);
103+
it("creates a Polyline for a way", function () {
104+
var osm = new L.OSM.DataLayer(fixture("way"));
105+
layers(osm).length.should.eq(1);
106+
layers(osm)[0].should.be.an.instanceof(L.Polyline);
107+
});
108+
109+
it("creates a Polygon for an area", function () {
110+
var osm = new L.OSM.DataLayer(fixture("area"));
111+
layers(osm).length.should.eq(1);
112+
layers(osm)[0].should.be.an.instanceof(L.Polygon);
162113
});
163114

164-
it("creates a CircleMarker for an interesting node", async function () {
165-
var osm = new L.OSM.DataLayer(fixture("node"), {asynchronous: true});
166-
await sleep(1);
115+
it("creates a CircleMarker for an interesting node", function () {
116+
var osm = new L.OSM.DataLayer(fixture("node"));
167117
layers(osm).length.should.eq(1);
168118
layers(osm)[0].should.be.an.instanceof(L.CircleMarker);
169119
});
170120

171-
it("creates a Rectangle for a changeset", async function () {
172-
var osm = new L.OSM.DataLayer(fixture("changeset"), {asynchronous: true});
173-
await sleep(1);
121+
it("creates a Rectangle for a changeset", function () {
122+
var osm = new L.OSM.DataLayer(fixture("changeset"));
174123
layers(osm).length.should.eq(1);
175124
layers(osm)[0].should.be.an.instanceof(L.Rectangle);
176125
});
177126

178-
it("sets the feature property on a layer", async function () {
179-
var osm = new L.OSM.DataLayer(fixture("node"), {asynchronous: true});
180-
await sleep(1);
127+
it("sets the feature property on a layer", function () {
128+
var osm = new L.OSM.DataLayer(fixture("node"));
181129
layers(osm)[0].feature.should.have.property("type", "node");
182130
layers(osm)[0].feature.should.have.property("id", "356552551");
183131
});
184132

185-
it("sets a way's style", async function () {
186-
var osm = new L.OSM.DataLayer(fixture("way"), {styles: {way: {color: "red"}}, asynchronous: true});
187-
await sleep(1);
188-
layers(osm)[20].options.should.have.property("color", "red");
133+
it("sets a way's style", function () {
134+
var osm = new L.OSM.DataLayer(fixture("way"), {styles: {way: {color: "red"}}});
135+
layers(osm)[0].options.should.have.property("color", "red");
189136
});
190137

191-
it("sets an area's style", async function () {
192-
var osm = new L.OSM.DataLayer(fixture("area"), {styles: {area: {color: "green"}}, asynchronous: true});
193-
await sleep(1);
194-
layers(osm)[14].options.should.have.property("color", "green");
138+
it("sets an area's style", function () {
139+
var osm = new L.OSM.DataLayer(fixture("area"), {styles: {area: {color: "green"}}});
140+
layers(osm)[0].options.should.have.property("color", "green");
195141
});
196142

197-
it("sets a node's style", async function () {
198-
var osm = new L.OSM.DataLayer(fixture("node"), {styles: {node: {color: "blue"}}, asynchronous: true});
199-
await sleep(1);
143+
it("sets a node's style", function () {
144+
var osm = new L.OSM.DataLayer(fixture("node"), {styles: {node: {color: "blue"}}});
200145
layers(osm)[0].options.should.have.property("color", "blue");
201146
});
202-
});
203147

204-
describe("#buildFeatures", function () {
205-
it("builds a node object", function () {
206-
var features = new L.OSM.DataLayer().buildFeatures(fixture("node"));
207-
features.length.should.eq(1);
208-
features[0].type.should.eq("node");
209-
});
148+
describe("asynchronously", function() {
149+
function sleep(time = 0) {
150+
return new Promise(res => {
151+
setTimeout(() => res(), time);
152+
});
153+
}
210154

211-
it("builds a way object", function () {
212-
var features = new L.OSM.DataLayer().buildFeatures(fixture("way"));
213-
features.length.should.eq(21);
214-
features[20].type.should.eq("way");
215-
});
216-
});
155+
it("can be added to the map", function () {
156+
(new L.OSM.DataLayer(null, {asynchronous: true})).addTo(this.map);
157+
});
217158

218-
describe("#interestingNode", function () {
219-
var layer = new L.OSM.DataLayer();
159+
it("creates a Polyline for a way", async function () {
160+
var osm = new L.OSM.DataLayer(fixture("way"), {asynchronous: true});
161+
await sleep(1);
162+
layers(osm).length.should.eq(1);
163+
layers(osm)[0].should.be.an.instanceof(L.Polyline);
164+
});
220165

221-
it("returns true when the node is not in any ways", function () {
222-
layer.interestingNode({id: 1}, {}, {}).should.be.true;
223-
});
166+
it("creates a Polygon for an area", async function () {
167+
var osm = new L.OSM.DataLayer(fixture("area"), {asynchronous: true});
168+
await sleep(1);
169+
layers(osm).length.should.eq(1);
170+
layers(osm)[0].should.be.an.instanceof(L.Polygon);
171+
});
172+
173+
it("creates a CircleMarker for an interesting node", async function () {
174+
var osm = new L.OSM.DataLayer(fixture("node"), {asynchronous: true});
175+
await sleep(1);
176+
layers(osm).length.should.eq(1);
177+
layers(osm)[0].should.be.an.instanceof(L.CircleMarker);
178+
});
179+
180+
it("creates a Rectangle for a changeset", async function () {
181+
var osm = new L.OSM.DataLayer(fixture("changeset"), {asynchronous: true});
182+
await sleep(1);
183+
layers(osm).length.should.eq(1);
184+
layers(osm)[0].should.be.an.instanceof(L.Rectangle);
185+
});
186+
187+
it("sets the feature property on a layer", async function () {
188+
var osm = new L.OSM.DataLayer(fixture("node"), {asynchronous: true});
189+
await sleep(1);
190+
layers(osm)[0].feature.should.have.property("type", "node");
191+
layers(osm)[0].feature.should.have.property("id", "356552551");
192+
});
193+
194+
it("sets a way's style", async function () {
195+
var osm = new L.OSM.DataLayer(fixture("way"), {styles: {way: {color: "red"}}, asynchronous: true});
196+
await sleep(1);
197+
layers(osm)[0].options.should.have.property("color", "red");
198+
});
199+
200+
it("sets an area's style", async function () {
201+
var osm = new L.OSM.DataLayer(fixture("area"), {styles: {area: {color: "green"}}, asynchronous: true});
202+
await sleep(1);
203+
layers(osm)[0].options.should.have.property("color", "green");
204+
});
224205

225-
it("returns true when the node has an interesting tag", function () {
226-
var node = {id: 1, tags: {interesting: true}};
227-
layer.interestingNode(node, {1: true}, {1: true}).should.be.true;
206+
it("sets a node's style", async function () {
207+
var osm = new L.OSM.DataLayer(fixture("node"), {styles: {node: {color: "blue"}}, asynchronous: true});
208+
await sleep(1);
209+
layers(osm)[0].options.should.have.property("color", "blue");
210+
});
228211
});
229212

230-
it("returns false when the node is used in a way and has uninteresting tags", function () {
231-
var node = {id: 1, tags: {source: 'who cares?'}};
232-
layer.interestingNode(node, {1: true}, {}).should.be.false;
213+
describe("#buildFeatures", function () {
214+
it("builds a node object", function () {
215+
var features = new L.OSM.DataLayer().buildFeatures(fixture("node"));
216+
features.length.should.eq(1);
217+
features[0].type.should.eq("node");
218+
});
219+
220+
it("builds a way object", function () {
221+
var features = new L.OSM.DataLayer().buildFeatures(fixture("way"));
222+
features.length.should.eq(1);
223+
features[0].type.should.eq("way");
224+
});
233225
});
234226

235-
it("returns true when the node is used in a way and is used in a relation", function () {
236-
var node = {id: 1};
237-
layer.interestingNode(node, {1: true}, {1: true}).should.be.true;
227+
describe("#interestingNode", function () {
228+
var layer = new L.OSM.DataLayer();
229+
230+
it("returns true when the node is not in any ways", function () {
231+
layer.interestingNode({id: 1}, {}, {}).should.be.true;
232+
});
233+
234+
it("returns true when the node has an interesting tag", function () {
235+
var node = {id: 1, tags: {interesting: true}};
236+
layer.interestingNode(node, {1: true}, {1: true}).should.be.true;
237+
});
238+
239+
it("returns false when the node is used in a way and has uninteresting tags", function () {
240+
var node = {id: 1, tags: {source: 'who cares?'}};
241+
layer.interestingNode(node, {1: true}, {}).should.be.false;
242+
});
243+
244+
it("returns true when the node is used in a way and is used in a relation", function () {
245+
var node = {id: 1};
246+
layer.interestingNode(node, {1: true}, {1: true}).should.be.true;
247+
});
238248
});
239249
});
240-
});
250+
});

0 commit comments

Comments
 (0)