Test & improve the getCentroid/centroid method of collections

This commit is contained in:
Marc Jansen
2012-06-22 18:23:40 +02:00
parent abb254a0fb
commit be9448457d
3 changed files with 73 additions and 2 deletions

View File

@@ -180,8 +180,12 @@ ol.geom.Collection.prototype.getCentroid = function() {
if (len > 0) {
goog.array.forEach(components, function(component){
var singleCentroid = component.getCentroid();
sum_x += singleCentroid.getX();
sum_y += singleCentroid.getX();
if (goog.isDefAndNotNull(singleCentroid)) {
sum_x += singleCentroid.getX();
sum_y += singleCentroid.getX();
} else {
len--;
}
});
centroid = new ol.geom.Point(sum_x / len, sum_y / len);
}

View File

@@ -178,6 +178,40 @@ describe("ol.geom.collection", function() {
expect(c.components().length).toBe(0);
});
});
describe("the centroid method is functional", function(){
it("returns an instance of ol.geom.Point", function(){
expect(c.centroid()).toBeA(ol.geom.Point);
});
it("does not choke when components returns a null centroid", function(){
var centroid;
expect(
function(){
c.add(new ol.geom.linestring([]));
centroid = c.centroid();
}
).not.toThrow();
expect(centroid).toBeA(ol.geom.Point);
});
it("has the expected coordinates", function(){
c = ol.geom.collection([
ol.geom.point([10,10]),
ol.geom.point([30,30]),
ol.geom.linestring([
ol.geom.point([10,10]),
ol.geom.point([10,30]),
ol.geom.point([30,30]),
ol.geom.point([30,10])
])
]);
debugger;
var centroid = c.centroid();
expect(centroid.x() + ',' + centroid.y()).toBe('20,20');
});
});
});

View File

@@ -132,4 +132,37 @@ describe("ol.geom.Collection", function() {
expect( components[1].getX() + ',' + components[1].getY()).toBe( '10,20' );
expect( components[2].getX() + ',' + components[2].getY()).toBe( '30,40' );
});
describe("the getCentroid method is functional", function(){
it("returns an instance of ol.geom.Point", function(){
expect(c.getCentroid()).toBeA(ol.geom.Point);
});
it("does not choke when components returns a null centroid", function(){
var centroid;
expect(
function(){
c.addComponent(new ol.geom.LineString([]));
centroid = c.getCentroid();
}
).not.toThrow();
expect(centroid).toBeA(ol.geom.Point);
});
it("has the expected coordinates", function(){
c = new ol.geom.Collection([
new ol.geom.Point(10,10),
new ol.geom.Point(30,30),
new ol.geom.LineString([
new ol.geom.Point(10,10),
new ol.geom.Point(10,30),
new ol.geom.Point(30,30),
new ol.geom.Point(30,10)
])
]);
var centroid = c.getCentroid();
expect(centroid.getX() + ',' + centroid.getY()).toBe('20,20');
});
});
});