Skip to content

Commit

Permalink
Adding tests for builder helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
peabnuts123 committed Dec 26, 2018
1 parent df3a293 commit 8c7480f
Show file tree
Hide file tree
Showing 7 changed files with 666 additions and 28 deletions.
12 changes: 6 additions & 6 deletions src/builders/cylinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/builders/plane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,43 @@ 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;
}

public get lowerYBound() {
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;
}

public get upperXBound() {
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;
}

public get upperYBound() {
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;
}

Expand Down
26 changes: 12 additions & 14 deletions src/builders/sphere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand All @@ -29,42 +29,40 @@ 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() {
return this._southLatBound;
}
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() {
return this._westLonBound;
}
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;
Expand Down
209 changes: 209 additions & 0 deletions test/builders/cylinder.test.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 8c7480f

Please sign in to comment.