mirror of
https://github.com/liabru/matter-js.git
synced 2025-03-14 00:38:41 -04:00
Vertices.translate unit tests; added lodash deepclone to test data
This commit is contained in:
parent
dd015b3e0a
commit
e1a59ff819
4 changed files with 143 additions and 47 deletions
1
package-lock.json
generated
1
package-lock.json
generated
|
@ -15,6 +15,7 @@
|
|||
"jest": "^29.7.0",
|
||||
"jest-worker": "^29.7.0",
|
||||
"json-stringify-pretty-compact": "^2.0.0",
|
||||
"lodash": "^4.17.21",
|
||||
"matter-tools": "^0.14.0",
|
||||
"matter-wrap": "^0.2.0",
|
||||
"mock-require": "^3.0.3",
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
"jest": "^29.7.0",
|
||||
"jest-worker": "^29.7.0",
|
||||
"json-stringify-pretty-compact": "^2.0.0",
|
||||
"lodash": "^4.17.21",
|
||||
"matter-tools": "^0.14.0",
|
||||
"matter-wrap": "^0.2.0",
|
||||
"mock-require": "^3.0.3",
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const Vector = require("../../src/geometry/Vector")
|
||||
const _ = require('lodash');
|
||||
|
||||
const testSquare = [
|
||||
{ x: 1., y: 1., },
|
||||
|
@ -150,22 +150,6 @@ const testBodyWithoutParts = {
|
|||
parts: [],
|
||||
};
|
||||
|
||||
const GetTestBodyPartsWithParent = () => {
|
||||
let parts = [];
|
||||
parts.push(testBodyWithoutParts);
|
||||
parts.push(...testBodyPartsWithoutParent);
|
||||
return parts;
|
||||
};
|
||||
|
||||
const testBodyPartsWithParent = GetTestBodyPartsWithParent();
|
||||
|
||||
const GetTestBodyWithParts = () => {
|
||||
const body = testBodyWithoutParts;
|
||||
body.parts.push(...testBodyPartsWithParent);
|
||||
return body;
|
||||
};
|
||||
|
||||
const testBodyWithParts = GetTestBodyWithParts();
|
||||
|
||||
const testVerticesSqaureWithoutBody = [
|
||||
{ x: 1., y: 1., index: 0, body: undefined, isInternal: false, },
|
||||
|
@ -188,13 +172,52 @@ const testVerticesAreaZeroWithoutBody = [
|
|||
{ x: 1., y: 3., index: 3, body: undefined, isInternal: false, },
|
||||
];
|
||||
|
||||
const getTestSquare = () => {
|
||||
return _.cloneDeep(testSquare);
|
||||
}
|
||||
|
||||
const getTestBodyPartsWithoutParent = () => {
|
||||
return _.cloneDeep(testBodyPartsWithoutParent);
|
||||
}
|
||||
|
||||
const getTestBodyWithoutParts = () => {
|
||||
return _cloneDeep(testBodyWithoutParts);
|
||||
}
|
||||
|
||||
const getTestBodyWithParts = () => {
|
||||
const body = testBodyWithoutParts;
|
||||
body.parts.push(...getTestBodyPartsWithoutParent());
|
||||
return _.cloneDeep(body);
|
||||
};
|
||||
|
||||
const getTestBodyPartsWithParent = () => {
|
||||
let parts = [];
|
||||
parts.push(testBodyWithoutParts);
|
||||
parts.push(...getTestBodyPartsWithoutParent);
|
||||
|
||||
return _.cloneDeep(parts);
|
||||
};
|
||||
|
||||
const getTestVerticesSqaureWithoutBody = () => {
|
||||
return _.cloneDeep(testVerticesSqaureWithoutBody);
|
||||
};
|
||||
|
||||
const getTestVerticesNegAreaWithoutBody = () => {
|
||||
return _.cloneDeep(testVerticesNegAreaWithoutBody);
|
||||
}
|
||||
|
||||
const getTestVerticesAreaZeroWithoutBody = () => {
|
||||
return _.cloneDeep(testVerticesAreaZeroWithoutBody);
|
||||
};
|
||||
|
||||
|
||||
module.exports = {
|
||||
testSquare,
|
||||
testVerticesSqaureWithoutBody,
|
||||
testVerticesAreaZeroWithoutBody,
|
||||
testVerticesNegAreaWithoutBody,
|
||||
testBodyPartsWithParent,
|
||||
testBodyPartsWithoutParent,
|
||||
testBodyWithParts,
|
||||
testBodyWithoutParts,
|
||||
getTestSquare,
|
||||
getTestBodyPartsWithoutParent,
|
||||
getTestBodyWithoutParts,
|
||||
getTestBodyWithParts,
|
||||
getTestBodyPartsWithParent,
|
||||
getTestVerticesSqaureWithoutBody,
|
||||
getTestVerticesNegAreaWithoutBody,
|
||||
getTestVerticesAreaZeroWithoutBody,
|
||||
};
|
|
@ -1,13 +1,18 @@
|
|||
const {assertFloat, assertXY} = require("../TestUtil");
|
||||
const {testSquare, testBodyWithParts, testVerticesSqaureWithoutBody, testVerticesAreaZeroWithoutBody, testVerticesNegAreaWithoutBody} = require("../TestData");
|
||||
const {
|
||||
getTestSquare,
|
||||
getTestBodyWithParts,
|
||||
getTestVerticesSqaureWithoutBody,
|
||||
getTestVerticesAreaZeroWithoutBody,
|
||||
getTestVerticesNegAreaWithoutBody } = require("../TestData");
|
||||
const Vector = require("../../../src/geometry/Vector");
|
||||
const Vertices = require("../../../src/geometry/Vertices");
|
||||
|
||||
describe('Vertices.create', () => {
|
||||
it('should be able to create the vertices with valid points and body', () => {
|
||||
// Arrange
|
||||
const points = testSquare;
|
||||
const body = testBodyWithParts;
|
||||
const points = getTestSquare();
|
||||
const body = getTestBodyWithParts();
|
||||
|
||||
// Act
|
||||
const result = Vertices.create(points, body);
|
||||
|
@ -35,7 +40,7 @@ describe('Vertices.create', () => {
|
|||
|
||||
it('should be able to create the vertices with valid points and undefined body', () => {
|
||||
// Arrange
|
||||
const points = testSquare;
|
||||
const points = getTestSquare();
|
||||
const body = undefined;
|
||||
|
||||
// Act
|
||||
|
@ -65,7 +70,7 @@ describe('Vertices.create', () => {
|
|||
it('should not be able to create the vertices with undefined points and valid body', () => {
|
||||
// Arrange
|
||||
const points = undefined;
|
||||
const body = testBodyWithParts;
|
||||
const body = getTestBodyWithParts();
|
||||
|
||||
// Act
|
||||
// TODO: This causes a read from undefined. This should probably be fixed.
|
||||
|
@ -80,7 +85,7 @@ describe('Vertices.fromPath', () => {
|
|||
it('should be able to create the vertices with valid path', () => {
|
||||
// Arrange
|
||||
const path = "1 2 L 3, 4 L 5 6";
|
||||
const body = testBodyWithParts;
|
||||
const body = getTestBodyWithParts();
|
||||
|
||||
// Act
|
||||
const result = Vertices.fromPath(path, body);
|
||||
|
@ -130,7 +135,7 @@ describe('Vertices.fromPath', () => {
|
|||
it('should (not?) be able to create the vertices with an invalid path', () => {
|
||||
// Arrange
|
||||
const path = "1 2 L123NND L 5 6";
|
||||
const body = testBodyWithParts;
|
||||
const body = getTestBodyWithParts();
|
||||
|
||||
// Act
|
||||
// TODO: This causes the result to have NaN y on the second Vector. This probaby should be fixed.
|
||||
|
@ -156,7 +161,7 @@ describe('Vertices.fromPath', () => {
|
|||
it('should not be able to create the vertices with an undefined path', () => {
|
||||
// Arrange
|
||||
const path = undefined;
|
||||
const body = testBodyWithParts;
|
||||
const body = getTestBodyWithParts();
|
||||
|
||||
// Act
|
||||
// TODO: This causes a read from undefined. This should probably be fixed.
|
||||
|
@ -171,7 +176,7 @@ describe('Vertices.fromPath', () => {
|
|||
describe('Vertices.area', () => {
|
||||
it('should be able to calulate the area with valid vertices and signed true', () => {
|
||||
// Arrange
|
||||
const vertices = testVerticesNegAreaWithoutBody;
|
||||
const vertices = getTestVerticesNegAreaWithoutBody();
|
||||
const signed = true;
|
||||
|
||||
// Act
|
||||
|
@ -184,7 +189,7 @@ describe('Vertices.area', () => {
|
|||
|
||||
it('should be able to calulate the area with valid vertices and signed false', () => {
|
||||
// Arrange
|
||||
const vertices = testVerticesNegAreaWithoutBody;
|
||||
const vertices = getTestVerticesNegAreaWithoutBody();
|
||||
const signed = false;
|
||||
|
||||
// Act
|
||||
|
@ -197,7 +202,7 @@ describe('Vertices.area', () => {
|
|||
|
||||
it('should be able to calulate the area with valid vertices and signed undefined', () => {
|
||||
// Arrange
|
||||
const vertices = testVerticesNegAreaWithoutBody;
|
||||
const vertices = getTestVerticesNegAreaWithoutBody();
|
||||
const signed = undefined;
|
||||
|
||||
// Act
|
||||
|
@ -210,7 +215,7 @@ describe('Vertices.area', () => {
|
|||
|
||||
it('should be able to calulate the area with valid vertices whose area add up to zero signed true', () => {
|
||||
// Arrange
|
||||
const vertices = testVerticesAreaZeroWithoutBody;
|
||||
const vertices = getTestVerticesAreaZeroWithoutBody();
|
||||
const signed = true;
|
||||
|
||||
// Act
|
||||
|
@ -223,7 +228,7 @@ describe('Vertices.area', () => {
|
|||
|
||||
it('should be able to calulate the area with valid vertices whose area add up to zero signed false', () => {
|
||||
// Arrange
|
||||
const vertices = testVerticesAreaZeroWithoutBody;
|
||||
const vertices = getTestVerticesAreaZeroWithoutBody();
|
||||
const signed = false;
|
||||
|
||||
// Act
|
||||
|
@ -252,7 +257,7 @@ describe('Vertices.area', () => {
|
|||
describe('Vertices.rotate', () => {
|
||||
it('should be able to rotate the vertices in place', () => {
|
||||
// Arrange
|
||||
const points = testVerticesNegAreaWithoutBody;
|
||||
const vertices = getTestVerticesSqaureWithoutBody();
|
||||
const angle = 37.;
|
||||
const point = Vector.create(42., 42.);
|
||||
|
||||
|
@ -269,7 +274,7 @@ describe('Vertices.rotate', () => {
|
|||
|
||||
it('should return undefined when rotating the vertices in place with angle set to zero', () => {
|
||||
// Arrange
|
||||
const vertices = testSquare;
|
||||
const vertices = getTestSquare();
|
||||
const angle = 0.;
|
||||
const point = Vector.create(42., 42.);
|
||||
|
||||
|
@ -283,7 +288,7 @@ describe('Vertices.rotate', () => {
|
|||
|
||||
it('should not be able to rotate the vertices in place with an undefined angle', () => {
|
||||
// Arrange
|
||||
const vertices = testSquare;
|
||||
const vertices = getTestSquare();
|
||||
const angle = undefined;
|
||||
const point = Vector.create(42., 42.);
|
||||
|
||||
|
@ -301,7 +306,7 @@ describe('Vertices.rotate', () => {
|
|||
|
||||
it('should not be able to rotate the vertices in place with undefined point', () => {
|
||||
// Arrange
|
||||
const vertices = testSquare;
|
||||
const vertices = getTestSquare();
|
||||
const angle = 37.;
|
||||
const point = undefined;
|
||||
|
||||
|
@ -333,7 +338,7 @@ describe('Vertices.rotate', () => {
|
|||
describe('Vertices.centre', () => {
|
||||
it('should be able to get the centre of the valid vertices', () => {
|
||||
// Arrange
|
||||
const vertices = testVerticesSqaureWithoutBody;
|
||||
const vertices = getTestVerticesSqaureWithoutBody();
|
||||
|
||||
// Act
|
||||
const result = Vertices.centre(vertices);
|
||||
|
@ -360,7 +365,7 @@ describe('Vertices.centre', () => {
|
|||
describe('Vertices.mean', () => {
|
||||
it('should be able to get the mean of the valid vertices', () => {
|
||||
// Arrange
|
||||
const vertices = testVerticesSqaureWithoutBody;
|
||||
const vertices = getTestVerticesSqaureWithoutBody();
|
||||
|
||||
// Act
|
||||
const result = Vertices.mean(vertices);
|
||||
|
@ -387,7 +392,7 @@ describe('Vertices.mean', () => {
|
|||
describe('Vertices.inertia', () => {
|
||||
it('should be able to get the inertia of the valid vertices', () => {
|
||||
// Arrange
|
||||
const vertices = testVerticesSqaureWithoutBody;
|
||||
const vertices = getTestVerticesSqaureWithoutBody();
|
||||
const mass = 37.3;
|
||||
|
||||
// Act
|
||||
|
@ -400,7 +405,7 @@ describe('Vertices.inertia', () => {
|
|||
|
||||
it('should be able to get the inertia of the valid vertices with negative mass', () => {
|
||||
// Arrange
|
||||
const vertices = testVerticesSqaureWithoutBody;
|
||||
const vertices = getTestVerticesSqaureWithoutBody();
|
||||
const mass = -37.3;
|
||||
|
||||
// Act
|
||||
|
@ -413,7 +418,7 @@ describe('Vertices.inertia', () => {
|
|||
|
||||
it('should not be able to get the inertia of the valid vertices with undefined mass', () => {
|
||||
// Arrange
|
||||
const vertices = testVerticesSqaureWithoutBody;
|
||||
const vertices = getTestVerticesSqaureWithoutBody();
|
||||
const mass = undefined;
|
||||
|
||||
// Act
|
||||
|
@ -439,5 +444,71 @@ describe('Vertices.inertia', () => {
|
|||
// TODO: This causes a read from undefined. This should probably be fixed.
|
||||
expect(result).toThrow("Cannot read properties of undefined (reading 'length')");
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe('Vertices.translate', () => {
|
||||
it('should be able to translate valid vertices', () => {
|
||||
// Arrange
|
||||
const vertices = getTestVerticesSqaureWithoutBody();
|
||||
const vector = { x: 42.2, y: 37.3 };
|
||||
const scalar = 97.8;
|
||||
|
||||
// Act
|
||||
const result = Vertices.translate(vertices, vector, scalar);
|
||||
|
||||
// Assert
|
||||
assertXY(result[0], 4128.16, 3648.9399999999996);
|
||||
assertXY(result[1], 4130.16, 3648.9399999999996);
|
||||
assertXY(result[2], 4130.16, 3650.9399999999996);
|
||||
assertXY(result[3], 4128.16, 3650.9399999999996);
|
||||
|
||||
});
|
||||
|
||||
it('should be able to translate valid vertices with undefined scalar', () => {
|
||||
// Arrange
|
||||
const vertices = getTestVerticesSqaureWithoutBody();
|
||||
const vector = { x: 42.2, y: 37.3 };
|
||||
const scalar = undefined;
|
||||
|
||||
// Act
|
||||
const result = Vertices.translate(vertices, vector, scalar);
|
||||
|
||||
// Assert
|
||||
assertXY(result[0], 43.2, 38.3);
|
||||
assertXY(result[1], 45.2, 38.3);
|
||||
assertXY(result[2], 45.2, 40.3);
|
||||
assertXY(result[3], 43.2, 40.3);
|
||||
|
||||
});
|
||||
|
||||
it('should not be able to translate valid vertices with undefined vector', () => {
|
||||
// Arrange
|
||||
const vertices = getTestVerticesSqaureWithoutBody();
|
||||
const vector = undefined;
|
||||
const scalar = undefined;
|
||||
|
||||
// Act
|
||||
const result = () => Vertices.translate(vertices, vector, scalar);
|
||||
|
||||
// Assert
|
||||
// TODO: This causes a read from undefined. This should probably be fixed.
|
||||
expect(result).toThrow("Cannot read properties of undefined (reading 'x')");
|
||||
|
||||
});
|
||||
|
||||
it('should not be able to translate undefined vertices', () => {
|
||||
// Arrange
|
||||
const vertices = undefined;
|
||||
const vector = { x: 42.2, y: 37.3 };
|
||||
const scalar = 97.8;
|
||||
|
||||
// Act
|
||||
const result = () => Vertices.translate(vertices, vector, scalar);
|
||||
|
||||
// Assert
|
||||
// TODO: This causes a read from undefined. This should probably be fixed.
|
||||
expect(result).toThrow("Cannot read properties of undefined (reading 'length')");
|
||||
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue