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

@ -1,6 +1,6 @@
describe('Bounds', function() {
var a, b, c;
beforeEach(function() {
a = new L.Bounds(
new L.Point(14, 12),
@ -12,7 +12,7 @@ describe('Bounds', function() {
]);
c = new L.Bounds();
});
describe('constructor', function() {
it('should create bounds with proper min & max on (Point, Point)', function() {
expect(a.min).toEqual(new L.Point(14, 12));
@ -23,25 +23,25 @@ describe('Bounds', function() {
expect(b.max).toEqual(new L.Point(30, 40));
});
});
describe('#extend', function() {
it('should extend the bounds to contain the given point', function() {
a.extend(new L.Point(50, 20));
expect(a.min).toEqual(new L.Point(14, 12));
expect(a.max).toEqual(new L.Point(50, 40));
b.extend(new L.Point(25, 50));
expect(b.min).toEqual(new L.Point(14, 12));
expect(b.max).toEqual(new L.Point(30, 50));
});
});
describe('#getCenter', function() {
it('should return the center point', function() {
expect(a.getCenter()).toEqual(new L.Point(22, 26));
});
});
describe('#contains', function() {
it('should contains other bounds or point', function() {
a.extend(new L.Point(50, 10));
@ -51,7 +51,7 @@ describe('Bounds', function() {
expect(a.contains(new L.Point(54, 65))).toBeFalsy();
});
});
describe('#isValid', function() {
it('should return true if properly set up', function() {
expect(a.isValid()).toBeTruthy();
@ -64,4 +64,24 @@ describe('Bounds', function() {
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

@ -1,20 +1,20 @@
describe("Point", function() {
describe('constructor', function() {
it("should create a point with the given x and y", function() {
var p = new L.Point(1.5, 2.5);
expect(p.x).toEqual(1.5);
expect(p.y).toEqual(2.5);
});
it("should round the given x and y if the third argument is true", function() {
var p = new L.Point(1.3, 2.7, true);
expect(p.x).toEqual(1);
expect(p.y).toEqual(3);
});
});
describe('#subtract', function() {
it('should subtract the given point from this one', function() {
var a = new L.Point(50, 30),
@ -22,24 +22,70 @@ describe("Point", function() {
expect(a.subtract(b)).toEqual(new L.Point(30, 20));
});
});
describe('#add', function() {
it('should add the given point to this one', function() {
expect(new L.Point(50, 30).add(new L.Point(20, 10))).toEqual(new L.Point(70, 40));
});
});
describe('#divideBy', function() {
it('should divide this point by the given amount', function() {
expect(new L.Point(50, 30).divideBy(5)).toEqual(new L.Point(10, 6));
});
});
describe('#multiplyBy', function() {
it('should multiply this point by the given amount', function() {
expect(new L.Point(50, 30).multiplyBy(2)).toEqual(new L.Point(100, 60));
});
});
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

@ -1,19 +1,31 @@
describe("Transformation", function() {
var t, p;
beforeEach(function() {
t = new L.Transformation(1, 2, 3, 4);
p = new L.Point(10, 20);
});
it("#transform should perform a transformation", function() {
var p2 = t.transform(p, 2);
expect(p2).toEqual(new L.Point(24, 128));
describe('#transform', function () {
it("should perform a transformation", function() {
var p2 = t.transform(p, 2);
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() {
var p2 = t.transform(p, 2);
var p3 = t.untransform(p2, 2);
expect(p3).toEqual(p);
describe('#untransform', function () {
it("should perform a reverse transformation", function() {
var p2 = t.transform(p, 2);
var p3 = t.untransform(p2, 2);
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));
});
});
});
});