Refactor tests with better structure following the Arrange-Act-Assert principle

This commit is contained in:
GabrielePrestifilippo 2017-01-16 22:28:44 +01:00
parent 65e1000b63
commit 446b410355
4 changed files with 282 additions and 149 deletions

View File

@ -9,98 +9,153 @@ define([
describe("AngleTest", function () {
it("Normalizes a specified value to be within the range of [-180, 180] degrees", function () {
//expected values
var normalized = Angle.normalizedDegrees(190);
expect(normalized).toEqual(-170);
describe("Normalizes a value in the range of [-180, 180] degrees", function () {
normalized = Angle.normalizedDegrees(360);
expect(normalized).toEqual(0);
it("Normalizes an angle", function () {
var normalized = Angle.normalizedDegrees(190);
expect(normalized).toEqual(-170);
});
//undefined or NaN testing
normalized = Angle.normalizedDegrees(undefined);
expect(normalized).toEqual(NaN);
it("Normalizes a full angle", function () {
var normalized = Angle.normalizedDegrees(360);
expect(normalized).toEqual(0);
});
normalized = Angle.normalizedDegrees(NaN);
expect(normalized).toEqual(NaN);
it("Normalizes an undefined angle", function () {
var normalized = Angle.normalizedDegrees(undefined);
expect(normalized).toEqual(NaN);
});
it("Normalizes a NaN", function () {
var normalized = Angle.normalizedDegrees(NaN);
expect(normalized).toEqual(NaN);
});
});
it("Normalizes a specified value to be within the range of [-90, 90] degrees", function () {
var normalized = Angle.normalizedDegreesLatitude(190);
expect(normalized).toEqual(10);
describe("Normalizes a Latitude value in the range of [-90, 90] degrees", function () {
normalized = Angle.normalizedDegreesLatitude(-91);
expect(normalized).toEqual(-89);
it("Normalizes a positive angle", function () {
var normalized = Angle.normalizedDegreesLatitude(190);
expect(normalized).toEqual(10);
});
normalized = Angle.normalizedDegreesLatitude(360);
expect(normalized).toEqual(0);
it("Normalizes a negative angle", function () {
var normalized = Angle.normalizedDegreesLatitude(-91);
expect(normalized).toEqual(-89);
});
it("Normalizes a full angle", function () {
var normalized = Angle.normalizedDegreesLatitude(360);
expect(normalized).toEqual(0);
});
});
it("Normalizes a specified value to be within the range of [-180, 180] degrees", function () {
var normalized = Angle.normalizedDegreesLongitude(190);
expect(normalized).toEqual(-170);
describe("Normalizes a Longitude value in the range of [-180, 180] degrees", function () {
normalized = Angle.normalizedDegreesLongitude(460);
expect(normalized).toEqual(100);
it("Normalizes a positive angle", function () {
var normalized = Angle.normalizedDegreesLongitude(190);
expect(normalized).toEqual(-170);
});
it("Normalizes a negative angle", function () {
var normalized = Angle.normalizedDegreesLongitude(-460);
expect(normalized).toEqual(-100);
});
});
it("Normalizes a specified value to be within the range of [-Pi, Pi] radians", function () {
var normalized = Angle.normalizedRadians(45);
expect(normalized).toEqual(45-14*Math.PI);
describe("Normalizes a specified value to be within the range of [-Pi, Pi] radians", function () {
normalized = Angle.normalizedRadians(60);
expect(normalized).toEqual(60-20*Math.PI);
it("Normalizes a 45 degree angle", function () {
var normalized = Angle.normalizedRadians(45);
expect(normalized).toEqual(45 - 14 * Math.PI);
});
it("Normalizes a 60 degree angle", function () {
var normalized = Angle.normalizedRadians(60);
expect(normalized).toEqual(60 - 20 * Math.PI);
});
});
it("Normalizes a specified value to be within the range of [-Pi/2, Pi/2] radians.", function () {
var normalized = Angle.normalizedRadiansLatitude(60);
expect(normalized).toBeCloseTo(60-19*Math.PI, 10);
describe("Normalizes a specified value to be within the range of [-Pi/2, Pi/2] radians.", function () {
normalized = Angle.normalizedRadiansLatitude(88);
expect(normalized).toBeCloseTo(88-28*Math.PI, 10);
it("Normalizes a 60 degree angle", function () {
var normalized = Angle.normalizedRadiansLatitude(60);
expect(normalized).toBeCloseTo(60 - 19 * Math.PI, 10);
});
it("Normalizes an 88 degree angle", function () {
var normalized = Angle.normalizedRadiansLatitude(88);
expect(normalized).toBeCloseTo(88 - 28 * Math.PI, 10);
});
});
it("Normalizes a specified value to be within the range of [-Pi/2, Pi/2] radians", function () {
var normalized = Angle.normalizedRadiansLongitude(60);
expect(normalized).toBeCloseTo(60-20*Math.PI, 10);
describe("Normalizes a specified value to be within the range of [-Pi/2, Pi/2] radians", function () {
it("Normalizes a 60 degree angle", function () {
var normalized = Angle.normalizedRadiansLongitude(60);
expect(normalized).toBeCloseTo(60 - 20 * Math.PI, 10);
});
normalized = Angle.normalizedRadiansLongitude(180);
expect(normalized).toBeCloseTo(180-58*Math.PI, 10);
it("Normalizes a 180 degree angle", function () {
var normalized = Angle.normalizedRadiansLongitude(180);
expect(normalized).toBeCloseTo(180 - 58 * Math.PI, 10);
});
});
it("Indicates whether a specified value is within the normal range of latitude, [-90, 90]", function () {
var validLatitude = Angle.isValidLatitude(60);
expect(validLatitude).toEqual(true);
describe("Indicates whether a specified value is within the normal range of latitude, [-90, 90]", function () {
var invalidLatitude = Angle.isValidLatitude(150);
expect(invalidLatitude).toEqual(false);
it("Valid latitude", function () {
var validLatitude = Angle.isValidLatitude(60);
expect(validLatitude).toEqual(true);
});
it("Not valid latitude", function () {
var invalidLatitude = Angle.isValidLatitude(150);
expect(invalidLatitude).toEqual(false);
});
});
it("Indicates whether a specified value is within the normal range of longitude, [-180, 180]", function () {
var validLongitude = Angle.isValidLongitude(150);
expect(validLongitude).toEqual(true);
describe("Indicates whether a specified value is within the normal range of longitude, [-180, 180]", function () {
var invalidLongitude = Angle.isValidLongitude(-250);
expect(invalidLongitude).toEqual(false);
it("Valid longitude", function () {
var validLongitude = Angle.isValidLongitude(150);
expect(validLongitude).toEqual(true);
});
it("Not valid longitude", function () {
var invalidLongitude = Angle.isValidLongitude(-250);
expect(invalidLongitude).toEqual(false);
});
});
it("Returns a decimal degrees string representation of a specified value in degrees", function () {
var angle = Angle.toDecimalDegreesString(150);
expect(angle).toEqual("150\u00B0");
describe("Returns a decimal degrees string representation of a specified value in degrees", function () {
angle = Angle.toDecimalDegreesString(-150);
expect(angle).toEqual("-150\u00B0");
it("Positive degrees", function () {
var angle = Angle.toDecimalDegreesString(150);
expect(angle).toEqual("150\u00B0");
});
it("Negative degrees", function () {
var angle = Angle.toDecimalDegreesString(-150);
expect(angle).toEqual("-150\u00B0");
});
});
it("Returns a degrees-minutes-seconds string representation of a specified value in degrees", function () {
var angle = Angle.toDMSString(150);
expect(angle).toEqual("150\u00B0 0\u2019 0\u201D");
describe("Returns a degrees-minutes-seconds string representation of a specified value in degrees", function () {
angle = Angle.toDMSString(270.65);
expect(angle).toEqual("270\u00B0 39\u2019 0\u201D");
it("Integer angle", function () {
var angle = Angle.toDMSString(150);
expect(angle).toEqual("150\u00B0 0\u2019 0\u201D");
});
angle = Angle.toDMSString(50.50556);
expect(angle).toEqual("50\u00B0 30\u2019 20\u201D");
it("Not integer angle with two decimals expecting minutes", function () {
var angle = Angle.toDMSString(270.65);
expect(angle).toEqual("270\u00B0 39\u2019 0\u201D");
});
it("Not integer angle with five decimals expecting seconds", function () {
var angle = Angle.toDMSString(50.50556);
expect(angle).toEqual("50\u00B0 30\u2019 20\u201D");
});
});
});

View File

@ -31,19 +31,22 @@ define([
});
describe("Set to points method", function () {
var boundingBox = new BoundingBox();
it("Sets this bounding box such that it minimally encloses a specified collection of points", function () {
var boundingBox = new BoundingBox();
var samplePoints = new Float32Array([1, 0, 0, 0, 1, 0, 0, 0, 1]);
expect(boundingBox.setToPoints(samplePoints).radius).toEqual(1.0606601717798212);
});
it("Should throw an exception because of the wrong input", function () {
//less than 3 points provided
it("Should throw an exception because only two points provided", function () {
var boundingBox = new BoundingBox();
expect(function () {
boundingBox.setToPoints(new Float32Array(2))
}).toThrow();
});
//no points provided
it("Should throw an exception because of the wrong input", function () { //no points provided
var boundingBox = new BoundingBox();
expect(function () {
boundingBox.setToPoints(null)
}).toThrow();
@ -52,20 +55,22 @@ define([
});
describe("Set to sector method", function () {
var boundingBox = new BoundingBox();
it("Sets this bounding box to contain a specified sector with min and max elevation", function () {
var boundingBox = new BoundingBox();
var globe = new Globe(new EarthElevationModel());
var sector = new Sector(-90, 90, -180, 180);
expect(boundingBox.setToSector(sector, globe, 10, 1000).radius).toEqual(9006353.499282671);
});
it("Should throw an exception because of missing inputs", function () {
//no globe provided
it("Should throw an exception because no globe is provided", function () {
var boundingBox = new BoundingBox();
expect(function () {
boundingBox.setToSector(sector, null, 10, 1000)
}).toThrow();
});
//no sector provided
it("Should throw an exception because no sector is provided", function () {
expect(function () {
boundingBox.setToSector(null, globe, 10, 1000)
}).toThrow();
@ -73,9 +78,9 @@ define([
});
describe("Translates this bounding box by a specified translation vector", function () {
var boundingBox = new BoundingBox();
it("Should return the translated bounding box", function () {
var boundingBox = new BoundingBox();
boundingBox.translate(new Vec3(1, 2, 3));
expect(boundingBox.center).toEqual(new Vec3(1, 2, 3));
expect(boundingBox.bottomCenter).toEqual(new Vec3(0.5, 2, 3));
@ -83,6 +88,7 @@ define([
});
it("Should throw an error on null vector", function () {
var boundingBox = new BoundingBox();
expect(function () {
boundingBox.translate(null)
}).toThrow();
@ -90,14 +96,15 @@ define([
});
describe("Computes the approximate distance between this bounding box and a specified point", function () {
var boundingBox = new BoundingBox();
it("Should return the correct distance", function () {
var boundingBox = new BoundingBox();
var point = new Vec3(3, 2, 5);
expect(boundingBox.distanceTo(point)).toBeCloseTo(4.432, 3);
});
it("Should throw an error on null point", function () {
var boundingBox = new BoundingBox();
expect(function () {
boundingBox.distanceTo(null)
}).toThrow();
@ -105,14 +112,15 @@ define([
});
describe("Computes the effective radius of this bounding box relative to a specified plane", function () {
var boundingBox = new BoundingBox();
it("Should return the correct radius", function () {
var boundingBox = new BoundingBox();
var plane = {normal: new Vec3(1, 2, 3)};
expect(boundingBox.effectiveRadius(plane)).toEqual(3);
});
it("Should throw an error on null plane", function () {
var boundingBox = new BoundingBox();
expect(function () {
boundingBox.effectiveRadius(null)
}).toThrow();
@ -140,7 +148,7 @@ define([
it("Should return a positive intersection", function () {
var p = new Plane(0.8, -0.5, 0.3, 10000000);
var frustum = {near: p,far:p,top:p,left:p,right:p,bottom:p};
var frustum = {near: p, far: p, top: p, left: p, right: p, bottom: p};
expect(boundingBox.intersectsFrustum(frustum)).toEqual(true);
});

View File

@ -15,17 +15,22 @@ define([
expect(vec2[1]).toEqual(8);
});
it("Set components of the vector", function () {
var vec2 = new Vec2();
vec2.set(2, 3);
expect(vec2).toEqual(new Vec2(2, 3));
describe("#Set components", function () {
vec2.set(5, 6);
expect(vec2[0]).toEqual(5);
expect(vec2[1]).toEqual(6);
it('sets vector equal to different vector', function () {
var vec2 = new Vec2();
vec2.set(2, 3);
expect(vec2).toEqual(new Vec2(2, 3));
});
it('sets vector and verify by components', function () {
var vec2 = new Vec2();
vec2.set(5, 6);
expect(vec2[0]).toEqual(5);
expect(vec2[1]).toEqual(6);
});
});
it("Copies the component of a Vec2", function () {
var destination = new Vec2();
var source = new Vec2(2, 3);
@ -33,13 +38,19 @@ define([
expect(destination).toEqual(source);
});
it("Check if two vectors are equal", function () {
var vec2_a = new Vec2(2, 3);
var vec2_b = new Vec2(2, 3);
expect(vec2_a.equals(vec2_b)).toEqual(true);
describe("#Vectors equality", function () {
vec2_b = new Vec2(4, 5);
expect(vec2_a.equals(vec2_b)).toEqual(false);
it("Two equal vectors", function () {
var vec2_a = new Vec2(2, 3);
var vec2_b = new Vec2(2, 3);
expect(vec2_a.equals(vec2_b)).toEqual(true);
});
it("Two unequal vectors", function () {
var vec2_a = new Vec2(2, 3);
var vec2_b = new Vec2(4, 5);
expect(vec2_a.equals(vec2_b)).toEqual(false);
});
});
it("Should return the average of a vector", function () {
@ -67,24 +78,37 @@ define([
expect(expected_Vec2).toEqual(new Vec2(6, 9));
});
it("Divides current vector by a scalar", function () {
var vec2 = new Vec2(6, 8);
var expected_vec2 = vec2.divide(2);
expect(expected_vec2).toEqual(new Vec2(3, 4));
describe("#Vector division", function () {
expected_vec2 = vec2.divide(4);
expect(expected_vec2).toEqual(new Vec2(0.75, 1));
it("Division by scalar with integer output", function () {
var vec2 = new Vec2(6, 8);
var expected_vec2 = vec2.divide(2);
expect(expected_vec2).toEqual(new Vec2(3, 4));
});
it("Division by scalar with non integer output", function () {
var vec2 = new Vec2(6, 8);
var expected_vec2 = vec2.divide(4);
expect(expected_vec2).toEqual(new Vec2(1.5, 2));
});
});
it("Interpolates a specified vector with this vector", function () {
var vec2_a = new WorldWind.Vec2(2, 3);
var vec2_b = new WorldWind.Vec2(4, 6);
var expected_vec2 = new WorldWind.Vec2(3, 4.5);
expect(vec2_a.mix(vec2_b, 0.5)).toEqual(expected_vec2);
describe("#Vector interpolation", function () {
expected_vec2 = new WorldWind.Vec2(8, 12);
expect(vec2_a.mix(vec2_b, 5)).toEqual(expected_vec2);
it("Interpolates with an integer weight", function () {
var vec2_a = new WorldWind.Vec2(2, 3);
var vec2_b = new WorldWind.Vec2(4, 6);
var expected_vec2 = new WorldWind.Vec2(12, 18);
expect(vec2_a.mix(vec2_b, 5)).toEqual(expected_vec2);
});
it("Interpolates with a non integer weight", function () {
var vec2_a = new WorldWind.Vec2(2, 3);
var vec2_b = new WorldWind.Vec2(4, 6);
var expected_vec2 = new WorldWind.Vec2(3, 4.5);
expect(vec2_a.mix(vec2_b, 0.5)).toEqual(expected_vec2);
});
});
@ -93,7 +117,7 @@ define([
var expected_vec2 = new WorldWind.Vec2(-2, -3);
expect(vec2_a.negate()).toEqual(expected_vec2);
});
it("Computes the scalar dot product of the current vector and a specified one", function () {
var vec2_a = new WorldWind.Vec2(2, 3);
var vec2_b = new WorldWind.Vec2(4, 5);
@ -106,12 +130,17 @@ define([
expect(vec2_a.magnitudeSquared()).toEqual(13);
});
it("Computes the magnitude of this vector", function () {
var vec2 = new WorldWind.Vec2(4, -3);
expect(vec2.magnitude()).toEqual(5);
describe("#Magnitude of a vector", function () {
vec2 = new WorldWind.Vec2(6, 8);
expect(vec2.magnitude()).toEqual(10);
it("Computes the magnitude with full positive components", function () {
var vec2 = new WorldWind.Vec2(6, 8);
expect(vec2.magnitude()).toEqual(10);
});
it("Computes the magnitude with a negative component", function () {
var vec2 = new WorldWind.Vec2(4, -3);
expect(vec2.magnitude()).toEqual(5);
});
});
it("Normalize this vector to a unit vector", function () {

View File

@ -74,27 +74,37 @@ define([
});
it("Computer buffer normals", function () {
var bufferNormal = Vec3.computeBufferNormal([0, 0, 0, 1, 0, 0, 1, 1, 0], 1);
expect(bufferNormal).toEqual(new Vec3(-1, 0, 0));
describe('#computeBufferNormal', function () {
bufferNormal = Vec3.computeBufferNormal([0, 0, 0, 1, 0, 0, 1, 1, 0], 3);
expect(bufferNormal).toEqual(new Vec3(0, 0, 1));
it("Buffer normal with stride of 1", function () {
var bufferNormal = Vec3.computeBufferNormal([0, 0, 0, 1, 0, 0, 1, 1, 0], 1);
expect(bufferNormal).toEqual(new Vec3(-1, 0, 0));
});
it("Buffer normal with stride of 3", function () {
var bufferNormal = Vec3.computeBufferNormal([0, 0, 0, 1, 0, 0, 1, 1, 0], 3);
expect(bufferNormal).toEqual(new Vec3(0, 0, 1));
});
});
it("Set components of the vector", function () {
var vec3 = Vec3.ZERO;
vec3.set(2, 3, 4);
expect(vec3).toEqual(new Vec3(2, 3, 4));
vec3.set(5, 6, 7);
expect(vec3[0]).toEqual(5);
expect(vec3[1]).toEqual(6);
expect(vec3[2]).toEqual(7);
describe('#Set components', function () {
it('sets vector equal to different vector', function () {
var vec3 = Vec3.ZERO;
vec3.set(2, 3, 4);
expect(vec3).toEqual(new Vec3(2, 3, 4));
});
it('sets vector and verify by components', function () {
var vec3 = Vec3.ZERO;
vec3.set(5, 6, 7);
expect(vec3[0]).toEqual(5);
expect(vec3[1]).toEqual(6);
expect(vec3[2]).toEqual(7);
})
});
it("Copies the component of a Vec3", function () {
var destination = Vec3.ZERO;
var source = new Vec3(2, 3, 4);
@ -102,13 +112,20 @@ define([
expect(destination).toEqual(source);
});
it("Check if two vectors are equal", function () {
var vec3_a = new Vec3(2, 3, 4);
var vec3_b = new Vec3(2, 3, 4);
expect(vec3_a.equals(vec3_b)).toEqual(true);
describe('#Vectors quality', function () {
vec3_b = new Vec3(4, 5, 6);
expect(vec3_a.equals(vec3_b)).toEqual(false);
it("Two equal vectors", function () {
var vec3_a = new Vec3(2, 3, 4);
var vec3_b = new Vec3(2, 3, 4);
expect(vec3_a.equals(vec3_b)).toEqual(true);
});
it("Two unequal vectors", function () {
var vec3_a = new Vec3(2, 3, 4);
var vec3_b = new Vec3(4, 5, 6);
expect(vec3_a.equals(vec3_b)).toEqual(false);
});
});
it("Add a vector to the current one", function () {
@ -129,14 +146,20 @@ define([
expect(expected_vec3).toEqual(new Vec3(6, 9, 12));
});
it("Divides current vector by a scalar", function () {
var vec3 = new Vec3(6, 8, 4);
var expected_vec3 = vec3.divide(2);
expect(expected_vec3).toEqual(new Vec3(3, 4, 2));
describe('#Scalar division', function () {
expected_vec3 = vec3.divide(4);
expect(expected_vec3).toEqual(new Vec3(0.75, 1, 0.5));
it("Division with integer output", function () {
var vec3 = new Vec3(6, 8, 4);
var expected_vec3 = vec3.divide(2);
expect(expected_vec3).toEqual(new Vec3(3, 4, 2));
});
it("Divides with non integer output", function () {
var vec3 = new Vec3(6, 8, 4);
var expected_vec3 = vec3.divide(4);
expect(expected_vec3).toEqual(new Vec3(1.5, 2, 1));
});
});
it("Multiplies current vector by 4x4 Matrix", function () {
@ -147,15 +170,21 @@ define([
});
it("Interpolates a specified vector with this vector", function () {
var vec3_a = new WorldWind.Vec3(2, 3, 4);
var vec3_b = new WorldWind.Vec3(4, 6, 8);
var expected_vec3 = new WorldWind.Vec3(3, 4.5, 6);
expect(vec3_a.mix(vec3_b, 0.5)).toEqual(expected_vec3);
describe('#Vector interpolation', function () {
expected_vec3 = new WorldWind.Vec3(8, 12, 16);
expect(vec3_a.mix(vec3_b, 5)).toEqual(expected_vec3);
it("Interpolates with an integer weight", function () {
var vec3_a = new WorldWind.Vec3(2, 3, 4);
var vec3_b = new WorldWind.Vec3(4, 6, 8);
var expected_vec3 = new WorldWind.Vec3(12, 18, 24);
expect(vec3_a.mix(vec3_b, 5)).toEqual(expected_vec3);
});
it("Interpolates with a non integer weight", function () {
var vec3_a = new WorldWind.Vec3(2, 3, 4);
var vec3_b = new WorldWind.Vec3(4, 6, 8);
var expected_vec3 = new WorldWind.Vec3(3, 4.5, 6);
expect(vec3_a.mix(vec3_b, 0.5)).toEqual(expected_vec3);
});
});
it("Negates the components of the current vector", function () {
@ -183,25 +212,37 @@ define([
expect(vec3_a.magnitudeSquared()).toEqual(29);
});
it("Computes the magnitude of this vector", function () {
var vec3 = new WorldWind.Vec3(2, 4, 4);
expect(vec3.magnitude()).toEqual(6);
describe("#Magnitude of a vector", function () {
vec3 = new WorldWind.Vec3(3, -4, 0);
expect(vec3.magnitude()).toEqual(5);
it("Computes the magnitude with full positive components", function () {
var vec3 = new WorldWind.Vec3(2, 4, 4);
expect(vec3.magnitude()).toEqual(6);
});
it("Computes the magnitude with a negative components", function () {
var vec3 = new WorldWind.Vec3(3, -4, 0);
expect(vec3.magnitude()).toEqual(5);
});
});
it("Normalize this vector to a unit vector", function () {
var vec3 = new WorldWind.Vec3(2, 4, 4);
var expected_vec3 = new Vec3(1/3, 2/3, 2/3);
var expected_vec3 = new Vec3(1 / 3, 2 / 3, 2 / 3);
expect(vec3.normalize()).toEqual(expected_vec3);
});
it("Computes the squared distance from a vector to a specified vector", function () {
var vec3_a = new WorldWind.Vec3(2, 3, 4);
var vec3_b = new WorldWind.Vec3(4, 5, 6);
expect(vec3_a.distanceToSquared(vec3_b)).toEqual(12);
expect(vec3_a.distanceToSquared(vec3_a)).toEqual(0);
describe("#distanceToSquared", function () {
it("Squared distance from a vector to another one", function () {
var vec3_a = new WorldWind.Vec3(2, 3, 4);
var vec3_b = new WorldWind.Vec3(4, 5, 6);
expect(vec3_a.distanceToSquared(vec3_b)).toEqual(12);
});
it("Squared distance from a vector to itself", function () {
var vec3_a = new WorldWind.Vec3(2, 3, 4);
expect(vec3_a.distanceToSquared(vec3_a)).toEqual(0);
});
});
it("Computes the distance from a vector to a specified vector", function () {