From 8c7480fb9d01445dd8f1fa508f1ff087b479cba6 Mon Sep 17 00:00:00 2001 From: peabnuts123 Date: Wed, 26 Dec 2018 19:10:20 +1300 Subject: [PATCH] Adding tests for builder helpers --- src/builders/cylinder.ts | 12 +- src/builders/plane.ts | 16 +++ src/builders/sphere.ts | 26 ++-- test/builders/cylinder.test.ts | 209 ++++++++++++++++++++++++++++++++ test/builders/plane.test.ts | 212 +++++++++++++++++++++++++++++++++ test/builders/sphere.test.ts | 211 ++++++++++++++++++++++++++++++++ test/example.test.ts | 8 -- 7 files changed, 666 insertions(+), 28 deletions(-) create mode 100644 test/builders/cylinder.test.ts create mode 100644 test/builders/plane.test.ts create mode 100644 test/builders/sphere.test.ts delete mode 100644 test/example.test.ts diff --git a/src/builders/cylinder.ts b/src/builders/cylinder.ts index 62597db..f2a7f95 100644 --- a/src/builders/cylinder.ts +++ b/src/builders/cylinder.ts @@ -29,7 +29,7 @@ class NoiseMapBuilderCylinder { } public set lowerAngleBound(v: number) { if (v >= this.upperAngleBound) { - throw new Error('Lower bound cannot equal or exceed upper bound!'); + throw new Error('Lower angle bound cannot be equal to or exceed upper angle bound!'); } this._lowerAngleBound = v; @@ -40,7 +40,7 @@ class NoiseMapBuilderCylinder { } public set lowerHeightBound(v: number) { if (v >= this.upperHeightBound) { - throw new Error('Lower bound cannot equal or exceed upper bound!'); + throw new Error('Lower angle height cannot be equal to or exceed upper angle height!'); } this._lowerHeightBound = v; @@ -50,8 +50,8 @@ class NoiseMapBuilderCylinder { return this._upperAngleBound; } public set upperAngleBound(v: number) { - if (v <= this.upperAngleBound) { - throw new Error('Upper bound cannot equal or exceed upper bound!'); + if (v <= this.lowerAngleBound) { + throw new Error('Upper angle bound cannot be equal to or less than lower angle bound!'); } this._upperAngleBound = v; @@ -61,8 +61,8 @@ class NoiseMapBuilderCylinder { return this._upperHeightBound; } public set upperHeightBound(v: number) { - if (v <= this.upperHeightBound) { - throw new Error('Upper bound cannot equal or exceed upper bound!'); + if (v <= this.lowerHeightBound) { + throw new Error('Upper angle height cannot be equal to or less than lower angle height!'); } this._upperHeightBound = v; diff --git a/src/builders/plane.ts b/src/builders/plane.ts index 0590d9f..a3a9388 100644 --- a/src/builders/plane.ts +++ b/src/builders/plane.ts @@ -31,6 +31,10 @@ class NoiseMapBuilderPlane { return this._lowerXBound; } public set lowerXBound(v: number) { + if (v >= this.upperXBound) { + throw new Error('Lower X bound cannot be equal to or exceed upper X bound!'); + } + this._lowerXBound = v; } @@ -38,6 +42,10 @@ class NoiseMapBuilderPlane { return this._lowerYBound; } public set lowerYBound(v: number) { + if (v >= this.upperYBound) { + throw new Error('Lower Y bound cannot be equal to or exceed upper Y bound!'); + } + this._lowerYBound = v; } @@ -45,6 +53,10 @@ class NoiseMapBuilderPlane { return this._upperXBound; } public set upperXBound(v: number) { + if (v <= this.lowerXBound) { + throw new Error('Upper X bound cannot be equal to or less than lower X bound!'); + } + this._upperXBound = v; } @@ -52,6 +64,10 @@ class NoiseMapBuilderPlane { return this._upperYBound; } public set upperYBound(v: number) { + if (v <= this.lowerYBound) { + throw new Error('Upper Y bound cannot be equal to or less than lower Y bound!'); + } + this._upperYBound = v; } diff --git a/src/builders/sphere.ts b/src/builders/sphere.ts index 0214977..df3757c 100644 --- a/src/builders/sphere.ts +++ b/src/builders/sphere.ts @@ -7,7 +7,7 @@ class NoiseMapBuilderSphere { private height: number; private noiseMap: NoiseMap; private _eastLonBound: number; - private _northLatbound: number; + private _northLatBound: number; private _southLatBound: number; private _westLonBound: number; @@ -16,10 +16,10 @@ class NoiseMapBuilderSphere { this.width = width || 256; this.height = height || 256; - this.northLatBound = 0.0; - this.southLatBound = 0.0; - this.eastLonBound = 0.0; - this.westLonBound = 0.0; + this._northLatBound = 0.0; + this._southLatBound = 0.0; + this._eastLonBound = 0.0; + this._westLonBound = 0.0; this.noiseMap = new NoiseMap(this.width, this.height); } @@ -29,21 +29,21 @@ class NoiseMapBuilderSphere { } public set eastLonBound(v: number) { if (v <= this.westLonBound) { - throw new Error('Lower bound cannot equal or exceed east bound!'); + throw new Error('East longitudinal bound cannot be equal to or less than west longitudinal bound!'); } this._eastLonBound = v; } public get northLatBound() { - return this._northLatbound; + return this._northLatBound; } public set northLatBound(v: number) { if (v <= this.southLatBound) { - throw new Error('Lower bound cannot equal or exceed east bound!'); + throw new Error('North latitudinal bound cannot be equal to or less than south latitudinal bound!'); } - this._northLatbound = v; + this._northLatBound = v; } public get southLatBound() { @@ -51,10 +51,10 @@ class NoiseMapBuilderSphere { } public set southLatBound(v: number) { if (v >= this.northLatBound) { - throw new Error('Lower bound cannot equal or exceed east bound!'); + throw new Error('South latitudinal bound cannot be equal to or exceed north latitudinal bound!'); } - this._westLonBound = v; + this._southLatBound = v; } public get westLonBound() { @@ -62,9 +62,7 @@ class NoiseMapBuilderSphere { } public set westLonBound(v: number) { if (v >= this.eastLonBound) { - - throw new Error('Lower bound cannot equal or exceed east bound!'); - + throw new Error('West longitudinal bound cannot be equal to or exceed east longitudinal bound!'); } this._westLonBound = v; diff --git a/test/builders/cylinder.test.ts b/test/builders/cylinder.test.ts new file mode 100644 index 0000000..6e3d20a --- /dev/null +++ b/test/builders/cylinder.test.ts @@ -0,0 +1,209 @@ +import { expect } from 'chai'; +import { describe, it } from 'mocha'; + +import { Cylinder } from '@app/builders'; +import { Const } from '@app/module/generator'; +import NoiseMap from '@app/noisemap'; + +describe("builders/cylinder", () => { + it("can construct successfully", () => { + // Setup + const value = 2; + const sourceModule = new Const(value); + const width = 10; + const height = 10; + + // Test + const testFunc = () => { + new Cylinder(sourceModule, width, height); + }; + + // Assert + expect(testFunc).not.to.throw(); + }); + + it("lowerAngleBound defaults to 0", () => { + // Setup + const mockModule = createMockCylinder(); + + // Test + let lowerAngleBound: number = mockModule.lowerAngleBound; + + // Assert + expect(lowerAngleBound).to.equal(0); + }); + + it("setting lowerAngleBound updates correctly", () => { + // Setup + const mockModule = createMockCylinder(); + const newValue = 0.5; + + // Test + mockModule.lowerAngleBound = newValue; + const updatedLowerAngleBound = mockModule.lowerAngleBound; + + // Assert + expect(updatedLowerAngleBound).to.equal(newValue); + }); + + it("setting lowerAngleBound to a value higher than upperAngleBound throws an error", () => { + // Setup + const mockModule = createMockCylinder(); + const newValue = mockModule.upperAngleBound + 1; + + // Test + const testFunc = () => { + mockModule.lowerAngleBound = newValue; + }; + + // Assert + expect(testFunc).to.throw(); + }); + + it("lowerHeightBound defaults to 0", () => { + // Setup + const mockModule = createMockCylinder(); + + // Test + let lowerHeightBound: number = mockModule.lowerHeightBound; + + // Assert + expect(lowerHeightBound).to.equal(0); + }); + + it("setting lowerHeightBound updates correctly", () => { + // Setup + const mockModule = createMockCylinder(); + const newValue = 0.5; + + // Test + mockModule.lowerHeightBound = newValue; + const updatedLowerHeightBound = mockModule.lowerHeightBound; + + // Assert + expect(updatedLowerHeightBound).to.equal(newValue); + }); + + it("setting lowerHeightBound to a value higher than upperHeightBound throws an error", () => { + // Setup + const mockModule = createMockCylinder(); + const newValue = mockModule.upperHeightBound + 1; + + // Test + const testFunc = () => { + mockModule.lowerHeightBound = newValue; + }; + + // Assert + expect(testFunc).to.throw(); + }); + + it("upperAngleBound defaults to 1", () => { + // Setup + const mockModule = createMockCylinder(); + + // Test + let upperAngleBound: number = mockModule.upperAngleBound; + + // Assert + expect(upperAngleBound).to.equal(1); + }); + + it("setting upperAngleBound updates correctly", () => { + // Setup + const mockModule = createMockCylinder(); + const newValue = 0.5; + + // Test + mockModule.upperAngleBound = newValue; + const updatedUpperAngleBound = mockModule.upperAngleBound; + + // Assert + expect(updatedUpperAngleBound).to.equal(newValue); + }); + + it("setting upperAngleBound to a value lower than lowerAngleBound throws an error", () => { + // Setup + const mockModule = createMockCylinder(); + const newValue = mockModule.lowerAngleBound - 1; + + // Test + const testFunc = () => { + mockModule.upperAngleBound = newValue; + }; + + // Assert + expect(testFunc).to.throw(); + }); + + it("upperHeightBound defaults to 1", () => { + // Setup + const mockModule = createMockCylinder(); + + // Test + let upperHeightBound: number = mockModule.upperHeightBound; + + // Assert + expect(upperHeightBound).to.equal(1); + }); + + it("setting upperHeightBound updates correctly", () => { + // Setup + const mockModule = createMockCylinder(); + const newValue = 0.5; + + // Test + mockModule.upperHeightBound = newValue; + const updatedUpperAngleBound = mockModule.upperHeightBound; + + // Assert + expect(updatedUpperAngleBound).to.equal(newValue); + }); + + it("setting upperHeightBound to a value lower than lowerHeightBound throws an error", () => { + // Setup + const mockModule = createMockCylinder(); + const newValue = mockModule.lowerHeightBound - 1; + + // Test + const testFunc = () => { + mockModule.upperHeightBound = newValue; + }; + + // Assert + expect(testFunc).to.throw(); + }); + + it("calling build returns a noise map", () => { + // Setup + const mockModule: Cylinder = createMockCylinder(); + + // Test + const noiseMap: NoiseMap = mockModule.build(); + + // Assert + expect(noiseMap).not.to.be.null; + }); + + it("calling build with missing source module throws an error", () => { + // Setup + const mockModule: Cylinder = new Cylinder(); + + // Test + const testFunc = () => { + mockModule.build(); + }; + + // Assert + expect(testFunc).to.throw(); + }); +}); + +function createMockCylinder(): Cylinder { + const value: number = 2; + const sourceModule: any = new Const(value); + const width: number = 10; + const height: number = 10; + + return new Cylinder(sourceModule, width, height); +} \ No newline at end of file diff --git a/test/builders/plane.test.ts b/test/builders/plane.test.ts new file mode 100644 index 0000000..47756b1 --- /dev/null +++ b/test/builders/plane.test.ts @@ -0,0 +1,212 @@ +import { expect } from 'chai'; +import { describe, it } from 'mocha'; + +import { Plane } from '@app/builders'; +import { Const } from '@app/module/generator'; +import NoiseMap from '@app/noisemap'; + +describe("builders/plane", () => { + it("can construct successfully", () => { + // Setup + const value: number = 2; + const sourceModule = new Const(value); + const width: number = 10; + const height: number = 10; + const seamless = false; + + // Test + const testFunc = () => { + new Plane(sourceModule, width, height, seamless); + }; + + // Assert + expect(testFunc).not.to.throw; + }); + + it("lowerXBound defaults to 0", () => { + // Setup + const mockModule = createMockPlane(); + + // Test + let lowerXBound: number = mockModule.lowerXBound; + + // Assert + expect(lowerXBound).to.equal(0); + }); + + it("setting lowerXBound updates correctly", () => { + // Setup + const mockModule = createMockPlane(); + const newValue = 0.5; + + // Test + mockModule.lowerXBound = newValue; + const updatedLowerXBound = mockModule.lowerXBound; + + // Assert + expect(updatedLowerXBound).to.equal(newValue); + }); + + it("setting lowerXBound to a value higher than upperXBound throws an error", () => { + // Setup + const mockModule = createMockPlane(); + const newValue = mockModule.upperXBound + 1; + + // Test + const testFunc = () => { + mockModule.lowerXBound = newValue; + }; + + // Assert + expect(testFunc).to.throw(); + }); + + it("lowerYBound defaults to 0", () => { + // Setup + const mockModule = createMockPlane(); + + // Test + let lowerYBound: number = mockModule.lowerYBound; + + // Assert + expect(lowerYBound).to.equal(0); + }); + + it("setting lowerYBound updates correctly", () => { + // Setup + const mockModule = createMockPlane(); + const newValue = 0.5; + + // Test + mockModule.lowerYBound = newValue; + const updatedLowerYBound = mockModule.lowerYBound; + + // Assert + expect(updatedLowerYBound).to.equal(newValue); + }); + + it("setting lowerYBound to a value higher than upperYBound throws an error", () => { + // Setup + const mockModule = createMockPlane(); + const newValue = mockModule.upperYBound + 1; + + // Test + const testFunc = () => { + mockModule.lowerYBound = newValue; + }; + + // Assert + expect(testFunc).to.throw(); + }); + + it("upperXBound defaults to 1", () => { + // Setup + const mockModule = createMockPlane(); + + // Test + let upperXBound: number = mockModule.upperXBound; + + // Assert + expect(upperXBound).to.equal(1); + }); + + it("setting upperXBound updates correctly", () => { + // Setup + const mockModule = createMockPlane(); + const newValue = 0.5; + + // Test + mockModule.upperXBound = newValue; + const updatedUpperXBound = mockModule.upperXBound; + + // Assert + expect(updatedUpperXBound).to.equal(newValue); + }); + + it("setting upperXBound to a value lower than lowerXBound throws an error", () => { + // Setup + const mockModule = createMockPlane(); + const newValue = mockModule.lowerXBound - 1; + + // Test + const testFunc = () => { + mockModule.upperXBound = newValue; + }; + + // Assert + expect(testFunc).to.throw(); + }); + + it("upperYBound defaults to 1", () => { + // Setup + const mockModule = createMockPlane(); + + // Test + let upperYBound: number = mockModule.upperYBound; + + // Assert + expect(upperYBound).to.equal(1); + }); + + it("setting upperYBound updates correctly", () => { + // Setup + const mockModule = createMockPlane(); + const newValue = 0.5; + + // Test + mockModule.upperYBound = newValue; + const updatedUpperAngleBound = mockModule.upperYBound; + + // Assert + expect(updatedUpperAngleBound).to.equal(newValue); + }); + + it("setting upperYBound to a value lower than lowerYBound throws an error", () => { + // Setup + const mockModule = createMockPlane(); + const newValue = mockModule.lowerYBound - 1; + + // Test + const testFunc = () => { + mockModule.upperYBound = newValue; + }; + + // Assert + expect(testFunc).to.throw(); + }); + + it("calling build returns a noise map", () => { + // Setup + const mockModule = createMockPlane(); + + // Test + const noiseMap: NoiseMap = mockModule.build(); + + // Assert + expect(noiseMap).not.to.be.null; + }); + + it("calling build with missing source module throws an error", () => { + // Setup + const mockModule = new Plane(); + + // Test + const testFunc = () => { + mockModule.build(); + }; + + // Assert + expect(testFunc).to.throw(); + }); +}); + +function createMockPlane() { + // Setup + const value: number = 2; + const sourceModule = new Const(value); + const width: number = 10; + const height: number = 10; + const seamless = false; + + return new Plane(sourceModule, width, height, seamless); +} \ No newline at end of file diff --git a/test/builders/sphere.test.ts b/test/builders/sphere.test.ts new file mode 100644 index 0000000..5e7dbb0 --- /dev/null +++ b/test/builders/sphere.test.ts @@ -0,0 +1,211 @@ +import { expect } from 'chai'; +import { describe, it } from 'mocha'; + +import { Sphere } from '@app/builders'; +import { Const } from '@app/module/generator'; +import NoiseMap from '@app/noisemap'; + +describe("builders/sphere", () => { + it("can construct successfully", () => { + // Setup + const value: number = 2; + const sourceModule = new Const(value); + const width: number = 10; + const height: number = 10; + + // Test + const testFunc = () => { + new Sphere(sourceModule, width, height); + }; + + // Assert + expect(testFunc).not.to.throw; + }); + + it("westLonBound defaults to 0", () => { + // Setup + const mockModule = createMockSphere(); + + // Test + let westLonBound: number = mockModule.westLonBound; + + // Assert + expect(westLonBound).to.equal(0); + }); + + it("setting westLonBound updates correctly", () => { + // Setup + const mockModule = createMockSphere(); + const newValue = -0.5; + + // Test + mockModule.westLonBound = newValue; + const updatedWestLonBound = mockModule.westLonBound; + + // Assert + expect(updatedWestLonBound).to.equal(newValue); + }); + + it("setting westLonBound to a value higher than eastLonBound throws an error", () => { + // Setup + const mockModule = createMockSphere(); + const newValue = mockModule.eastLonBound + 1; + + // Test + const testFunc = () => { + mockModule.westLonBound = newValue; + }; + + // Assert + expect(testFunc).to.throw(); + }); + + it("southLatBound defaults to 0", () => { + // Setup + const mockModule = createMockSphere(); + + // Test + let southLatBound: number = mockModule.southLatBound; + + // Assert + expect(southLatBound).to.equal(0); + }); + + it("setting southLatBound updates correctly", () => { + // Setup + const mockModule = createMockSphere(); + const newValue = -0.5; + + // Test + mockModule.southLatBound = newValue; + const updatedSouthLatBound = mockModule.southLatBound; + + // Assert + expect(updatedSouthLatBound).to.equal(newValue); + }); + + it("setting southLatBound to a value higher than northLatBound throws an error", () => { + // Setup + const mockModule = createMockSphere(); + const newValue = mockModule.northLatBound + 1; + + // Test + const testFunc = () => { + mockModule.southLatBound = newValue; + }; + + // Assert + expect(testFunc).to.throw(); + }); + + it("eastLonBound defaults to 0", () => { + // Setup + const mockModule = createMockSphere(); + + // Test + let eastLonBound: number = mockModule.eastLonBound; + + // Assert + expect(eastLonBound).to.equal(0); + }); + + it("setting eastLonBound updates correctly", () => { + // Setup + const mockModule = createMockSphere(); + const newValue = 0.5; + + // Test + mockModule.eastLonBound = newValue; + const updatedEastLonBound = mockModule.eastLonBound; + + // Assert + expect(updatedEastLonBound).to.equal(newValue); + }); + + it("setting eastLonBound to a value lower than westLonBound throws an error", () => { + // Setup + const mockModule = createMockSphere(); + const newValue = mockModule.westLonBound - 1; + + // Test + const testFunc = () => { + mockModule.eastLonBound = newValue; + }; + + // Assert + expect(testFunc).to.throw(); + }); + + it("northLatBound defaults to 0", () => { + // Setup + const mockModule = createMockSphere(); + + // Test + let northLatBound: number = mockModule.northLatBound; + + // Assert + expect(northLatBound).to.equal(0); + }); + + it("setting northLatBound updates correctly", () => { + // Setup + const mockModule = createMockSphere(); + const newValue = 0.5; + + // Test + mockModule.northLatBound = newValue; + const updatedNorthLatBound = mockModule.northLatBound; + + // Assert + expect(updatedNorthLatBound).to.equal(newValue); + }); + + it("setting northLatBound to a value lower than southLatBound throws an error", () => { + // Setup + const mockModule = createMockSphere(); + const newValue = mockModule.southLatBound - 1; + + // Test + const testFunc = () => { + mockModule.northLatBound = newValue; + }; + + // Assert + expect(testFunc).to.throw(); + }); + + it("calling build returns a noise map", () => { + // Setup + const mockModule = createMockSphere(); + + // Test + const noiseMap: NoiseMap = mockModule.build(); + + // Assert + expect(noiseMap).not.to.be.null; + }); + + it("calling build with missing source module throws an error", () => { + // Setup + const mockModule = new Sphere(); + + // Test + const testFunc = () => { + mockModule.build(); + }; + + // Assert + expect(testFunc).to.throw(); + }); +}); + +function createMockSphere() { + const value: number = 2; + const sourceModule = new Const(value); + const width: number = 10; + const height: number = 10; + + + + return new Sphere(sourceModule, width, height); +} \ No newline at end of file diff --git a/test/example.test.ts b/test/example.test.ts deleted file mode 100644 index 1d33020..0000000 --- a/test/example.test.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { expect } from 'chai'; -import { describe, it } from 'mocha'; - -describe('example test', () => { - it('passes', () => { - expect(true).to.be.true; - }); -});