0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2025-03-14 00:38:41 -04:00

Body.setVertices unit tests

This commit is contained in:
gsenden 2024-05-27 08:39:49 +02:00
parent 87ed85d11f
commit e299ed14a8
2 changed files with 75 additions and 6 deletions

View file

@ -1,19 +1,30 @@
const assertFloat = (result, expected) => {
expect(Math.round(result * 100000),'Assert float\n\tExpected: ' + expected + '\n\tReceived: ' + result +'\n\nIn order to test float values, the values have been multiplied by 100000 in official result below.').toEqual(Math.round(expected * 100000));
}
expect(Math.round(result * 100000), 'Assert float\n\tExpected: ' + expected + '\n\tReceived: ' + result + '\n\nIn order to test float values, the values have been multiplied by 100000 in official result below.').toEqual(Math.round(expected * 100000));
};
const assertXY = (result, expectedX, expectedY) => {
assertFloat(result.x, expectedX);
assertFloat(result.y, expectedY);
}
};
const assertBounds = (result, expectedMinX, expectedMinY, expectedMaxX, expectedMaxY) => {
assertFloat(result.min.x, expectedMinX);
assertFloat(result.min.y, expectedMinY);
assertFloat(result.max.x, expectedMaxX);
assertFloat(result.max.y, expectedMaxY);
}
};
const assertVertex = (result, expextedId, expectedX, expectedY, expectedIndex, expectedIsInternal) => {
expect(result.body.id).toEqual(expextedId);
assertFloat(result.x, expectedX);
assertFloat(result.y, expectedY);
expect(result.index).toEqual(expectedIndex);
expect(result.isInternal).toEqual(expectedIsInternal);
};
module.exports = {
assertFloat, assertXY, assertBounds
assertFloat,
assertXY,
assertBounds,
assertVertex,
};

View file

@ -1,8 +1,9 @@
const {assertFloat, assertXY, assertBounds} = require("../TestUtil");
const {assertFloat, assertXY, assertBounds, assertVertex} = require("../TestUtil");
const {
getTestBodyWithoutParts,
getTestBodyPartsWithoutParent,
getTestBodyWithPartsWithParent,
getTestSquare,
} = require("../TestData");
const Body = require("../../../src/body/Body");
@ -774,5 +775,62 @@ describe("Body.setInertia()", () => {
// Assert
// TODO: This causes a read or set from undefined. This should probably be fixed.
expect(result).toThrow(/^Cannot .* properties of undefined \(.* '.*'\)$/);
});
});
describe('Body.setVertices', () => {
it('should mutate the body to contain valid values', () => {
// Arrange
const vertices = getTestSquare();
const body = getTestBodyWithPartsWithParent();
// Act
Body.setVertices(body, vertices)
// Assert
assertFloat(body.area, 4.0);
assertXY(body.axes[0], 0.0, 1.0);
assertXY(body.axes[1], -1.0, 0.0);
assertBounds(body.bounds, 138., 139., 299., 301.);
assertFloat(body.density, 126.);
assertFloat(body.inertia, 1344.);
assertFloat(body.inverseInertia, 0.000744047619047619);
assertFloat(body.inverseMass, 0.001984126984126984);
assertFloat(body.mass, 504.);
assertXY(body.position, 139., 140.);
assertXY(body.velocity, 159., 160.);
assertVertex(body.vertices[0], body.id, 138., 139., 0, false);
assertVertex(body.vertices[1], body.id, 140., 139., 1, false);
assertVertex(body.vertices[2], body.id, 140., 141., 2, false);
assertVertex(body.vertices[3], body.id, 138., 141., 3, false);
});
it('should not be able mutate the body to contain valid values with undefined vertices', () => {
// Arrange
const vertices = undefined;
const body = getTestBodyWithPartsWithParent();
// Act
let result = () => Body.setVertices(body, vertices)
// Assert
// TODO: This causes a read or set from undefined. This should probably be fixed.
expect(result).toThrow(/^Cannot .* properties of undefined \(.* '.*'\)$/);
});
it('should not be able mutate the body to contain valid values on undefined body', () => {
// Arrange
const vertices = getTestSquare();
const body = undefined;
// Act
let result = () => Body.setVertices(body, vertices)
// Assert
// TODO: This causes a read or set from undefined. This should probably be fixed.
expect(result).toThrow(/^Cannot .* properties of undefined \(.* '.*'\)$/);
});
});