Rework code validation to be bound to a context and not context-independent. It means that the generated code is validated based on different phases during the compilation. This is done by moving validation from `ScriptCode` constructor to a different callable function. It removes duplicate detection for function calls once a call is fully compiled, but still checks for duplicates inside each function body that has inline code. This allows for having duplicates in final scripts (thus relaxing the duplicate detection), e.g., when multiple calls to the same function is made. It fixes non-duplicates (when using common syntax) being misrepresented as duplicate lines. It improves the output of errors, such as printing valid lines, to give more context. This improvement also fixes empty line validation not showing the right empty lines in the error output. Empty line validation shows tabs and whitespaces more clearly. Finally, it adds more tests including tests for existing logic, such as singleton factories.
93 lines
2.4 KiB
TypeScript
93 lines
2.4 KiB
TypeScript
import 'mocha';
|
|
import { expect } from 'chai';
|
|
import { ScriptCode } from '@/domain/ScriptCode';
|
|
import { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
|
|
|
|
describe('ScriptCode', () => {
|
|
describe('code', () => {
|
|
describe('throws with invalid code', () => {
|
|
// arrange
|
|
const testCases = [
|
|
{
|
|
name: 'throws when "execute" and "revert" are same',
|
|
code: {
|
|
execute: 'same',
|
|
revert: 'same',
|
|
},
|
|
expectedError: '(revert): Code itself and its reverting code cannot be the same',
|
|
},
|
|
...AbsentStringTestCases.map((testCase) => ({
|
|
name: `cannot construct with ${testCase.valueName} "execute"`,
|
|
code: {
|
|
execute: testCase.absentValue,
|
|
revert: 'code',
|
|
},
|
|
expectedError: 'missing code',
|
|
})),
|
|
];
|
|
for (const testCase of testCases) {
|
|
it(testCase.name, () => {
|
|
// act
|
|
const act = () => new ScriptCodeBuilder()
|
|
.withExecute(testCase.code.execute)
|
|
.withRevert(testCase.code.revert)
|
|
.build();
|
|
// assert
|
|
expect(act).to.throw(testCase.expectedError);
|
|
});
|
|
}
|
|
});
|
|
describe('sets as expected with valid "execute" or "revert"', () => {
|
|
// arrange
|
|
const testCases = [
|
|
{
|
|
testName: 'code and revert code is given',
|
|
code: 'valid code',
|
|
revertCode: 'valid revert-code',
|
|
},
|
|
{
|
|
testName: 'only code is given but not revert code',
|
|
code: 'valid code',
|
|
revertCode: undefined,
|
|
},
|
|
];
|
|
// assert
|
|
for (const testCase of testCases) {
|
|
it(testCase.testName, () => {
|
|
// act
|
|
const sut = new ScriptCodeBuilder()
|
|
.withExecute(testCase.code)
|
|
.withRevert(testCase.revertCode)
|
|
.build();
|
|
// assert
|
|
expect(sut.execute).to.equal(testCase.code);
|
|
expect(sut.revert).to.equal(testCase.revertCode);
|
|
});
|
|
}
|
|
});
|
|
});
|
|
});
|
|
|
|
class ScriptCodeBuilder {
|
|
public execute = 'default-execute-code';
|
|
|
|
public revert = '';
|
|
|
|
public withExecute(execute: string) {
|
|
this.execute = execute;
|
|
return this;
|
|
}
|
|
|
|
public withRevert(revert: string) {
|
|
this.revert = revert;
|
|
return this;
|
|
}
|
|
|
|
public build(): ScriptCode {
|
|
return new ScriptCode(
|
|
this.execute,
|
|
this.revert,
|
|
);
|
|
}
|
|
}
|