diff --git a/src/module/selector/blend.ts b/src/module/selector/blend.ts index 26b4178..d424738 100644 --- a/src/module/selector/blend.ts +++ b/src/module/selector/blend.ts @@ -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)!'); } diff --git a/src/module/selector/select.ts b/src/module/selector/select.ts index cd7e7b9..a5e7167 100644 --- a/src/module/selector/select.ts +++ b/src/module/selector/select.ts @@ -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)!'); } diff --git a/test/module/selector/blend.test.ts b/test/module/selector/blend.test.ts new file mode 100644 index 0000000..9cd461d --- /dev/null +++ b/test/module/selector/blend.test.ts @@ -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); +} diff --git a/test/module/selector/select.test.ts b/test/module/selector/select.test.ts new file mode 100644 index 0000000..6344a8d --- /dev/null +++ b/test/module/selector/select.test.ts @@ -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); +}