Add tests for d3.geo.stream.
This commit is contained in:
Родитель
8f5fe826c7
Коммит
2e88ce7f03
|
@ -5397,76 +5397,71 @@
|
||||||
while (++i < n) d3_geo_streamLine(coordinates[i], listener);
|
while (++i < n) d3_geo_streamLine(coordinates[i], listener);
|
||||||
listener.polygonEnd();
|
listener.polygonEnd();
|
||||||
}
|
}
|
||||||
function d3_geo_streamBuffer() {
|
function d3_geo_streamBuffer(buffer, pointRadius) {
|
||||||
this._buffer = [];
|
var stream = {
|
||||||
this._point = d3_geo_pathCircle(4.5);
|
point: point,
|
||||||
}
|
lineStart: function() {
|
||||||
d3_geo_streamBuffer.prototype = {
|
stream.point = pointLineStart;
|
||||||
point: d3_geo_streamBufferPoint,
|
},
|
||||||
lineStart: function() {
|
lineEnd: lineEnd,
|
||||||
this.point = d3_geo_streamBufferPointLineStart;
|
polygonStart: function() {
|
||||||
},
|
stream.lineEnd = lineEndPolygon;
|
||||||
lineEnd: d3_geo_streamBufferLineEnd,
|
},
|
||||||
polygonStart: function() {
|
polygonEnd: function() {
|
||||||
this.lineEnd = d3_geo_streamBufferLineEndPolygon;
|
stream.lineEnd = lineEnd;
|
||||||
},
|
}
|
||||||
polygonEnd: function() {
|
};
|
||||||
this.lineEnd = d3_geo_streamBufferLineEnd;
|
function point(x, y) {
|
||||||
},
|
buffer.push("M", x, ",", y, pointRadius);
|
||||||
toString: function() {
|
|
||||||
var s = this._buffer.join("");
|
|
||||||
this._buffer = [];
|
|
||||||
return s;
|
|
||||||
}
|
}
|
||||||
};
|
function pointLineStart(x, y) {
|
||||||
function d3_geo_streamBufferPoint(x, y) {
|
buffer.push("M", x, ",", y);
|
||||||
this._buffer.push("M", x, ",", y, this._point);
|
stream.point = pointLine;
|
||||||
}
|
|
||||||
function d3_geo_streamBufferPointLineStart(x, y) {
|
|
||||||
this._buffer.push("M", x, ",", y);
|
|
||||||
this.point = d3_geo_streamBufferPointLine;
|
|
||||||
}
|
|
||||||
function d3_geo_streamBufferPointLine(x, y) {
|
|
||||||
this._buffer.push("L", x, ",", y);
|
|
||||||
}
|
|
||||||
function d3_geo_streamBufferLineEnd() {
|
|
||||||
this.point = d3_geo_streamBufferPoint;
|
|
||||||
}
|
|
||||||
function d3_geo_streamBufferLineEndPolygon() {
|
|
||||||
this._buffer.push("Z");
|
|
||||||
}
|
|
||||||
function d3_geo_streamContext() {
|
|
||||||
this._pointRadius = 4.5;
|
|
||||||
}
|
|
||||||
d3_geo_streamContext.prototype = {
|
|
||||||
point: d3_geo_streamContextPoint,
|
|
||||||
lineStart: function() {
|
|
||||||
this.point = d3_geo_streamContextPointLineStart;
|
|
||||||
},
|
|
||||||
lineEnd: d3_geo_streamContextLineEnd,
|
|
||||||
polygonStart: function() {
|
|
||||||
this.lineEnd = d3_geo_streamContextLineEndPolygon;
|
|
||||||
},
|
|
||||||
polygonEnd: function() {
|
|
||||||
this.lineEnd = d3_geo_streamContextLineEnd;
|
|
||||||
}
|
}
|
||||||
};
|
function pointLine(x, y) {
|
||||||
function d3_geo_streamContextPoint(x, y) {
|
buffer.push("L", x, ",", y);
|
||||||
this._context.moveTo(x, y);
|
}
|
||||||
this._context.arc(x, y, this._pointRadius, 0, 2 * π);
|
function lineEnd() {
|
||||||
|
stream.point = point;
|
||||||
|
}
|
||||||
|
function lineEndPolygon() {
|
||||||
|
buffer.push("Z");
|
||||||
|
}
|
||||||
|
pointRadius = d3_geo_pathCircle(4.5);
|
||||||
|
return stream;
|
||||||
}
|
}
|
||||||
function d3_geo_streamContextPointLineStart(x, y) {
|
function d3_geo_streamContext(context, pointRadius) {
|
||||||
this._context.moveTo(x, y);
|
var stream = {
|
||||||
this.point = d3_geo_streamContextPointLine;
|
point: point,
|
||||||
}
|
lineStart: function() {
|
||||||
function d3_geo_streamContextPointLine(x, y) {
|
stream.point = pointLineStart;
|
||||||
this._context.lineTo(x, y);
|
},
|
||||||
}
|
lineEnd: lineEnd,
|
||||||
function d3_geo_streamContextLineEnd() {
|
polygonStart: function() {
|
||||||
this.point = d3_geo_streamContextPoint;
|
stream.lineEnd = lineEndPolygon;
|
||||||
}
|
},
|
||||||
function d3_geo_streamContextLineEndPolygon() {
|
polygonEnd: function() {
|
||||||
this._context.closePath();
|
stream.lineEnd = lineEnd;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
function point(x, y) {
|
||||||
|
context.moveTo(x, y);
|
||||||
|
context.arc(x, y, pointRadius, 0, 2 * π);
|
||||||
|
}
|
||||||
|
function pointLineStart(x, y) {
|
||||||
|
context.moveTo(x, y);
|
||||||
|
stream.point = pointLine;
|
||||||
|
}
|
||||||
|
function pointLine(x, y) {
|
||||||
|
context.lineTo(x, y);
|
||||||
|
}
|
||||||
|
function lineEnd() {
|
||||||
|
stream.point = point;
|
||||||
|
}
|
||||||
|
function lineEndPolygon() {
|
||||||
|
context.closePath();
|
||||||
|
}
|
||||||
|
return stream;
|
||||||
}
|
}
|
||||||
function d3_geo_spherical(cartesian) {
|
function d3_geo_spherical(cartesian) {
|
||||||
return [ Math.atan2(cartesian[1], cartesian[0]), Math.asin(Math.max(-1, Math.min(1, cartesian[2]))) ];
|
return [ Math.atan2(cartesian[1], cartesian[0]), Math.asin(Math.max(-1, Math.min(1, cartesian[2]))) ];
|
||||||
|
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -0,0 +1,261 @@
|
||||||
|
require("../env");
|
||||||
|
|
||||||
|
var vows = require("vows"),
|
||||||
|
assert = require("assert");
|
||||||
|
|
||||||
|
var suite = vows.describe("d3.geo.stream");
|
||||||
|
|
||||||
|
suite.addBatch({
|
||||||
|
"stream": {
|
||||||
|
topic: function() {
|
||||||
|
return d3.geo.stream;
|
||||||
|
},
|
||||||
|
"does not allow null input": function(stream) {
|
||||||
|
try {
|
||||||
|
stream(null);
|
||||||
|
assert.fail("expected error");
|
||||||
|
} catch (expected) {}
|
||||||
|
},
|
||||||
|
"ignores unknown types": function(stream) {
|
||||||
|
stream({type: "Unknown"}, {});
|
||||||
|
stream({type: "Feature", geometry: {type: "Unknown"}}, {});
|
||||||
|
stream({type: "FeatureCollection", features: [{type: "Feature", geometry: {type: "Unknown"}}]}, {});
|
||||||
|
stream({type: "GeometryCollection", geometries: [{type: "Unknown"}]}, {});
|
||||||
|
},
|
||||||
|
"returns void": function(stream) {
|
||||||
|
assert.isUndefined(stream({type: "Point", coordinates: [1, 2]}, {point: function() { return true; }}));
|
||||||
|
},
|
||||||
|
"allows empty multi-geometries": function(stream) {
|
||||||
|
stream({type: "MultiPoint", coordinates: []}, {});
|
||||||
|
stream({type: "MultiLineString", coordinates: []}, {});
|
||||||
|
stream({type: "MultiPolygon", coordinates: []}, {});
|
||||||
|
},
|
||||||
|
"Sphere ↦ sphere": function(stream) {
|
||||||
|
var calls = 0;
|
||||||
|
stream({type: "Sphere"}, {
|
||||||
|
sphere: function() {
|
||||||
|
++calls;
|
||||||
|
assert.equal(arguments.length, 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assert.equal(calls, 1);
|
||||||
|
},
|
||||||
|
"Point ↦ point": function(stream) {
|
||||||
|
var calls = 0;
|
||||||
|
stream({type: "Point", coordinates: [1, 2]}, {
|
||||||
|
point: function(x, y) {
|
||||||
|
++calls;
|
||||||
|
assert.equal(arguments.length, 2);
|
||||||
|
assert.equal(x, 1);
|
||||||
|
assert.equal(y, 2);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assert.equal(calls, 1);
|
||||||
|
},
|
||||||
|
"MultiPoint ↦ point*": function(stream) {
|
||||||
|
var calls = 0;
|
||||||
|
stream({type: "MultiPoint", coordinates: [[1, 2], [3, 4]]}, {
|
||||||
|
point: function(x, y) {
|
||||||
|
assert.equal(arguments.length, 2);
|
||||||
|
if (++calls === 1) {
|
||||||
|
assert.equal(x, 1);
|
||||||
|
assert.equal(y, 2);
|
||||||
|
} else {
|
||||||
|
assert.equal(x, 3);
|
||||||
|
assert.equal(y, 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assert.equal(calls, 2);
|
||||||
|
},
|
||||||
|
"LineString ↦ lineStart, point{2,}, lineEnd": function(stream) {
|
||||||
|
var calls = 0;
|
||||||
|
stream({type: "LineString", coordinates: [[1, 2], [3, 4]]}, {
|
||||||
|
lineStart: function() {
|
||||||
|
assert.equal(++calls, 1);
|
||||||
|
assert.equal(arguments.length, 0);
|
||||||
|
},
|
||||||
|
point: function(x, y) {
|
||||||
|
assert.equal(arguments.length, 2);
|
||||||
|
if (++calls === 2) {
|
||||||
|
assert.equal(x, 1);
|
||||||
|
assert.equal(y, 2);
|
||||||
|
} else if (calls === 3) {
|
||||||
|
assert.equal(x, 3);
|
||||||
|
assert.equal(y, 4);
|
||||||
|
} else {
|
||||||
|
assert.fail("too many points");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
lineEnd: function() {
|
||||||
|
assert.equal(++calls, 4);
|
||||||
|
assert.equal(arguments.length, 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assert.equal(calls, 4);
|
||||||
|
},
|
||||||
|
"MultiLineString ↦ (lineStart, point{2,}, lineEnd)*": function(stream) {
|
||||||
|
var calls = 0;
|
||||||
|
stream({type: "MultiLineString", coordinates: [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]}, {
|
||||||
|
lineStart: function() {
|
||||||
|
++calls;
|
||||||
|
assert.isTrue(calls === 1 || calls === 5);
|
||||||
|
assert.equal(arguments.length, 0);
|
||||||
|
},
|
||||||
|
point: function(x, y) {
|
||||||
|
++calls;
|
||||||
|
assert.equal(arguments.length, 2);
|
||||||
|
if (calls === 2) {
|
||||||
|
assert.equal(x, 1);
|
||||||
|
assert.equal(y, 2);
|
||||||
|
} else if (calls === 3) {
|
||||||
|
assert.equal(x, 3);
|
||||||
|
assert.equal(y, 4);
|
||||||
|
} else if (calls === 6) {
|
||||||
|
assert.equal(x, 5);
|
||||||
|
assert.equal(y, 6);
|
||||||
|
} else if (calls === 7) {
|
||||||
|
assert.equal(x, 7);
|
||||||
|
assert.equal(y, 8);
|
||||||
|
} else {
|
||||||
|
assert.fail("too many points");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
lineEnd: function() {
|
||||||
|
++calls;
|
||||||
|
assert.isTrue(calls === 4 || calls === 8);
|
||||||
|
assert.equal(arguments.length, 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assert.equal(calls, 8);
|
||||||
|
},
|
||||||
|
"Polygon ↦ polygonStart, lineStart, point{3,}, lineEnd, polygonEnd": function(stream) {
|
||||||
|
var calls = 0;
|
||||||
|
stream({type: "Polygon", coordinates: [[[1, 2], [3, 4], [1, 2]], [[5, 6], [7, 8], [5, 6]]]}, {
|
||||||
|
polygonStart: function() {
|
||||||
|
++calls;
|
||||||
|
assert.isTrue(calls === 1);
|
||||||
|
assert.equal(arguments.length, 0);
|
||||||
|
},
|
||||||
|
lineStart: function() {
|
||||||
|
++calls;
|
||||||
|
assert.isTrue(calls === 2 || calls === 7);
|
||||||
|
assert.equal(arguments.length, 0);
|
||||||
|
},
|
||||||
|
point: function(x, y) {
|
||||||
|
++calls;
|
||||||
|
assert.equal(arguments.length, 2);
|
||||||
|
if (calls === 3 || calls === 5) {
|
||||||
|
assert.equal(x, 1);
|
||||||
|
assert.equal(y, 2);
|
||||||
|
} else if (calls === 4) {
|
||||||
|
assert.equal(x, 3);
|
||||||
|
assert.equal(y, 4);
|
||||||
|
} else if (calls === 8 || calls === 10) {
|
||||||
|
assert.equal(x, 5);
|
||||||
|
assert.equal(y, 6);
|
||||||
|
} else if (calls === 9) {
|
||||||
|
assert.equal(x, 7);
|
||||||
|
assert.equal(y, 8);
|
||||||
|
} else {
|
||||||
|
assert.fail("too many points");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
lineEnd: function() {
|
||||||
|
++calls;
|
||||||
|
assert.isTrue(calls === 6 || calls === 11);
|
||||||
|
assert.equal(arguments.length, 0);
|
||||||
|
},
|
||||||
|
polygonEnd: function() {
|
||||||
|
++calls;
|
||||||
|
assert.isTrue(calls === 12);
|
||||||
|
assert.equal(arguments.length, 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assert.equal(calls, 12);
|
||||||
|
},
|
||||||
|
"MultiPolygon ↦ (polygonStart, lineStart, point{3,}, lineEnd, polygonEnd)*": function(stream) {
|
||||||
|
var calls = 0;
|
||||||
|
stream({type: "MultiPolygon", coordinates: [[[[1, 2], [3, 4], [1, 2]]], [[[5, 6], [7, 8], [5, 6]]]]}, {
|
||||||
|
polygonStart: function() {
|
||||||
|
++calls;
|
||||||
|
assert.isTrue(calls === 1 || calls === 8);
|
||||||
|
assert.equal(arguments.length, 0);
|
||||||
|
},
|
||||||
|
lineStart: function() {
|
||||||
|
++calls;
|
||||||
|
assert.isTrue(calls === 2 || calls === 9);
|
||||||
|
assert.equal(arguments.length, 0);
|
||||||
|
},
|
||||||
|
point: function(x, y) {
|
||||||
|
++calls;
|
||||||
|
assert.equal(arguments.length, 2);
|
||||||
|
if (calls === 3 || calls === 5) {
|
||||||
|
assert.equal(x, 1);
|
||||||
|
assert.equal(y, 2);
|
||||||
|
} else if (calls === 4) {
|
||||||
|
assert.equal(x, 3);
|
||||||
|
assert.equal(y, 4);
|
||||||
|
} else if (calls === 10 || calls === 12) {
|
||||||
|
assert.equal(x, 5);
|
||||||
|
assert.equal(y, 6);
|
||||||
|
} else if (calls === 11) {
|
||||||
|
assert.equal(x, 7);
|
||||||
|
assert.equal(y, 8);
|
||||||
|
} else {
|
||||||
|
assert.fail("too many points");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
lineEnd: function() {
|
||||||
|
++calls;
|
||||||
|
assert.isTrue(calls === 6 || calls === 13);
|
||||||
|
assert.equal(arguments.length, 0);
|
||||||
|
},
|
||||||
|
polygonEnd: function() {
|
||||||
|
++calls;
|
||||||
|
assert.isTrue(calls === 7 || calls === 14);
|
||||||
|
assert.equal(arguments.length, 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assert.equal(calls, 14);
|
||||||
|
},
|
||||||
|
"Feature ↦ .*": function(stream) {
|
||||||
|
var calls = 0;
|
||||||
|
stream({type: "Feature", geometry: {type: "Point", coordinates: [1, 2]}}, {
|
||||||
|
point: function(x, y) {
|
||||||
|
++calls;
|
||||||
|
assert.equal(arguments.length, 2);
|
||||||
|
assert.equal(x, 1);
|
||||||
|
assert.equal(y, 2);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assert.equal(calls, 1);
|
||||||
|
},
|
||||||
|
"FeatureCollection ↦ .*": function(stream) {
|
||||||
|
var calls = 0;
|
||||||
|
stream({type: "FeatureCollection", features: [{type: "Feature", geometry: {type: "Point", coordinates: [1, 2]}}]}, {
|
||||||
|
point: function(x, y) {
|
||||||
|
++calls;
|
||||||
|
assert.equal(arguments.length, 2);
|
||||||
|
assert.equal(x, 1);
|
||||||
|
assert.equal(y, 2);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assert.equal(calls, 1);
|
||||||
|
},
|
||||||
|
"GeometryCollection ↦ .*": function(stream) {
|
||||||
|
var calls = 0;
|
||||||
|
stream({type: "GeometryCollection", geometries: [{type: "Point", coordinates: [1, 2]}]}, {
|
||||||
|
point: function(x, y) {
|
||||||
|
++calls;
|
||||||
|
assert.equal(arguments.length, 2);
|
||||||
|
assert.equal(x, 1);
|
||||||
|
assert.equal(y, 2);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assert.equal(calls, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
suite.export(module);
|
Загрузка…
Ссылка в новой задаче