Add more and unify tests for absent object cases

- Unify test data for nonexistence of an object/string and collection.
- Introduce more test through adding missing test data to existing tests.
- Improve logic for checking absence of values to match tests.
- Add missing tests for absent value validation.
- Update documentation to include shared test functionality.
This commit is contained in:
undergroundwires
2022-01-21 22:34:11 +01:00
parent 0e52a99efa
commit 44d79e2c9a
100 changed files with 1380 additions and 976 deletions

View File

@@ -1,5 +1,6 @@
import 'mocha';
import { EscapeDoubleQuotes } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeDefinitions/EscapeDoubleQuotes';
import { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
import { runPipeTests } from './PipeTestRunner';
describe('EscapeDoubleQuotes', () => {
@@ -22,10 +23,10 @@ describe('EscapeDoubleQuotes', () => {
input: '""hello world""',
expectedOutput: '"^"""^""hello world"^"""^""',
},
{
name: 'returns undefined when if input is undefined',
input: undefined,
expectedOutput: undefined,
},
...AbsentStringTestCases.map((testCase) => ({
name: 'returns as it is when if input is missing',
input: testCase.absentValue,
expectedOutput: testCase.absentValue,
})),
]);
});

View File

@@ -2,6 +2,7 @@ import 'mocha';
import { expect } from 'chai';
import { PipeFactory } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeFactory';
import { PipeStub } from '@tests/unit/stubs/PipeStub';
import { AbsentStringTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('PipeFactory', () => {
describe('ctor', () => {
@@ -19,10 +20,21 @@ describe('PipeFactory', () => {
// expect
expect(act).to.throw(expectedError);
});
it('throws when a pipe is undefined', () => {
describe('throws when a pipe is missing', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing pipe in list';
const pipes = [new PipeStub(), absentValue];
// act
const act = () => new PipeFactory(pipes);
// expect
expect(act).to.throw(expectedError);
});
});
it('throws when pipes are null', () => {
// arrange
const expectedError = 'undefined pipe in list';
const pipes = [new PipeStub(), undefined];
const expectedError = 'missing pipes';
const pipes = null;
// act
const act = () => new PipeFactory(pipes);
// expect
@@ -70,44 +82,34 @@ describe('PipeFactory', () => {
function testPipeNameValidation(testRunner: (invalidName: string) => void) {
const testCases = [
{
exceptionBuilder: () => 'empty pipe name',
values: [null, undefined, ''],
},
{
exceptionBuilder: (name: string) => `Pipe name should be camelCase: "${name}"`,
values: [
'PascalCase',
'snake-case',
'includesNumb3rs',
'includes Whitespace',
'noSpec\'ial',
],
},
// Validate missing value
...AbsentStringTestCases.map((testCase) => ({
name: `empty pipe name (${testCase.valueName})`,
value: testCase.absentValue,
expectedError: 'empty pipe name',
})),
// Validate camelCase
...[
'PascalCase',
'snake-case',
'includesNumb3rs',
'includes Whitespace',
'noSpec\'ial',
].map((nonCamelCaseValue) => ({
name: `non camel case value (${nonCamelCaseValue})`,
value: nonCamelCaseValue,
expectedError: `Pipe name should be camelCase: "${nonCamelCaseValue}"`,
})),
];
for (const testCase of testCases) {
for (const invalidName of testCase.values) {
it(`invalid name (${printValue(invalidName)}) throws`, () => {
// arrange
const expectedError = testCase.exceptionBuilder(invalidName);
// act
const act = () => testRunner(invalidName);
// expect
expect(act).to.throw(expectedError);
});
}
}
}
function printValue(value: string) {
switch (value) {
case undefined:
return 'undefined';
case null:
return 'null';
case '':
return 'empty';
default:
return value;
it(testCase.name, () => {
// arrange
const invalidName = testCase.value;
const { expectedError } = testCase;
// act
const act = () => testRunner(invalidName);
// expect
expect(act).to.throw(expectedError);
});
}
}

View File

@@ -5,6 +5,7 @@ import { IPipelineCompiler } from '@/application/Parser/Script/Compiler/Expressi
import { IPipeFactory } from '@/application/Parser/Script/Compiler/Expressions/Pipes/PipeFactory';
import { PipeStub } from '@tests/unit/stubs/PipeStub';
import { PipeFactoryStub } from '@tests/unit/stubs/PipeFactoryStub';
import { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
describe('PipelineCompiler', () => {
describe('compile', () => {
@@ -15,26 +16,16 @@ describe('PipelineCompiler', () => {
expectedError: string;
}
const testCases: ITestCase[] = [
{
name: '"value" is empty',
act: (test) => test.withValue(''),
expectedError: 'undefined value',
},
{
name: '"value" is undefined',
act: (test) => test.withValue(undefined),
expectedError: 'undefined value',
},
{
name: '"pipeline" is empty',
act: (test) => test.withPipeline(''),
expectedError: 'undefined pipeline',
},
{
name: '"pipeline" is undefined',
act: (test) => test.withPipeline(undefined),
expectedError: 'undefined pipeline',
},
...AbsentStringTestCases.map((testCase) => ({
name: `"value" is ${testCase.valueName}`,
act: (test) => test.withValue(testCase.absentValue),
expectedError: 'missing value',
})),
...AbsentStringTestCases.map((testCase) => ({
name: `"pipeline" is ${testCase.valueName}`,
act: (test) => test.withPipeline(testCase.absentValue),
expectedError: 'missing pipeline',
})),
{
name: '"pipeline" does not start with pipe',
act: (test) => test.withPipeline('pipeline |'),