-
Notifications
You must be signed in to change notification settings - Fork 60
Utility Module Unit Tests [AARD-1932]
#1178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d69ebdf
Create EasingFunctions.test.ts
AlexD717 5aa8e77
Create Lazy.test.ts
AlexD717 880d4a1
Create MeshCreation.test.ts
AlexD717 78e10a4
DOMUnit Test
AlexD717 6f80d3e
DOMUnitExpression Test
AlexD717 d993c00
Units.test error fixed
AlexD717 bee29ba
Merge `dev`
BrandonPacewic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { test, expect, describe, vi } from "vitest" | ||
import { easeOutQuad } from "@/util/EasingFunctions" | ||
|
||
describe("Ease Out Quad Tests", () => { | ||
test("Whole Numbers", () => { | ||
expect(easeOutQuad(0)).toBeCloseTo(0) | ||
expect(easeOutQuad(1)).toBeCloseTo(1) | ||
}) | ||
|
||
test("Fractions", () => { | ||
expect(easeOutQuad(0.5)).toBeCloseTo(0.75) | ||
expect(easeOutQuad(0.25)).toBeCloseTo(0.4375) | ||
expect(easeOutQuad(0.75)).toBeCloseTo(0.9375) | ||
}) | ||
|
||
test("Within Range", () => { | ||
const steps = 100 | ||
for (let i = 0; i <= steps; i++) { | ||
const n = i / steps | ||
const result = easeOutQuad(n) | ||
expect(result).toBeGreaterThanOrEqual(0) | ||
expect(result).toBeLessThanOrEqual(1) | ||
} | ||
}) | ||
|
||
test("Out of Range Inputs", () => { | ||
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}) | ||
easeOutQuad(-0.1) | ||
easeOutQuad(1.1) | ||
easeOutQuad(2) | ||
easeOutQuad(-3) | ||
expect(consoleErrorSpy).toHaveBeenCalledTimes(4) | ||
consoleErrorSpy.mockRestore() | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { describe, test, expect, vi } from "vitest" | ||
import Lazy from "@/util/Lazy" | ||
|
||
describe("Lazy<T> Tests", () => { | ||
test("Should Call The Init Function Only Once", () => { | ||
const initFn = vi.fn(() => "computed value") | ||
const lazy = new Lazy(initFn) | ||
|
||
const val1 = lazy.getValue() | ||
const val2 = lazy.getValue() | ||
|
||
expect(val1).toBe("computed value") | ||
expect(val2).toBe("computed value") | ||
expect(initFn).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test("Should Not Initialize Until GetValue Is Called", () => { | ||
const initFn = vi.fn(() => "late init") | ||
const lazy = new Lazy(initFn) | ||
|
||
expect(initFn).not.toHaveBeenCalled() | ||
lazy.getValue() | ||
expect(initFn).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test("Should Work With Complex Types", () => { | ||
const initFn = () => ({ x: 1, y: 2 }) | ||
const lazy = new Lazy(initFn) | ||
|
||
const val = lazy.getValue() | ||
expect(val).toEqual({ x: 1, y: 2 }) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { test, describe, expect } from "vitest" | ||
import JOLT from "@/util/loading/JoltSyncLoader" | ||
import { createMeshForShape, DeltaFieldTransforms_PhysicalProp } from "@/util/threejs/MeshCreation" | ||
import { Vector3 } from "three" | ||
import * as THREE from "three" | ||
|
||
describe("Mesh Creation Tests", () => { | ||
test("Sphere Mesh Creation", () => { | ||
const sphereShape = new JOLT.SphereShape(4.0) | ||
const shapeResult = createMeshForShape(sphereShape) | ||
|
||
expect(shapeResult).toBeDefined() | ||
shapeResult.computeBoundingSphere() | ||
expect(shapeResult.boundingSphere?.radius).toBeCloseTo(4.0, 2) | ||
}) | ||
|
||
test("Box Mesh Creation", () => { | ||
const boxShape = new JOLT.BoxShape(new JOLT.Vec3(0.5, 2, 4.5)) | ||
const shapeResult = createMeshForShape(boxShape) | ||
|
||
expect(shapeResult).toBeDefined() | ||
shapeResult.computeBoundingBox() | ||
const boxSize = new Vector3() | ||
shapeResult.boundingBox?.getSize(boxSize) | ||
expect(boxSize.x).toBeCloseTo(1.0, 2) | ||
expect(boxSize.y).toBeCloseTo(4.0, 2) | ||
expect(boxSize.z).toBeCloseTo(9.0, 2) | ||
}) | ||
}) | ||
|
||
describe("Delta Field Transform Physical Properties Tests", () => { | ||
test("Returns Identity Transform When Both Matrices Are Identity", () => { | ||
const identity = new THREE.Matrix4() | ||
const result = DeltaFieldTransforms_PhysicalProp(identity, identity) | ||
|
||
expect(result.translation).toEqual(new THREE.Vector3(0, 0, 0)) | ||
expect(result.rotation).toEqual(new THREE.Quaternion(0, 0, 0, 1)) | ||
expect(result.scale).toEqual(new THREE.Vector3(1, 1, 1)) | ||
}) | ||
|
||
test("Applies Translation From Delta Only", () => { | ||
const delta = new THREE.Matrix4().makeTranslation(5, 0, 0) | ||
const field = new THREE.Matrix4() | ||
const result = DeltaFieldTransforms_PhysicalProp(delta, field) | ||
|
||
expect(result.translation).toEqual(new THREE.Vector3(5, 0, 0)) | ||
expect(result.rotation.equals(new THREE.Quaternion(0, 0, 0, 1))).toBe(true) | ||
expect(result.scale.equals(new THREE.Vector3(1, 1, 1))).toBe(true) | ||
}) | ||
|
||
test("Composes Rotation And Scale With Premultiply", () => { | ||
const delta = new THREE.Matrix4().makeRotationZ(Math.PI / 2) | ||
const field = new THREE.Matrix4().makeScale(2, 2, 2) | ||
const result = DeltaFieldTransforms_PhysicalProp(delta, field) | ||
|
||
expect(result.scale.x).toBeCloseTo(2) | ||
expect(result.scale.y).toBeCloseTo(2) | ||
expect(result.scale.z).toBeCloseTo(2) | ||
|
||
const expectedQuat = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 0, 1), Math.PI / 2) | ||
expect(result.rotation.angleTo(expectedQuat)).toBeLessThan(1e-6) | ||
}) | ||
|
||
test("Handles Scaled Translation", () => { | ||
const delta = new THREE.Matrix4().makeTranslation(1, 2, 3) | ||
const field = new THREE.Matrix4().makeScale(2, 2, 2) | ||
|
||
const result = DeltaFieldTransforms_PhysicalProp(delta, field) | ||
|
||
expect(result.translation).toEqual(new THREE.Vector3(2, 4, 6)) | ||
expect(result.scale).toEqual(new THREE.Vector3(2, 2, 2)) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { test, expect, describe, vi, beforeEach } from "vitest" | ||
import { DOMUnit, DOMUnitExpression } from "@/util/Units" | ||
|
||
vi.mock("@/util/Utility", () => { | ||
const original = vi.importActual<typeof import("@/util/Utility")>("@/util/Utility") | ||
return { ...original, getFontSize: vi.fn(() => 16) } | ||
}) | ||
|
||
describe("DOMUnit", () => { | ||
let mockElement: HTMLElement | ||
|
||
beforeEach(async () => { | ||
mockElement = document.createElement("div") | ||
Object.defineProperty(mockElement, "clientWidth", { value: 400 }) | ||
Object.defineProperty(mockElement, "clientHeight", { value: 200 }) | ||
}) | ||
|
||
test("Evaluates PX Units Correctly", () => { | ||
const unit = new DOMUnit(42, "px") | ||
expect(unit.evaluate(mockElement)).toBe(42) | ||
}) | ||
|
||
test("Evaluates REM Units Correctly", () => { | ||
const unit = new DOMUnit(2, "rem") | ||
expect(unit.evaluate(mockElement)).toBe(32) | ||
}) | ||
|
||
test("Evaluates Width (W) Units Correctly", () => { | ||
const unit = new DOMUnit(0.5, "w") | ||
expect(unit.evaluate(mockElement)).toBe(200) | ||
}) | ||
|
||
test("Evaluates Height (H) Units Correctly", () => { | ||
const unit = new DOMUnit(0.2, "h") | ||
expect(unit.evaluate(mockElement)).toBe(40) | ||
}) | ||
}) | ||
|
||
describe("DOMUnitExpression", () => { | ||
let mockElement: HTMLElement | ||
|
||
beforeEach(() => { | ||
mockElement = document.createElement("div") | ||
Object.defineProperty(mockElement, "clientWidth", { value: 400 }) | ||
Object.defineProperty(mockElement, "clientHeight", { value: 200 }) | ||
}) | ||
|
||
test("Evaluates Single Unit Expression Correctly", () => { | ||
const result = DOMUnitExpression.fromUnit(42, "px").evaluate(mockElement) | ||
expect(result).toBe(42) | ||
}) | ||
|
||
test("Evaluates Addition of Two PX Units Correctly", () => { | ||
const result = DOMUnitExpression.fromUnit(20, "px").add(DOMUnitExpression.fromUnit(22, "px")) | ||
expect(result.evaluate(mockElement)).toBe(42) | ||
}) | ||
|
||
test("Evaluates Addition of PX and REM Units Correctly", () => { | ||
const expr = DOMUnitExpression.fromUnit(16, "px").add(DOMUnitExpression.fromUnit(1, "rem")) | ||
expect(expr.evaluate(mockElement)).toBe(32) | ||
}) | ||
|
||
test("Evaluates Subtraction of PX Units Correctly", () => { | ||
const expr = DOMUnitExpression.fromUnit(50, "px").sub(DOMUnitExpression.fromUnit(8, "px")) | ||
expect(expr.evaluate(mockElement)).toBe(42) | ||
}) | ||
|
||
test("Evaluates Multiplication of PX Unit by Scalar", () => { | ||
const expr = DOMUnitExpression.fromUnit(7, "px").mul(DOMUnitExpression.fromUnit(6, "px")) | ||
expect(expr.evaluate(mockElement)).toBe(42) | ||
}) | ||
|
||
test("Evaluate Multiplication of PX Unit by Decimal", () => { | ||
const expr = DOMUnitExpression.fromUnit(84, "px").mul(DOMUnitExpression.fromUnit(0.5, "px")) | ||
expect(expr.evaluate(mockElement)).toBe(42) | ||
}) | ||
|
||
test("Evaluates Division of PX Unit by Scalar", () => { | ||
const expr = DOMUnitExpression.fromUnit(84, "px").div(DOMUnitExpression.fromUnit(2, "px")) | ||
expect(expr.evaluate(mockElement)).toBe(42) | ||
}) | ||
|
||
test("Evaluates Addition of Width (W) and Height (H) Units Correctly", () => { | ||
const expr = DOMUnitExpression.fromUnit(0.1, "w").add(DOMUnitExpression.fromUnit(0.11, "h")) | ||
// 0.1 * 400 + 0.11 * 200 = 40 + 22 = 62 | ||
expect(expr.evaluate(mockElement)).toBe(62) | ||
}) | ||
|
||
test("Evaluates Subtraction of Width (W) and Height (H) Units Correctly", () => { | ||
const expr = DOMUnitExpression.fromUnit(0.5, "w").sub(DOMUnitExpression.fromUnit(0.2, "h")) | ||
// 0.5 * 400 - 0.2 * 200 = 200 - 40 = 160 | ||
expect(expr.evaluate(mockElement)).toBe(160) | ||
}) | ||
|
||
test("Evaluates Subtraction of REM and PX Units Correctly", () => { | ||
const expr = DOMUnitExpression.fromUnit(3, "rem").sub(DOMUnitExpression.fromUnit(6, "px")) | ||
// 3 * 16 - 6 = 48 - 6 = 42 | ||
expect(expr.evaluate(mockElement)).toBe(42) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very helpful comments!