Skip to content

Commit

Permalink
tests: write asserts for dpad
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Feb 4, 2024
1 parent 71b8b7d commit 146d58c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
35 changes: 27 additions & 8 deletions tests/draw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { draw, drawCircle } from "../src/draw"

const drawJoyMock = (o: any) => draw['Joy'](o)
const drawBtnMock = (o: any) => draw['Btn'](o)
const drawDpadMock = (o: any) => draw['Dpad'](o)
const drawCircleMock = (a: any, b: any, c: any, d: any, e: any) => drawCircle(a, b, e, {x: c, y: d})

test("drawCircle should draw a filled circle with the correct attributes", () => {
Expand All @@ -17,8 +18,7 @@ test("drawCircle should draw a filled circle with the correct attributes", () =>

drawCircleMock(ctxMock, "blue", 50, 50, 10)
expect(ctxMock.beginPath).toHaveBeenCalled()
expect(ctxMock.arc).toHaveBeenCalled()// TODO: replace
//expect(ctxMock.arc).toHaveBeenCalledWith(50, 50, 10, 0, 2 * Math.PI)
expect(ctxMock.arc).toHaveBeenCalledWith(50, 50, 10, 0, 2 * Math.PI)
expect(ctxMock.fillStyle).toBe("blue")
expect(ctxMock.fill).toHaveBeenCalled()
expect(ctxMock.stroke).toHaveBeenCalled()
Expand All @@ -42,8 +42,8 @@ test("draw.Btn should draw a red circle when stateNew is true", () => {

drawBtnMock(gpz)
expect(gpz.ctx2d.fillStyle).toBe("red")
expect(gpz.ctx2d.clearRect).toHaveBeenCalled() // TODO: replace
//expect(gpz.ctx2d.clearRect).toHaveBeenCalledWith(0, 0, 100, 100)
expect(gpz.ctx2d.clearRect).toHaveBeenCalled()
expect(gpz.ctx2d.clearRect).toHaveBeenCalledWith(0, 0, 100, 100)
})


Expand All @@ -64,8 +64,7 @@ test("draw.Btn should draw a gray circle when stateNew is false", () => {

drawBtnMock(gpz)
expect(gpz.ctx2d.fillStyle).not.toBe("red")
expect(gpz.ctx2d.clearRect).toHaveBeenCalled() // TODO: replace
//expect(gpz.ctx2d.clearRect).toHaveBeenCalledWith(0, 0, 100, 100)
expect(gpz.ctx2d.clearRect).toHaveBeenCalledWith(0, 0, 100, 100)
})

test("draw.Joy should draw circles with the correct colors and positions", () => {
Expand All @@ -84,6 +83,26 @@ test("draw.Joy should draw circles with the correct colors and positions", () =>
}

drawJoyMock(gpz)
expect(gpz.ctx2d.clearRect).toHaveBeenCalled() // TODO: replace
//expect(gpz.ctx2d.clearRect).toHaveBeenCalledWith(0, 0, 100, 100)
expect(gpz.ctx2d.clearRect).toHaveBeenCalledWith(0, 0, 100, 100)
})

test("Dpad should draw correctly based on stateNew", () => {
const gpz = {
canvas: { width: 100, height: 100, dataset: { gpzSize: '16', color: '#aaaaaa80' } },
stateNew: [false, false, true, false],
ctx2d: {
fillStyle: "",
beginPath: jest.fn(),
moveTo: jest.fn(),
lineTo: jest.fn(),
fill: jest.fn(),
stroke: jest.fn(),
closePath: jest.fn(),
clearRect: jest.fn()
},
}

drawDpadMock(gpz)

expect(gpz.ctx2d.clearRect).toHaveBeenCalledWith(0, 0, 100, 100)
})
30 changes: 29 additions & 1 deletion tests/math.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect, test } from "bun:test";
import { expect, test } from "bun:test"
import { angleInRadians, polyTransform } from "../src/math"
import { interpolation, clamp, normalize, desnormalize } from '../src/util'

test('should interpolate correctly', () => {
Expand All @@ -23,3 +24,30 @@ test('should desnormalize values correctly', () => {
expect(desnormalize(0)).toBe(0.5)
expect(desnormalize(1)).toBe(1)
})

test("converts degrees to radians", () => {
expect(angleInRadians(0)).toBe(0)
expect(angleInRadians(90)).toBeCloseTo(Math.PI / 2)
expect(angleInRadians(180)).toBeCloseTo(Math.PI)
expect(angleInRadians(360)).toBeCloseTo(2 * Math.PI)
})

test("transforms a polygon correctly", () => {
const polygon = [
{ x: -1, y: -1 },
{ x: -1, y: 1 },
{ x: 1, y: -1 },
{ x: 1, y: 1 },
]

const offset = { x: 10, y: 10 }
const size = { x: 2, y: 2 }
const angle = Math.PI / 2

const transformedPoly = polyTransform(polygon, offset, size, angle)

expect(transformedPoly[0]).toEqual({x: 12, y: 8})
expect(transformedPoly[1]).toEqual({x: 8, y: 8})
expect(transformedPoly[2]).toEqual({x: 12, y: 12})
expect(transformedPoly[3]).toEqual({x: 8, y: 12})
})

0 comments on commit 146d58c

Please sign in to comment.