complete geometry tests to 100% #1347

This commit is contained in:
Vladimir Agafonkin 2013-02-05 13:51:27 +02:00
parent 94a69bec1e
commit 17cf297c9b
5 changed files with 209 additions and 29 deletions

View File

@ -64,4 +64,24 @@ describe('Bounds', function() {
expect(c.isValid()).toBeTruthy(); expect(c.isValid()).toBeTruthy();
}); });
}); });
describe('#getSize', function () {
it('should return the size of the bounds as point', function () {
expect(a.getSize()).toEqual(new L.Point(16, 28));
});
});
describe('#intersects', function () {
it('should return true if bounds intersect', function () {
expect(a.intersects(b)).toBe(true);
expect(a.intersects(new L.Bounds(new L.Point(100, 100), new L.Point(120, 120)))).toEqual(false);
});
});
describe('L.bounds factory', function () {
it('should create bounds from array of number arrays', function () {
var bounds = L.bounds([[14, 12], [30, 40]]);
expect(bounds).toEqual(a);
});
});
}); });

View File

@ -0,0 +1,75 @@
describe('LineUtil', function () {
describe('#clipSegment', function () {
var bounds;
beforeEach(function () {
bounds = L.bounds([5, 0], [15, 10]);
});
it('should clip segment by bounds correctly', function () {
var a = new L.Point(0, 0);
var b = new L.Point(15, 15);
var segment = L.LineUtil.clipSegment(a, b, bounds);
expect(segment[0]).toEqual(new L.Point(5, 5));
expect(segment[1]).toEqual(new L.Point(10, 10));
var c = new L.Point(5, -5);
var d = new L.Point(20, 10);
var segment2 = L.LineUtil.clipSegment(c, d, bounds);
expect(segment2[0]).toEqual(new L.Point(10, 0));
expect(segment2[1]).toEqual(new L.Point(15, 5));
});
it('should use last bit code and reject segments out of bounds', function () {
var a = new L.Point(15, 15);
var b = new L.Point(25, 20);
var segment = L.LineUtil.clipSegment(a, b, bounds, true);
expect(segment).toBe(false);
});
});
describe('#pointToSegmentDistance & #closestPointOnSegment', function () {
var p1 = new L.Point(0, 10);
var p2 = new L.Point(10, 0);
var p = new L.Point(0, 0);
it('should calculate distance from point to segment', function () {
expect(L.LineUtil.pointToSegmentDistance(p, p1, p2)).toEqual(Math.sqrt(200) / 2);
});
it('should get point closest to segment', function () {
expect(L.LineUtil.closestPointOnSegment(p, p1, p2)).toEqual(new L.Point(5, 5));
});
});
describe('#simplify', function () {
it('should simplify polylines according to tolerance', function () {
var points = [
new L.Point(0, 0),
new L.Point(0.01, 0),
new L.Point(0.5, 0.01),
new L.Point(0.7, 0),
new L.Point(1, 0),
new L.Point(1.999, 0.999),
new L.Point(2, 1)
];
var simplified = L.LineUtil.simplify(points, 0.1);
expect(simplified).toEqual([
new L.Point(0, 0),
new L.Point(1, 0),
new L.Point(2, 1)
]);
});
});
});

View File

@ -41,5 +41,51 @@ describe("Point", function() {
}); });
}); });
describe('#distanceTo', noSpecs); describe('#floor', function () {
it('should return a new point with floored coordinates', function () {
expect(new L.Point(50.56, 30.123).floor()).toEqual(new L.Point(50, 30));
});
});
describe('#distanceTo', function () {
it('should calculate distance between two points', function () {
var p1 = new L.Point(0, 30);
var p2 = new L.Point(40, 0);
expect(p1.distanceTo(p2)).toEqual(50.0);
});
});
describe('#equals', function () {
it('should return true if points are equal', function () {
var p1 = new L.Point(20.4, 50.12);
var p2 = new L.Point(20.4, 50.12);
var p3 = new L.Point(20.5, 50.13);
expect(p1.equals(p2)).toBe(true);
expect(p1.equals(p3)).toBe(false);
});
});
describe('#toString', function () {
it('should format a string out of point coordinates', function () {
expect(new L.Point(50, 30) + '').toEqual('Point(50, 30)');
});
});
describe('L.point factory', function () {
it('should leave L.Point instances as is', function () {
var p = new L.Point(50, 30);
expect(L.point(p)).toBe(p);
});
it('should create a point out of three arguments', function () {
expect(L.point(50.1, 30.1, true)).toEqual(new L.Point(50, 30));
});
it('should create a point from an array of coordinates', function () {
expect(L.point([50, 30])).toEqual(new L.Point(50, 30));
});
it('should not fail on invalid arguments', function () {
expect(L.point(undefined)).toBe(undefined);
expect(L.point(null)).toBe(null);
});
});
}); });

View File

@ -0,0 +1,27 @@
describe('PolyUtil', function () {
describe('#clipPolygon', function () {
it('should clip polygon by bounds correctly', function () {
var bounds = L.bounds([0, 0], [10, 10]);
var points = [
new L.Point(5, 5),
new L.Point(15, 10),
new L.Point(10, 15)
];
var clipped = L.PolyUtil.clipPolygon(points, bounds);
for (var i = 0, len = clipped.length; i < len; i++) {
delete clipped[i]._code;
}
expect(clipped).toEqual([
new L.Point(7.5, 10),
new L.Point(5, 5),
new L.Point(10, 7.5),
new L.Point(10, 10)
]);
});
});
});

View File

@ -6,14 +6,26 @@ describe("Transformation", function() {
p = new L.Point(10, 20); p = new L.Point(10, 20);
}); });
it("#transform should perform a transformation", function() { describe('#transform', function () {
it("should perform a transformation", function() {
var p2 = t.transform(p, 2); var p2 = t.transform(p, 2);
expect(p2).toEqual(new L.Point(24, 128)); expect(p2).toEqual(new L.Point(24, 128));
}); });
it('should assume scale of 1 if not specified', function () {
var p2 = t.transform(p);
expect(p2).toEqual(new L.Point(12, 64));
});
});
it("#untransform should perform a reverse transformation", function() { describe('#untransform', function () {
it("should perform a reverse transformation", function() {
var p2 = t.transform(p, 2); var p2 = t.transform(p, 2);
var p3 = t.untransform(p2, 2); var p3 = t.untransform(p2, 2);
expect(p3).toEqual(p); expect(p3).toEqual(p);
}); });
it('should assume scale of 1 if not specified', function () {
var p2 = t.transform(p);
expect(t.untransform(new L.Point(12, 64))).toEqual(new L.Point(10, 20));
});
});
}); });