Files
privacy.sexy/tests/unit/application/Parser/Script/Compiler/Function/SharedFunctionCollection.spec.ts
undergroundwires 60c80611ea add module alias '@tests/'
Alias would remove unnecessary repetitions and less relative paths make changes easier when moving around files. This commit cleans also up some relative paths ('../../../') by using the alias and orders imports. It updates both path alias in tsconfig and module alias in Vue CLI's bundler (vuejs/vue-cli#2398).
2021-04-15 18:34:40 +02:00

75 lines
2.9 KiB
TypeScript

import 'mocha';
import { expect } from 'chai';
import { SharedFunctionCollection } from '@/application/Parser/Script/Compiler/Function/SharedFunctionCollection';
import { SharedFunctionStub } from '@tests/unit/stubs/SharedFunctionStub';
describe('SharedFunctionCollection', () => {
describe('addFunction', () => {
it('throws if function is undefined', () => {
// arrange
const expectedError = 'undefined function';
const func = undefined;
const sut = new SharedFunctionCollection();
// act
const act = () => sut.addFunction(func);
// assert
expect(act).to.throw(expectedError);
});
it('throws if function with same name already exists', () => {
// arrange
const functionName = 'duplicate-function';
const expectedError = `function with name ${functionName} already exists`;
const func = new SharedFunctionStub()
.withName('duplicate-function');
const sut = new SharedFunctionCollection();
sut.addFunction(func);
// act
const act = () => sut.addFunction(func);
// assert
expect(act).to.throw(expectedError);
});
});
describe('getFunctionByName', () => {
it('throws if name is undefined', () => {
// arrange
const expectedError = 'undefined function name';
const invalidValues = [ undefined, '' ];
const sut = new SharedFunctionCollection();
for (const invalidValue of invalidValues) {
const name = invalidValue;
// act
const act = () => sut.getFunctionByName(name);
// assert
expect(act).to.throw(expectedError);
}
});
it('throws if function does not exist', () => {
// arrange
const name = 'unique-name';
const expectedError = `called function is not defined "${name}"`;
const func = new SharedFunctionStub()
.withName('unexpected-name');
const sut = new SharedFunctionCollection();
sut.addFunction(func);
// act
const act = () => sut.getFunctionByName(name);
// assert
expect(act).to.throw(expectedError);
});
it('returns existing function', () => {
// arrange
const name = 'expected-function-name';
const expected = new SharedFunctionStub()
.withName(name);
const sut = new SharedFunctionCollection();
sut.addFunction(new SharedFunctionStub().withName('another-function-name'));
sut.addFunction(expected);
// act
const actual = sut.getFunctionByName(name);
// assert
expect(actual).to.equal(expected);
});
});
});