Skip to content

Commit

Permalink
Adding tests for selector modules
Browse files Browse the repository at this point in the history
  • Loading branch information
peabnuts123 committed Dec 30, 2018
1 parent 37cb3eb commit b6738b9
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/module/selector/blend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Blend {
}

public getValue(x: number, y: number, z: number) {
if (!(this.sourceModules.length < 2)) {
if (this.sourceModules.length < 2) {
throw new Error('Invalid or missing source module(s)!');
}

Expand Down
2 changes: 1 addition & 1 deletion src/module/selector/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Select {
}

public getValue(x: number, y: number, z: number) {
if (!(this.sourceModules.length < 2)) {
if (this.sourceModules.length < 2) {
throw new Error('Invalid or missing source module(s)!');
}

Expand Down
108 changes: 108 additions & 0 deletions test/module/selector/blend.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { Const } from '@app/module/generator';
import { Blend } from '@app/module/selector';

describe('module/selector/blend', () => {
it("can construct successfully", () => {
// Setup
const sourceValueA = 2;
const sourceModuleA = new Const(sourceValueA);
const sourceValueB = 2;
const sourceModuleB = new Const(sourceValueB);
const controlValue = 0.5;
const controlModule = new Const(controlValue);

// Test
const testFunc = () => {
new Blend([sourceModuleA, sourceModuleB], controlModule);
};

// Assert
expect(testFunc).not.to.throw();
});

it("calling getValue with 0 sourceModules throws an error", () => {
// Setup
const x = 10;
const y = 10;
const z = 10;
const controlValue = 0.5;
const controlModule = new Const(controlValue);
const mockModule = new Blend([], controlModule);

// Test
const testFunc = () => {
mockModule.getValue(x, y, z);
};

// Assert
expect(testFunc).to.throw();
});

it("calling getValue with 1 sourceModule throws an error", () => {
// Setup
const x = 10;
const y = 10;
const z = 10;
const sourceValueA = 2;
const sourceModuleA = new Const(sourceValueA);
const controlValue = 0.5;
const controlModule = new Const(controlValue);
const mockModule = new Blend([sourceModuleA], controlModule);

// Test
const testFunc = () => {
mockModule.getValue(x, y, z);
};

// Assert
expect(testFunc).to.throw();
});

it("calling getValue with no controlModule throws an error", () => {
// Setup
const x = 10;
const y = 10;
const z = 10;
const sourceValueA = 2;
const sourceModuleA = new Const(sourceValueA);
const sourceValueB = 2;
const sourceModuleB = new Const(sourceValueB);
const mockModule = new Blend([sourceModuleA, sourceModuleB]);

// Test
const testFunc = () => {
mockModule.getValue(x, y, z);
};

// Assert
expect(testFunc).to.throw();
});

it("calling getValue returns a number", () => {
// Setup
const x = 10;
const y = 10;
const z = 10;
const mockModule = createMockModule();

// Test
const value = mockModule.getValue(x, y, z);

// Assert
expect(value).to.be.ok;
});
});

function createMockModule() {
const sourceValueA = 2;
const sourceModuleA = new Const(sourceValueA);
const sourceValueB = 2;
const sourceModuleB = new Const(sourceValueB);
const controlValue = 0.5;
const controlModule = new Const(controlValue);

return new Blend([sourceModuleA, sourceModuleB], controlModule);
}
194 changes: 194 additions & 0 deletions test/module/selector/select.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { Const } from '@app/module/generator';
import { Select } from '@app/module/selector';

describe('module/selector/select', () => {
it("can construct successfully", () => {
// Setup
const sourceValueA = 2;
const sourceModuleA = new Const(sourceValueA);
const sourceValueB = 2;
const sourceModuleB = new Const(sourceValueB);
const controlValue = 0.5;
const controlModule = new Const(controlValue);
const edgeFalloff = 1;
const lowerBound = -0.5;
const upperBound = 0.5;

// Test
const testFunc = () => {
new Select([sourceModuleA, sourceModuleB], controlModule, edgeFalloff, lowerBound, upperBound);
};

// Assert
expect(testFunc).not.to.throw();
});

it("calling getValue with 0 sourceModules throws an error", () => {
// Setup
const x = 10;
const y = 10;
const z = 10;
const controlValue = 0.5;
const controlModule = new Const(controlValue);
const edgeFalloff = 1;
const lowerBound = -0.5;
const upperBound = 0.5;

const mockModule = new Select([], controlModule, edgeFalloff, lowerBound, upperBound);

// Test
const testFunc = () => {
mockModule.getValue(x, y, z);
};

// Assert
expect(testFunc).to.throw();
});

it("calling getValue with 1 sourceModule throws an error", () => {
// Setup
const x = 10;
const y = 10;
const z = 10;
const sourceValueA = 2;
const sourceModuleA = new Const(sourceValueA);
const controlValue = 0.5;
const controlModule = new Const(controlValue);
const edgeFalloff = 1;
const lowerBound = -0.5;
const upperBound = 0.5;

const mockModule = new Select([sourceModuleA], controlModule, edgeFalloff, lowerBound, upperBound);

// Test
const testFunc = () => {
mockModule.getValue(x, y, z);
};

// Assert
expect(testFunc).to.throw();
});

it("calling getValue with no controlModule throws an error", () => {
// Setup
const x = 10;
const y = 10;
const z = 10;
const sourceValueA = 2;
const sourceModuleA = new Const(sourceValueA);
const sourceValueB = 2;
const sourceModuleB = new Const(sourceValueB);
const edgeFalloff = 1;
const lowerBound = -0.5;
const upperBound = 0.5;

const mockModule = new Select([sourceModuleA, sourceModuleB], null, edgeFalloff, lowerBound, upperBound);

// Test
const testFunc = () => {
mockModule.getValue(x, y, z);
};

// Assert
expect(testFunc).to.throw();
});

it("calling getValue returns a number", () => {
// Setup
const x = 10;
const y = 10;
const z = 10;
const mockModule = createMockModule();

// Test
const value = mockModule.getValue(x, y, z);

// Assert
expect(value).to.be.ok;
});

it("setting edge updates correctly", () => {
// Setup
const newEdgeValue = 0.2;
const mockModule = createMockModule();

// Test
mockModule.edge = newEdgeValue;
const updatedEdgeValue = mockModule.edge;

// Assert
expect(updatedEdgeValue).to.equal(newEdgeValue);
});

it("setting lowerBound updates correctly", () => {
// Setup
const newLowerBoundValue = -1;
const mockModule = createMockModule();

// Test
mockModule.lowerBound = newLowerBoundValue;
const updatedLowerBoundValue = mockModule.lowerBound;

// Assert
expect(updatedLowerBoundValue).to.equal(newLowerBoundValue);
});

it("setting lowerBound to a value higher than upperBound throws an error", () => {
// Setup
const mockModule = createMockModule();
const newLowerBoundValue = mockModule.upperBound + 1;

// Test
const testFunc = () => {
mockModule.lowerBound = newLowerBoundValue;
};

// Assert
expect(testFunc).to.throw();
});

it("setting upperBound updates correctly", () => {
// Setup
const newUpperBoundValue = 2;
const mockModule = createMockModule();

// Test
mockModule.upperBound = newUpperBoundValue;
const updatedUpperBoundValue = mockModule.upperBound;

// Assert
expect(updatedUpperBoundValue).to.equal(newUpperBoundValue);
});

it("setting upperBound to a value higher than lowerBound throws an error", () => {
// Setup
const mockModule = createMockModule();
const newUpperBoundValue = mockModule.lowerBound - 1;

// Test
const testFunc = () => {
mockModule.upperBound = newUpperBoundValue;
};

// Assert
expect(testFunc).to.throw();
});
});

function createMockModule() {
const sourceValueA = 2;
const sourceModuleA = new Const(sourceValueA);
const sourceValueB = 2;
const sourceModuleB = new Const(sourceValueB);
const controlValue = 0.5;
const controlModule = new Const(controlValue);
const edgeFalloff = 1;
const lowerBound = -0.5;
const upperBound = 0.5;

// Test
return new Select([sourceModuleA, sourceModuleB], controlModule, edgeFalloff, lowerBound, upperBound);
}

0 comments on commit b6738b9

Please sign in to comment.