From 30e6323bf338f2ed6a18237220044ab8ce84bfbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Lemaire?= Date: Fri, 6 Feb 2015 01:00:00 +0100 Subject: [PATCH] Added unit tests --- src/scripts/game/specs/Target.spec.ts | 43 +++++++++++++++++++++++++++ src/scripts/game/specs/Tools.spec.ts | 32 ++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/scripts/game/specs/Target.spec.ts create mode 100644 src/scripts/game/specs/Tools.spec.ts diff --git a/src/scripts/game/specs/Target.spec.ts b/src/scripts/game/specs/Target.spec.ts new file mode 100644 index 0000000..35367b6 --- /dev/null +++ b/src/scripts/game/specs/Target.spec.ts @@ -0,0 +1,43 @@ +/// + +module SpaceTac.Game.Specs { + "use strict"; + + describe("Target", () => { + it("initializes from ship or location", () => { + var target: Target; + + target = Target.newFromLocation(2, 3); + expect(target.x).toEqual(2); + expect(target.y).toEqual(3); + expect(target.ship).toBeNull(); + + var ship = new Ship(); + ship.arena_x = 4; + ship.arena_y = -2.1; + target = Target.newFromShip(ship); + expect(target.x).toEqual(4); + expect(target.y).toEqual(-2.1); + expect(target.ship).toBe(ship); + }); + + it("gets distance to another target", () => { + var t1 = Target.newFromLocation(5, 1); + var t2 = Target.newFromLocation(6, 2); + expect(t1.getDistanceTo(t2)).toBeCloseTo(Math.sqrt(2), 0.00001); + }); + + it("checks if a target is in range of another", () => { + var t1 = Target.newFromLocation(5, 4); + expect(t1.isInRange(7, 3, 2)).toBe(false); + expect(t1.isInRange(7, 3, 3)).toBe(true); + expect(t1.isInRange(5, 5, 2)).toBe(true); + }); + + it("constraints a target to a limited range", () => { + var target = Target.newFromLocation(5, 9); + expect(target.constraintInRange(1, 1, Math.sqrt(80) * 0.5)).toEqual(Target.newFromLocation(3, 5)); + expect(target.constraintInRange(1, 1, 70)).toBe(target); + }); + }); +} diff --git a/src/scripts/game/specs/Tools.spec.ts b/src/scripts/game/specs/Tools.spec.ts new file mode 100644 index 0000000..1eda223 --- /dev/null +++ b/src/scripts/game/specs/Tools.spec.ts @@ -0,0 +1,32 @@ +/// + +module SpaceTac.Game.Specs { + "use strict"; + + class TestObj { + a: string; + b: any; + + constructor() { + this.a = "test"; + this.b = {c: 5.1, d: ["unit", "test", 5]}; + } + + get(): string { + return this.a; + } + } + + describe("Tools", () => { + it("copies full javascript objects", () => { + var ini = new TestObj(); + + var cop = Tools.copyObject(ini); + + expect(cop).not.toBe(ini); + expect(cop).toEqual(ini); + + expect(cop.get()).toEqual("test"); + }); + }); +}