From c770e54f9192245672e86a22734f3fcd028fdab7 Mon Sep 17 00:00:00 2001 From: James Chen Date: Thu, 19 Dec 2024 10:55:04 +0800 Subject: [PATCH] Fix toString and update test case. --- cocos/core/math/mat3.ts | 4 ++-- cocos/core/math/mat4.ts | 9 +++++---- tests/core/math/mat3.test.ts | 16 ++++++++++++++++ tests/core/math/mat4.test.ts | 16 ++++++++++++++++ tests/core/math/rect.test.ts | 2 +- tests/core/math/size.test.ts | 2 +- tests/core/math/vec2.test.ts | 5 +++++ tests/core/math/vec3.test.ts | 5 +++++ tests/core/math/vec4.test.ts | 5 +++++ 9 files changed, 56 insertions(+), 8 deletions(-) diff --git a/cocos/core/math/mat3.ts b/cocos/core/math/mat3.ts index 6c5101129a4..ad588c2ca9c 100644 --- a/cocos/core/math/mat3.ts +++ b/cocos/core/math/mat3.ts @@ -833,8 +833,8 @@ export class Mat3 extends ValueType { const t = this; return `[\n${ t.m00}, ${t.m01}, ${t.m02},\n${ - t.m03},\n${t.m04}, ${t.m05},\n${ - t.m06}, ${t.m07},\n${t.m08}\n` + t.m03}, ${t.m04}, ${t.m05},\n${ + t.m06}, ${t.m07}, ${t.m08}\n` + `]`; } diff --git a/cocos/core/math/mat4.ts b/cocos/core/math/mat4.ts index 266e8990045..9e2da778df8 100644 --- a/cocos/core/math/mat4.ts +++ b/cocos/core/math/mat4.ts @@ -1825,11 +1825,12 @@ export class Mat4 extends ValueType { * @return 当前矩阵的字符串表示。 */ public toString (): string { + const t = this; return `[\n${ - this.m00}, ${this.m01}, ${this.m02}, ${this.m03},\n${ - this.m04}, ${this.m05}, ${this.m06}, ${this.m07},\n${ - this.m08}, ${this.m09}, ${this.m10}, ${this.m11},\n${ - this.m12}, ${this.m13}, ${this.m14}, ${this.m15}\n` + t.m00}, ${t.m01}, ${t.m02}, ${t.m03},\n${ + t.m04}, ${t.m05}, ${t.m06}, ${t.m07},\n${ + t.m08}, ${t.m09}, ${t.m10}, ${t.m11},\n${ + t.m12}, ${t.m13}, ${t.m14}, ${t.m15}\n` + ']'; } diff --git a/tests/core/math/mat3.test.ts b/tests/core/math/mat3.test.ts index a2bf16c0e50..e5e302519d5 100644 --- a/tests/core/math/mat3.test.ts +++ b/tests/core/math/mat3.test.ts @@ -138,4 +138,20 @@ describe('Test Mat3', () => { log('toEuler: ', v0); expect(Vec3.equals(v0, expect0)).toBe(true); }); + + test('toString', () => { + const m = new Mat3( +12.1234, 0, 44.123433, +55.12234, 6.1230835, 12384.126, +7348.12237, 1283.8, 0.000009 +); + const expect0 = `[ +12.1234, 0, 44.123433, +55.12234, 6.1230835, 12384.126, +7348.12237, 1283.8, 0.000009 +]`; + const s = m.toString(); + log('toString: ', s); + expect(s).toBe(expect0); + }); }); \ No newline at end of file diff --git a/tests/core/math/mat4.test.ts b/tests/core/math/mat4.test.ts index 9740ca02dc2..dff276fd0da 100644 --- a/tests/core/math/mat4.test.ts +++ b/tests/core/math/mat4.test.ts @@ -631,4 +631,20 @@ describe('Test Mat4', () => { const r = Mat4.multiplyScalarAndAdd(new Mat4(), b, a, 2); expect(Mat4.equals(r, res)).toBe(true); }); + + test('toString', () => { + const m = new Mat4( +12.1234, 0.002, 44.123433, 55.12234, +6.1230835, 12384.126, 7348.12237, 283.23848943, +1283.8, 0.000009, 0.012384294, 0.18492384, +1284734.123, 0.12841203, 0, 0.00001283 +); + const expect0 = `[ +12.1234, 0.002, 44.123433, 55.12234, +6.1230835, 12384.126, 7348.12237, 283.23848943, +1283.8, 0.000009, 0.012384294, 0.18492384, +1284734.123, 0.12841203, 0, 0.00001283 +]`; + expect(m.toString()).toBe(expect0); + }); }); \ No newline at end of file diff --git a/tests/core/math/rect.test.ts b/tests/core/math/rect.test.ts index 95c8260ee8c..db6c7856010 100644 --- a/tests/core/math/rect.test.ts +++ b/tests/core/math/rect.test.ts @@ -78,7 +78,7 @@ describe('Test Rect', () => { }); test('toString', () => { const rect1 = new Rect(0, 0, 100, 100); - const res = '(0.00, 0.00, 100.00, 100.00)'; + const res = '(0, 0, 100, 100)'; expect(rect1.toString()).toBe(res); }); diff --git a/tests/core/math/size.test.ts b/tests/core/math/size.test.ts index 341eaa31f94..934b7f9d33a 100644 --- a/tests/core/math/size.test.ts +++ b/tests/core/math/size.test.ts @@ -34,7 +34,7 @@ describe('Test Size', () => { test('toString', () => { const size = new Size(100, 100); - expect(size.toString()).toBe('(100.00, 100.00)'); + expect(size.toString()).toBe('(100, 100)'); }); }); \ No newline at end of file diff --git a/tests/core/math/vec2.test.ts b/tests/core/math/vec2.test.ts index bfaeb687d43..1ac80219ef7 100644 --- a/tests/core/math/vec2.test.ts +++ b/tests/core/math/vec2.test.ts @@ -118,6 +118,11 @@ describe('Test Vec2', () => { expect(vec3.y).toBe(2); expect(vec3.z).toBe(0); }); + + test('toString', () => { + const v = new Vec2(100, 100); + expect(v.toString()).toBe('(100, 100)'); + }); }); function polar(angle: number, length: number) { diff --git a/tests/core/math/vec3.test.ts b/tests/core/math/vec3.test.ts index 63b0d2881fd..17c08592214 100644 --- a/tests/core/math/vec3.test.ts +++ b/tests/core/math/vec3.test.ts @@ -343,4 +343,9 @@ describe('Test Vec3', () => { expect(vec2.x).toBe(1); expect(vec2.y).toBe(2); }); + + test('toString', () => { + const v = new Vec3(100, 100, 1283.3892389); + expect(v.toString()).toBe('(100, 100, 1283.3892389)'); + }); }); \ No newline at end of file diff --git a/tests/core/math/vec4.test.ts b/tests/core/math/vec4.test.ts index 38801806935..b587a81c845 100644 --- a/tests/core/math/vec4.test.ts +++ b/tests/core/math/vec4.test.ts @@ -474,4 +474,9 @@ describe('Test Vec4', () => { expect(Vec4.equals(v4_0, v4_1)).toBeTruthy(); expect(Vec4.equals(v4_0, v4_2)).toBeFalsy(); }); + + test('toString', () => { + const v = new Vec4(100, 100, 1283.3892389, 0); + expect(v.toString()).toBe('(100, 100, 1283.3892389, 0)'); + }); }); \ No newline at end of file