Skip to content

Commit

Permalink
Follow general format for test cases descriptions in vector utils. Re…
Browse files Browse the repository at this point in the history
…-export transform utils. Docs for toJSON method.
  • Loading branch information
Neloreck committed Jul 5, 2023
1 parent d1102c1 commit f89f106
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/engine/core/utils/transform/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "@/engine/core/utils/transform/decamelize";
export * from "@/engine/core/utils/transform/json";
7 changes: 7 additions & 0 deletions src/engine/core/utils/transform/json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
/**
* JSON.stringify alternative for lua data types.
*
* @param target - value to stringify as json
* @param separator - separator of each key-value pair after comma
* @param depth - current depth of json transformation
* @param maxDepth - maximal depth to transform, replace with `<depth_limit>` otherwise
* @param circular - registry of circular items references, return `<depth_limit>``` in case of circular references
* @returns stringified to json value
*/
export function toJSON(
target: unknown,
Expand Down
32 changes: 16 additions & 16 deletions src/engine/core/utils/vector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ import { Vector } from "@/engine/lib/types";
import { MockVector } from "@/fixtures/xray/mocks/vector.mock";

describe("'vector' utils", () => {
it("should correctly create empty vectors", () => {
it("'createEmptyVector' should correctly create empty vectors", () => {
expect(areSameVectors(new vector().set(0, 0, 0), createEmptyVector())).toBeTruthy();
expect(areSameVectors(new vector().set(1, 0, 0), createEmptyVector())).not.toBeTruthy();
});

it("should correctly create vectors", () => {
it("'createVector' should correctly create vectors", () => {
expect(areSameVectors(new vector().set(0, 0, 0), createVector(0, 0, 0))).toBeTruthy();
expect(areSameVectors(new vector().set(1, 0, 1), createVector(1, 0, 1))).toBeTruthy();
expect(areSameVectors(new vector().set(55, -5, 25), createVector(55, -5, 25))).toBeTruthy();
});

it("should correctly add vectors", () => {
it("'addVectors' should correctly add vectors", () => {
const first: Vector = createVector(1, 2, 4);
const second: Vector = createVector(3, 6, 9);
const result: Vector = addVectors(first, second);
Expand All @@ -44,7 +44,7 @@ describe("'vector' utils", () => {
expect(second).not.toBe(result);
});

it("should correctly sub vectors", () => {
it("'subVectors' should correctly sub vectors", () => {
const first: Vector = createVector(25, 50, 100);
const second: Vector = createVector(12, 15, 25);
const result: Vector = subVectors(first, second);
Expand All @@ -54,7 +54,7 @@ describe("'vector' utils", () => {
expect(second).not.toBe(result);
});

it("should correctly copy vectors", () => {
it("'copyVector' should correctly copy vectors", () => {
const first: Vector = createVector(1, 2, 3);
const second: Vector = createVector(-1, -2, -3);

Expand All @@ -68,15 +68,15 @@ describe("'vector' utils", () => {
expect(copy.z).toBe(-3);
});

it("should correctly compare same vectors by value", () => {
it("'areSameVectors' should correctly compare same vectors by value", () => {
expect(areSameVectors(createEmptyVector(), createEmptyVector())).toBeTruthy();
expect(areSameVectors(MockVector.mock(1, 2, 3), MockVector.mock(1, 2, 3))).toBeTruthy();
expect(areSameVectors(MockVector.mock(1, 2, 3), createEmptyVector())).toBeFalsy();
// Not precision based.
expect(areSameVectors(MockVector.mock(1, 1, 0.3), MockVector.mock(1, 1, 0.2 + 0.1))).toBeFalsy();
});

it("should correctly compare same vectors by precision rate", () => {
it("'areSameVectorsByPrecision' should correctly compare same vectors by precision rate", () => {
expect(areSameVectorsByPrecision(createEmptyVector(), createEmptyVector(), 0.001)).toBeTruthy();
expect(areSameVectorsByPrecision(MockVector.mock(1, 2, 3), MockVector.mock(1, 2, 3), 0.001)).toBeTruthy();
expect(areSameVectorsByPrecision(MockVector.mock(1, 2, 3), createEmptyVector(), 0.001)).toBeFalsy();
Expand All @@ -85,7 +85,7 @@ describe("'vector' utils", () => {
expect(areSameVectorsByPrecision(MockVector.mock(1, 1, 0.3), MockVector.mock(1, 1, 0.2 + 0.1), 0.001)).toBeTruthy();
});

it("should correctly check 2d distance", () => {
it("'distanceBetween2d' should correctly check 2d distance", () => {
expect(distanceBetween2d(createEmptyVector(), createEmptyVector())).toBe(0);
expect(distanceBetween2d(MockVector.mock(1, 1, 1), MockVector.mock(-1, -1, -1))).toBe(2.8284271247461903);
expect(distanceBetween2d(MockVector.mock(1, 1, 0), MockVector.mock(-1, -1, -1))).toBe(2.23606797749979);
Expand All @@ -95,7 +95,7 @@ describe("'vector' utils", () => {
expect(distanceBetween2d(MockVector.mock(1000, 1, 0), MockVector.mock(0, -1, 0))).toBe(1000);
});

it("should correctly calculate yaw", () => {
it("'yaw' should correctly calculate yaw", () => {
expect(yaw(MockVector.mock(1, 0.25, 0.33), MockVector.mock(0.2, 0.2, 0.1))).toBe(0.1449000485801625);
expect(yaw(MockVector.mock(0.5, 0.25, 0.1), MockVector.mock(0.05, 0.1, 0.8))).toBe(1.3109819569490586);
expect(yaw(MockVector.mock(0.1, 0.1, 0.1), MockVector.mock(0.9, 0.9, 0.9))).toBe(0);
Expand All @@ -106,7 +106,7 @@ describe("'vector' utils", () => {
expect(yaw(MockVector.mock(0, 0, 0), MockVector.mock(0, 0, 0))).toBeNaN();
});

it("should correctly calculate yaw degree", () => {
it("'yawDegree' should correctly calculate yaw degree", () => {
expect(yawDegree(MockVector.mock(1, 0.25, 0.33), MockVector.mock(0.2, 0.2, 0.1))).toBe(8.302149713434416);
expect(yawDegree(MockVector.mock(0.5, 0.25, 0.1), MockVector.mock(0.05, 0.1, 0.8))).toBe(75.11362891076617);
expect(yawDegree(MockVector.mock(0.1, 0.1, 0.1), MockVector.mock(0.9, 0.9, 0.9))).toBe(0);
Expand All @@ -117,7 +117,7 @@ describe("'vector' utils", () => {
expect(yawDegree(MockVector.mock(0, 0, 0), MockVector.mock(0, 0, 0))).toBeNaN();
});

it("should correctly calculate yaw degree 3d", () => {
it("'yawDegree3d' should correctly calculate yaw degree 3d", () => {
expect(yawDegree3d(MockVector.mock(1, 0.25, 0.33), MockVector.mock(0.2, 0.2, 0.1))).toBe(29.355944608609963);
expect(yawDegree3d(MockVector.mock(0.5, 0.25, 0.1), MockVector.mock(0.05, 0.1, 0.8))).toBe(73.53711801923248);
expect(yawDegree3d(MockVector.mock(0.1, 0.1, 0.1), MockVector.mock(0.9, 0.9, 0.9))).toBe(0);
Expand All @@ -128,7 +128,7 @@ describe("'vector' utils", () => {
expect(yawDegree3d(MockVector.mock(0, 0, 0), MockVector.mock(0, 0, 0))).toBeNaN();
});

it("should correctly calculate cross multiplication", () => {
it("'vectorCross' should correctly calculate cross multiplication", () => {
expect(vectorCross(MockVector.mock(1, -0.1, 0.25), MockVector.mock(0.2, -1, -1))).toEqual(
MockVector.create(0.35, 1.05, -0.98)
);
Expand All @@ -146,7 +146,7 @@ describe("'vector' utils", () => {
expect(vectorCross(MockVector.mock(0, 0, 0), MockVector.mock(0, 0, 0))).toEqual(MockVector.create(0, 0, 0));
});

it("should correctly rotate y", () => {
it("'vectorRotateY' should correctly rotate y", () => {
expect(vectorRotateY(MockVector.mock(1, 0.5, 0.25), 45)).toEqual(
MockVector.create(0.5303300858899107, 0.5, 0.8838834764831843)
);
Expand All @@ -155,21 +155,21 @@ describe("'vector' utils", () => {
);
});

it("radianToDegree should correctly convert", () => {
it("'radianToDegree' should correctly convert", () => {
expect(radianToDegree(1)).toBe(57.29577951308232);
expect(radianToDegree(1000)).toBe(57295.77951308232);
expect(radianToDegree(math.pi)).toBe(180);
expect(radianToDegree(math.pi * 5)).toBe(900);
});

it("degreeToRadian should correctly convert", () => {
it("'degreeToRadian' should correctly convert", () => {
expect(degreeToRadian(57.29577951308232)).toBe(1);
expect(degreeToRadian(57295.77951308232)).toBe(1000);
expect(degreeToRadian(180)).toBe(math.pi);
expect(degreeToRadian(900)).toBe(math.pi * 5);
});

it("vectorToString should correctly transform to string", () => {
it("'vectorToString' should correctly transform to string", () => {
expect(vectorToString(null)).toBe("nil");
expect(vectorToString(createEmptyVector())).toBe("[0:0:0]");
expect(vectorToString(createVector(1, 2, 3))).toBe("[1:2:3]");
Expand Down

0 comments on commit f89f106

Please sign in to comment.