From 119b2cb71a6af6bc2702bfa2bc9b7939b03c5658 Mon Sep 17 00:00:00 2001 From: Daan Vanden Bosch Date: Tue, 28 May 2019 22:24:28 +0200 Subject: [PATCH] Converted all tests to TypeScript. --- ...rsor.test.js => ArrayBufferCursor.test.ts} | 48 +++++++++---------- .../prs/{index.test.js => index.test.ts} | 4 +- src/data/parsing/{bin.test.js => bin.test.ts} | 0 src/data/parsing/{dat.test.js => dat.test.ts} | 0 src/data/parsing/{qst.test.js => qst.test.ts} | 0 .../parsing/{quest.test.js => quest.test.ts} | 14 +++--- .../{entities.test.js => entities.test.ts} | 16 ++++--- src/rendering/entities.ts | 6 +-- src/three-orbit-controls.d.ts | 1 + tsconfig.json | 2 +- 10 files changed, 46 insertions(+), 45 deletions(-) rename src/data/{ArrayBufferCursor.test.js => ArrayBufferCursor.test.ts} (79%) rename src/data/compression/prs/{index.test.js => index.test.ts} (94%) rename src/data/parsing/{bin.test.js => bin.test.ts} (100%) rename src/data/parsing/{dat.test.js => dat.test.ts} (100%) rename src/data/parsing/{qst.test.js => qst.test.ts} (100%) rename src/data/parsing/{quest.test.js => quest.test.ts} (89%) rename src/rendering/{entities.test.js => entities.test.ts} (80%) create mode 100644 src/three-orbit-controls.d.ts diff --git a/src/data/ArrayBufferCursor.test.js b/src/data/ArrayBufferCursor.test.ts similarity index 79% rename from src/data/ArrayBufferCursor.test.js rename to src/data/ArrayBufferCursor.test.ts index d224ed7a..09eb892b 100644 --- a/src/data/ArrayBufferCursor.test.js +++ b/src/data/ArrayBufferCursor.test.ts @@ -45,31 +45,31 @@ function test_integer_read(method_name: string) { let test_number_1 = 0; let test_number_2 = 0; // The "false" arrays are for big endian tests and the "true" arrays for little endian tests. - const test_arrays = { false: [], true: [] }; + const test_arrays: { [index: string]: number[] } = { false: [], true: [] }; for (let i = 1; i <= bytes; ++i) { // Generates numbers of the form 0x010203... test_number_1 <<= 8; test_number_1 |= i; - test_arrays[false].push(i); - test_arrays[true].unshift(i); + test_arrays['false'].push(i); + test_arrays['true'].unshift(i); } for (let i = bytes + 1; i <= 2 * bytes; ++i) { test_number_2 <<= 8; test_number_2 |= i; - test_arrays[false].push(i); - test_arrays[true].splice(bytes, 0, i); + test_arrays['false'].push(i); + test_arrays['true'].splice(bytes, 0, i); } for (const little_endian of [false, true]) { const cursor = new ArrayBufferCursor( - new Uint8Array(test_arrays[little_endian]).buffer, little_endian); + new Uint8Array(test_arrays[String(little_endian)]).buffer, little_endian); - expect(cursor[method_name]()).toBe(test_number_1); + expect((cursor as any)[method_name]()).toBe(test_number_1); expect(cursor.position).toBe(bytes); - expect(cursor[method_name]()).toBe(test_number_2); + expect((cursor as any)[method_name]()).toBe(test_number_2); expect(cursor.position).toBe(2 * bytes); } }); @@ -109,25 +109,25 @@ function test_string_read(method_name: string, char_size: number) { new Uint8Array(char_array_copy).buffer, little_endian); cursor.seek_start(char_size); - expect(cursor[method_name](4 * char_size, true, true)).toBe('AB'); + expect((cursor as any)[method_name](4 * char_size, true, true)).toBe('AB'); expect(cursor.position).toBe(5 * char_size); cursor.seek_start(char_size); - expect(cursor[method_name](2 * char_size, true, true)).toBe('AB'); + expect((cursor as any)[method_name](2 * char_size, true, true)).toBe('AB'); expect(cursor.position).toBe(3 * char_size); cursor.seek_start(char_size); - expect(cursor[method_name](4 * char_size, true, false)).toBe('AB'); + expect((cursor as any)[method_name](4 * char_size, true, false)).toBe('AB'); expect(cursor.position).toBe(4 * char_size); cursor.seek_start(char_size); - expect(cursor[method_name](2 * char_size, true, false)).toBe('AB'); + expect((cursor as any)[method_name](2 * char_size, true, false)).toBe('AB'); expect(cursor.position).toBe(3 * char_size); cursor.seek_start(char_size); - expect(cursor[method_name](4 * char_size, false, true)).toBe('AB\0ÿ'); + expect((cursor as any)[method_name](4 * char_size, false, true)).toBe('AB\0ÿ'); expect(cursor.position).toBe(5 * char_size); cursor.seek_start(char_size); - expect(cursor[method_name](4 * char_size, false, false)).toBe('AB\0ÿ'); + expect((cursor as any)[method_name](4 * char_size, false, false)).toBe('AB\0ÿ'); expect(cursor.position).toBe(5 * char_size); } }); @@ -142,8 +142,8 @@ function test_integer_write(method_name: string) { let test_number_1 = 0; let test_number_2 = 0; // The "false" arrays are for big endian tests and the "true" arrays for little endian tests. - const test_arrays_1 = { false: [], true: [] }; - const test_arrays_2 = { false: [], true: [] }; + const test_arrays_1: { [index: string]: number[] } = { false: [], true: [] }; + const test_arrays_2: { [index: string]: number[] } = { false: [], true: [] }; for (let i = 1; i <= bytes; ++i) { // Generates numbers of the form 0x010203... @@ -151,26 +151,26 @@ function test_integer_write(method_name: string) { test_number_1 |= i; test_number_2 <<= 8; test_number_2 |= i + bytes; - test_arrays_1[false].push(i); - test_arrays_1[true].unshift(i); - test_arrays_2[false].push(i + bytes); - test_arrays_2[true].unshift(i + bytes); + test_arrays_1['false'].push(i); + test_arrays_1['true'].unshift(i); + test_arrays_2['false'].push(i + bytes); + test_arrays_2['true'].unshift(i + bytes); } for (const little_endian of [false, true]) { const cursor = new ArrayBufferCursor(0, little_endian); - cursor[method_name](test_number_1); + (cursor as any)[method_name](test_number_1); expect(cursor.position).toBe(bytes); expect(cursor.seek_start(0).u8_array(bytes)) - .toEqual(test_arrays_1[little_endian]); + .toEqual(test_arrays_1[String(little_endian)]); expect(cursor.position).toBe(bytes); - cursor[method_name](test_number_2); + (cursor as any)[method_name](test_number_2); expect(cursor.position).toBe(2 * bytes); expect(cursor.seek_start(0).u8_array(2 * bytes)) - .toEqual(test_arrays_1[little_endian].concat(test_arrays_2[little_endian])); + .toEqual(test_arrays_1[String(little_endian)].concat(test_arrays_2[String(little_endian)])); } }); } diff --git a/src/data/compression/prs/index.test.js b/src/data/compression/prs/index.test.ts similarity index 94% rename from src/data/compression/prs/index.test.js rename to src/data/compression/prs/index.test.ts index 7e5f3e45..29581444 100644 --- a/src/data/compression/prs/index.test.js +++ b/src/data/compression/prs/index.test.ts @@ -1,5 +1,5 @@ import { ArrayBufferCursor } from '../../ArrayBufferCursor'; -import { compress, decompress } from '../prs'; +import { compress, decompress } from '.'; function test_with_bytes(bytes: number[], expected_compressed_size: number) { const cursor = new ArrayBufferCursor(new Uint8Array(bytes).buffer, true); @@ -46,7 +46,7 @@ test('PRS compression and decompression, typical case', () => { const pattern = [0, 0, 2, 0, 3, 0, 5, 0, 0, 0, 7, 9, 11, 13, 0, 0]; const arrays = new Array(100) .fill(pattern) - .map(array => array.map(e => e + prng.next_integer(0, 10))); + .map(array => array.map((e: number) => e + prng.next_integer(0, 10))); const flattened_array = [].concat.apply([], arrays); // Compression factor: 0.834 diff --git a/src/data/parsing/bin.test.js b/src/data/parsing/bin.test.ts similarity index 100% rename from src/data/parsing/bin.test.js rename to src/data/parsing/bin.test.ts diff --git a/src/data/parsing/dat.test.js b/src/data/parsing/dat.test.ts similarity index 100% rename from src/data/parsing/dat.test.js rename to src/data/parsing/dat.test.ts diff --git a/src/data/parsing/qst.test.js b/src/data/parsing/qst.test.ts similarity index 100% rename from src/data/parsing/qst.test.js rename to src/data/parsing/qst.test.ts diff --git a/src/data/parsing/quest.test.js b/src/data/parsing/quest.test.ts similarity index 89% rename from src/data/parsing/quest.test.js rename to src/data/parsing/quest.test.ts index 386e15cd..099d59fc 100644 --- a/src/data/parsing/quest.test.js +++ b/src/data/parsing/quest.test.ts @@ -2,13 +2,13 @@ import * as fs from 'fs'; import { ArrayBufferCursor } from '../ArrayBufferCursor'; import * as prs from '../compression/prs'; import { parse_quest, write_quest_qst } from './quest'; -import { ObjectType } from '../../domain'; +import { ObjectType, Quest } from '../../domain'; import { walk_qst_files } from '../../../test/src/utils'; test('parse Towards the Future', () => { const buffer = fs.readFileSync('test/resources/quest118_e.qst').buffer; const cursor = new ArrayBufferCursor(buffer, true); - const quest = parse_quest(cursor); + const quest = parse_quest(cursor)!; expect(quest.name).toBe('Towards the Future'); expect(quest.short_description).toBe('Challenge the\nnew simulator.'); @@ -29,8 +29,8 @@ test('parse Towards the Future', () => { test('parse_quest and write_quest_qst', () => { const buffer = fs.readFileSync('test/resources/tethealla_v0.143_quests/solo/ep1/02.qst').buffer; const cursor = new ArrayBufferCursor(buffer, true); - const orig_quest = parse_quest(cursor); - const test_quest = parse_quest(write_quest_qst(orig_quest, '02.qst')); + const orig_quest = parse_quest(cursor)!; + const test_quest = parse_quest(write_quest_qst(orig_quest, '02.qst'))!; expect(test_quest.name).toBe(orig_quest.name); expect(test_quest.short_description).toBe(orig_quest.short_description); @@ -44,7 +44,7 @@ test('parse_quest and write_quest_qst', () => { .toEqual(testable_area_variants(orig_quest)); }); -function testable_objects(quest) { +function testable_objects(quest: Quest) { return quest.objects.map(object => [ object.area_id, object.section_id, @@ -53,7 +53,7 @@ function testable_objects(quest) { ]); } -function testable_npcs(quest) { +function testable_npcs(quest: Quest) { return quest.npcs.map(npc => [ npc.area_id, npc.section_id, @@ -62,6 +62,6 @@ function testable_npcs(quest) { ]); } -function testable_area_variants(quest) { +function testable_area_variants(quest: Quest) { return quest.area_variants.map(av => [av.area.id, av.id]); } diff --git a/src/rendering/entities.test.js b/src/rendering/entities.test.ts similarity index 80% rename from src/rendering/entities.test.js rename to src/rendering/entities.test.ts index 8254b58b..440b5203 100644 --- a/src/rendering/entities.test.js +++ b/src/rendering/entities.test.ts @@ -4,16 +4,18 @@ import { OBJECT_COLOR, NPC_COLOR } from './entities'; -import { Object3D, Vector3 } from 'three'; +import { Object3D, Vector3, MeshLambertMaterial, CylinderBufferGeometry } from 'three'; import { Vec3, QuestNpc, QuestObject, Section, NpcType, ObjectType } from '../domain'; +const cylinder = new CylinderBufferGeometry(3, 3, 20).translate(0, 10, 0); + test('create geometry for quest objects', () => { const object = new QuestObject(7, 13, new Vec3(17, 19, 23), new Vec3(), ObjectType.PrincipalWarp, null); const sect_rot = 0.6; const sect_rot_sin = Math.sin(sect_rot); const sect_rot_cos = Math.cos(sect_rot); const geometry = create_object_mesh( - object, [new Section(13, new Vec3(29, 31, 37), sect_rot)]); + object, [new Section(13, new Vec3(29, 31, 37), sect_rot)], cylinder); expect(geometry).toBeInstanceOf(Object3D); expect(geometry.name).toBe('Object'); @@ -21,7 +23,7 @@ test('create geometry for quest objects', () => { expect(geometry.position.x).toBe(sect_rot_cos * 17 + sect_rot_sin * 23 + 29); expect(geometry.position.y).toBe(19 + 31); expect(geometry.position.z).toBe(-sect_rot_sin * 17 + sect_rot_cos * 23 + 37); - expect(geometry.material.color.getHex()).toBe(OBJECT_COLOR); + expect((geometry.material as MeshLambertMaterial).color.getHex()).toBe(OBJECT_COLOR); }); test('create geometry for quest NPCs', () => { @@ -30,7 +32,7 @@ test('create geometry for quest NPCs', () => { const sect_rot_sin = Math.sin(sect_rot); const sect_rot_cos = Math.cos(sect_rot); const geometry = create_npc_mesh( - npc, [new Section(13, new Vec3(29, 31, 37), sect_rot)]); + npc, [new Section(13, new Vec3(29, 31, 37), sect_rot)], cylinder); expect(geometry).toBeInstanceOf(Object3D); expect(geometry.name).toBe('NPC'); @@ -38,13 +40,13 @@ test('create geometry for quest NPCs', () => { expect(geometry.position.x).toBe(sect_rot_cos * 17 + sect_rot_sin * 23 + 29); expect(geometry.position.y).toBe(19 + 31); expect(geometry.position.z).toBe(-sect_rot_sin * 17 + sect_rot_cos * 23 + 37); - expect(geometry.material.color.getHex()).toBe(NPC_COLOR); + expect((geometry.material as MeshLambertMaterial).color.getHex()).toBe(NPC_COLOR); }); test('geometry position changes when entity position changes element-wise', () => { const npc = new QuestNpc(7, 13, new Vec3(17, 19, 23), new Vec3(), NpcType.Booma, null); const geometry = create_npc_mesh( - npc, [new Section(13, new Vec3(0, 0, 0), 0)]); + npc, [new Section(13, new Vec3(0, 0, 0), 0)], cylinder); npc.position = new Vec3(2, 3, 5).add(npc.position); expect(geometry.position).toEqual(new Vector3(19, 22, 28)); @@ -53,7 +55,7 @@ test('geometry position changes when entity position changes element-wise', () = test('geometry position changes when entire entity position changes', () => { const npc = new QuestNpc(7, 13, new Vec3(17, 19, 23), new Vec3(), NpcType.Booma, null); const geometry = create_npc_mesh( - npc, [new Section(13, new Vec3(0, 0, 0), 0)]); + npc, [new Section(13, new Vec3(0, 0, 0), 0)], cylinder); npc.position = new Vec3(2, 3, 5); expect(geometry.position).toEqual(new Vector3(2, 3, 5)); diff --git a/src/rendering/entities.ts b/src/rendering/entities.ts index c6e69ab3..42b37691 100644 --- a/src/rendering/entities.ts +++ b/src/rendering/entities.ts @@ -1,4 +1,4 @@ -import { BufferGeometry, CylinderGeometry, DoubleSide, Mesh, MeshLambertMaterial } from 'three'; +import { BufferGeometry, DoubleSide, Mesh, MeshLambertMaterial } from 'three'; import { autorun } from 'mobx'; import { Vec3, VisibleQuestEntity, QuestNpc, QuestObject, Section } from '../domain'; @@ -17,8 +17,6 @@ export function create_npc_mesh(npc: QuestNpc, sections: Section[], geometry: Bu return create_mesh(npc, sections, geometry, NPC_COLOR, 'NPC'); } -const cylinder = new CylinderGeometry(3, 3, 20).translate(0, 10, 0); - function create_mesh( entity: VisibleQuestEntity, sections: Section[], @@ -43,7 +41,7 @@ function create_mesh( } const object_3d = new Mesh( - geometry || cylinder, + geometry, new MeshLambertMaterial({ color, side: DoubleSide diff --git a/src/three-orbit-controls.d.ts b/src/three-orbit-controls.d.ts new file mode 100644 index 00000000..e82fb35f --- /dev/null +++ b/src/three-orbit-controls.d.ts @@ -0,0 +1 @@ +declare module 'three-orbit-controls'; \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index b3583351..8780ab67 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -24,4 +24,4 @@ "include": [ "src" ] -} \ No newline at end of file +}