Skip to content

Commit

Permalink
Coverage improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ib Green committed Jun 12, 2019
1 parent ebb355d commit d300565
Show file tree
Hide file tree
Showing 13 changed files with 570 additions and 447 deletions.
2 changes: 1 addition & 1 deletion modules/core/src/addons/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class Polygon {
for (let i = 0; i < length - 1; i++) {
visitor(this.points[i], this.points[i + 1], i, i + 1);
}
if (this.isPolygon && !this.isClosed()) {
if (!this.isClosed) {
// Call function with points and indices
visitor(this.points[length - 1], this.points[0], length - 1, 0);
}
Expand Down
14 changes: 0 additions & 14 deletions modules/core/src/classes/base/math-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,6 @@ export default class MathArray extends Array {
return this.check();
}

inverse() {
for (let i = 0; i < this.ELEMENTS; ++i) {
this[i] = 1 / this[i];
}
return this.check();
}

lerp(a, b, t) {
if (t === undefined) {
t = b;
Expand Down Expand Up @@ -172,13 +165,6 @@ export default class MathArray extends Array {
return this.check();
}

scaleAndAdd(vector, scale) {
for (let i = 0; i < this.ELEMENTS; ++i) {
this[i] = this[i] * scale + vector[i];
}
return this.check();
}

// three.js compatibility

sub(a) {
Expand Down
13 changes: 1 addition & 12 deletions modules/core/src/classes/base/matrix.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MathArray from './math-array';
import {checkNumber, deprecated} from '../../lib/validators';
import {checkNumber} from '../../lib/validators';
import {config} from '../../lib/common';

export default class Matrix extends MathArray {
Expand Down Expand Up @@ -63,15 +63,4 @@ export default class Matrix extends MathArray {
}
return this;
}

// three.js compatibility

multiplyMatrices(a, b) {
return this.copy(a).multiplyRight(b);
}

setColumnMajor() {
deprecated('Matrix.setColumnMajor', '3.0');
return this.set(arguments);
}
}
5 changes: 0 additions & 5 deletions modules/core/src/classes/base/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,4 @@ export default class Vector extends MathArray {
addScaledVector(a, b) {
return this.add(new this.constructor(a).multiplyScalar(b));
}

operation(operation, ...args) {
operation(this, this, ...args);
return this.check();
}
}
2 changes: 1 addition & 1 deletion modules/core/src/classes/pose.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class Pose {
* pose's position and with with the defining pose's orientation
* aligned with axis.
*/
constructor({x = 0, y = 0, z = 0, roll = 0, pitch = 0, yaw = 0, position, orientation}) {
constructor({x = 0, y = 0, z = 0, roll = 0, pitch = 0, yaw = 0, position, orientation} = {}) {
if (Array.isArray(position) && position.length === 3) {
this.position = new Vector3(position);
} else {
Expand Down
25 changes: 22 additions & 3 deletions modules/core/test/addons/polygon.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,22 @@ const TEST_CASES = [
title: 'non-closed poly',
polygon: [[5, 0], [6, 4], [4, 5], [1, 5], [1, 0]],
area: 22,
sign: -1
sign: -1,
segments: 5
},
{
title: 'exactly closed poly',
polygon: [[5, 0], [6, 4], [4, 5], [1, 5], [1, 0], [5, 0]],
area: 22,
sign: -1
sign: -1,
segments: 5
},
{
title: 'EPSILON closed poly',
polygon: [[5, 0], [6, 4], [4, 5], [1, 5], [1, 0], [5, 0.0000001]],
area: 22,
sign: -1
sign: -1,
segments: 5
}
];

Expand Down Expand Up @@ -79,3 +82,19 @@ test('Polygon#methods', t => {
configure({EPSILON: 1e-12});
t.end();
});

test('Polygon#forEachSegment', t => {
const config = configure({EPSILON: 1e-4});

for (const tc of TEST_CASES) {
const polygon = new Polygon(tc.polygon);
let count = 0;
polygon.forEachSegment(() => {
count++;
});
t.equals(count, tc.segments, 'forEachSegment() iterated over all virtual segments');
}

configure(config);
t.end();
});
19 changes: 17 additions & 2 deletions modules/core/test/classes/matrix4.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ test('Matrix4#from', t => {
t.end();
});

test.skip('Matrix4#to', t => {
test('Matrix4#to', t => {
const matrix = new Matrix4(...INDICES_MATRIX);
tapeEquals(t, matrix.to([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), INDICES_MATRIX);
t.ok(matrix.to(matrix), 'Handles copy to self');
// tapeEquals(t, matrix.to([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), INDICES_MATRIX);
// t.deepEquals(matrix.to({x: 0, y: 0, z: 0, w: 0}), {x: 1, y: 2, z: 4});
t.end();
});
Expand All @@ -84,6 +85,20 @@ test('Matrix4.toFloat32Array', t => {
t.end();
});

test('Matrix4.equals', t => {
const m = new Matrix4();
t.ok(m.equals(IDENTITY_MATRIX));
t.notOk(m.equals([...IDENTITY_MATRIX, 0]));
t.end();
});

test('Matrix4.exactEquals', t => {
const m = new Matrix4();
t.ok(m.exactEquals(IDENTITY_MATRIX));
t.notOk(m.exactEquals([...IDENTITY_MATRIX, 0]));
t.end();
});

test('Matrix4#identity (identity matrix)', t => {
t.equals(typeof Matrix4.prototype.identity, 'function');
const m = new Matrix4();
Expand Down
44 changes: 29 additions & 15 deletions modules/core/test/classes/spherical-coordinates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,9 @@
// THE SOFTWARE.

/* eslint-disable max-statements, max-depth */
import {_SphericalCoordinates as SphericalCoordinates} from 'math.gl';
import test from 'tape-catch';

// FOR TAPE TESTING
// Use tape assert to compares using a.equals(b)
// Usage test(..., t => { tapeEquals(t, a, b, ...); });
export function tapeEquals(t, a, b, msg, extra) {
/* eslint-disable no-invalid-this */
t._assert(a.equals(b), {
message: msg || 'should be equal',
operator: 'equal',
actual: a,
expected: b,
extra
});
}
import {tapeEquals} from 'test/utils/tape-assertions';
import {_SphericalCoordinates as SphericalCoordinates} from 'math.gl';

const REPRESENTATION_TEST_CASES = [
{
Expand Down Expand Up @@ -103,3 +90,30 @@ test('SphericalCoordinates#representations', t => {
}
t.end();
});

test('SphericalCoordinates#accessors', t => {
const spherical = new SphericalCoordinates();
t.equals(spherical.bearing, 180, 'bearing');
t.equals(spherical.pitch, 0, 'pitch');
// t.equals(spherical.altitude, 0, 'altitude');
t.equals(spherical.longitude, 0, 'longitude');
t.equals(spherical.latitude, 0, 'latitude');
t.equals(spherical.lng, 0, 'lng');
t.equals(spherical.lat, 0, 'lat');
t.equals(spherical.z, 0, 'z');
t.end();
});

test('SphericalCoordinates#clone', t => {
const spherical = new SphericalCoordinates();
const s2 = spherical.clone();
t.notEqual(spherical, s2, 'clone');
tapeEquals(t, spherical, s2, 'clone');
t.end();
});

test('SphericalCoordinates#makeSafe', t => {
const spherical = new SphericalCoordinates();
t.doesNotThrow(() => spherical.makeSafe());
t.end();
});
12 changes: 12 additions & 0 deletions modules/core/test/classes/vector2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@ test('Vector2#transform', t => {
t.end();
});

test('Vector2#transformAsVector', t => {
const transform = new Matrix4().scale([0.5, 0.5, 0.5]).translate([1, 1, 1]);

const TEST_CASES = [{input: [0, 0], result: [0, 0]}, {input: [1, 0], result: [0.5, 0]}];
for (const testCase of TEST_CASES) {
const v = new Vector2(...testCase.input);
const result = v.transformAsVector(transform);
tapeEquals(t, result, testCase.result);
}
t.end();
});

test('Vector2#transformByMatrix3', t => {
const transform = new Matrix3().scale([0.5, 0.5, 0.5]).translate([1, 1, 1]);

Expand Down
9 changes: 9 additions & 0 deletions modules/core/test/classes/vector3.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ test('Vector3#dot', t => {
t.end();
});

test('Vector3#angle', t => {
const TEST_CASES = [{input: [0, 1, 0], result: Math.PI / 2}];
for (const tc of TEST_CASES) {
const result = new Vector3([1, 0, 0]).angle(tc.input);
tapeEquals(t, result, tc.result);
}
t.end();
});

test('Vector3#normalize', t => {
const TEST_CASES = [
{input: [0, 0, 0], result: [0, 0, 0]},
Expand Down
43 changes: 42 additions & 1 deletion modules/core/test/lib/common.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import {Vector2, Vector3} from 'math.gl';
import {Vector2, Vector3, _Pose as Pose, _MathUtils} from 'math.gl';
import {config, configure, isArray, clone, equals, exactEquals, formatValue} from 'math.gl';
import {toRadians, toDegrees} from 'math.gl';
import {radians, degrees, sin, cos, tan, asin, acos, atan, clamp, lerp} from 'math.gl';
Expand All @@ -30,6 +30,7 @@ test('math.gl#types', t => {
t.equals(typeof radians, 'function');
t.equals(typeof equals, 'function');
t.equals(typeof config.EPSILON, 'number');
t.ok(_MathUtils);
t.end();
});

Expand Down Expand Up @@ -96,6 +97,22 @@ test('math.gl#equals', t => {
equals(new Vector2([1.0, 2.0]), [1.0, 2.0]),
'should return true for Array and Vector2 with same values'
);
t.notOk(
equals([1.0, 2.0], [1.0, 2.0, 3.0]),
'should return false for Arrays of different lengths'
);
t.ok(
equals(new Vector2([1.0, 2.0]), [1.0, 2.0]),
'should return true for Arrays of different types'
);
t.notOk(
equals([1.0, 2.0], new Pose()),
'should return false for incompatible objects w equals method'
);
t.notOk(
equals(new Pose(), [1.0, 2.0]),
'should return false for incompatible objects w equals method'
);
t.end();
});

Expand All @@ -119,6 +136,30 @@ test('math.gl#exactEquals', t => {
exactEquals([1.0, 2.0], new Vector2([1.0, 2.0])),
'should return false for Array and Vector2 with same values'
);
t.notOk(
exactEquals([1.0, 2.0], [1.0, 2.0, 3.0]),
'should return false for Arrays of different lengths'
);
t.notOk(
exactEquals(new Pose(), [1.0, 2.0]),
'should return false for incompatible objects w equals method'
);
t.notOk(
exactEquals([1.0, 2.0], new Pose()),
'should return false for incompatible objects w equals method'
);
t.notOk(
exactEquals(new Pose(), new Pose({x: 1})),
'should return false for different compatible objects w equals method'
);
t.notOk(
exactEquals(new Pose({x: 1}), new Pose()),
'should return false for different compatible objects w equals method'
);
t.notOk(
exactEquals([new Pose({x: 1})], [new Pose()]),
'should return false for arrays of different compatible objects w equals method'
);
t.end();
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"bootstrap": "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true yarn && ocular-bootstrap",
"clean": "ocular-clean",
"build": "ocular-build",
"cover": "ocular-test cover",
"cover": "yarn clean ; rm -fr coverage/* ; ocular-test cover",
"lint": "ocular-lint",
"metrics": "ocular-metrics",
"publish": "ocular-publish",
Expand Down

0 comments on commit d300565

Please sign in to comment.