- Use better error messages with more context. - Unify their validation logic and share tests. - Validate also type of the name. - Refactor node (Script/Category) parser tests for easier future changes and cleaner test code (using `TestBuilder` to do dirty work in unified way). - Add more tests. Custom `Error` properties are compared manually due to `chai` not supporting deep equality checks (chaijs/chai#1065, chaijs/chai#1405).
32 lines
944 B
TypeScript
32 lines
944 B
TypeScript
import { IEnumParser } from '@/application/Common/Enum';
|
|
|
|
export class EnumParserStub<T> implements IEnumParser<T> {
|
|
private readonly scenarios = new Array<{
|
|
inputName: string, inputValue: string, outputValue: T }>();
|
|
|
|
private defaultValue: T;
|
|
|
|
public setup(inputName: string, inputValue: string, outputValue: T) {
|
|
this.scenarios.push({ inputName, inputValue, outputValue });
|
|
return this;
|
|
}
|
|
|
|
public setupDefaultValue(outputValue: T) {
|
|
this.defaultValue = outputValue;
|
|
return this;
|
|
}
|
|
|
|
public parseEnum(value: string, propertyName: string): T {
|
|
const scenario = this.scenarios.find(
|
|
(s) => s.inputName === propertyName && s.inputValue === value,
|
|
);
|
|
if (scenario) {
|
|
return scenario.outputValue;
|
|
}
|
|
if (this.defaultValue !== undefined) {
|
|
return this.defaultValue;
|
|
}
|
|
throw new Error(`Don't know now what to return from ${EnumParserStub.name}, forgot to set-up?`);
|
|
}
|
|
}
|