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

@@ -10,20 +10,24 @@ import { ExpressionEvaluationContextStub } from '@tests/unit/stubs/ExpressionEva
import { IPipelineCompiler } from '@/application/Parser/Script/Compiler/Expressions/Pipes/IPipelineCompiler';
import { PipelineCompilerStub } from '@tests/unit/stubs/PipelineCompilerStub';
import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection';
import { AbsentObjectTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
import { IExpressionEvaluationContext } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionEvaluationContext';
describe('Expression', () => {
describe('ctor', () => {
describe('position', () => {
it('throws if undefined', () => {
// arrange
const expectedError = 'undefined position';
const position = undefined;
// act
const act = () => new ExpressionBuilder()
.withPosition(position)
.build();
// assert
expect(act).to.throw(expectedError);
describe('throws when missing', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing position';
const position = absentValue;
// act
const act = () => new ExpressionBuilder()
.withPosition(position)
.build();
// assert
expect(act).to.throw(expectedError);
});
});
it('sets as expected', () => {
// arrange
@@ -37,17 +41,19 @@ describe('Expression', () => {
});
});
describe('parameters', () => {
it('defaults to empty array if undefined', () => {
// arrange
const parameters = undefined;
// act
const actual = new ExpressionBuilder()
.withParameters(parameters)
.build();
// assert
expect(actual.parameters);
expect(actual.parameters.all);
expect(actual.parameters.all.length).to.equal(0);
describe('defaults to empty array if absent', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const parameters = absentValue;
// act
const actual = new ExpressionBuilder()
.withParameters(parameters)
.build();
// assert
expect(actual.parameters);
expect(actual.parameters.all);
expect(actual.parameters.all.length).to.equal(0);
});
});
it('sets as expected', () => {
// arrange
@@ -63,37 +69,44 @@ describe('Expression', () => {
});
});
describe('evaluator', () => {
it('throws if undefined', () => {
// arrange
const expectedError = 'undefined evaluator';
const evaluator = undefined;
// act
const act = () => new ExpressionBuilder()
.withEvaluator(evaluator)
.build();
// assert
expect(act).to.throw(expectedError);
describe('throws if missing', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing evaluator';
const evaluator = absentValue;
// act
const act = () => new ExpressionBuilder()
.withEvaluator(evaluator)
.build();
// assert
expect(act).to.throw(expectedError);
});
});
});
});
describe('evaluate', () => {
describe('throws with invalid arguments', () => {
const testCases = [
{
name: 'throws if arguments is undefined',
context: undefined,
expectedError: 'undefined context',
},
const testCases: readonly {
name: string,
context: IExpressionEvaluationContext,
expectedError: string,
sutBuilder?: (builder: ExpressionBuilder) => ExpressionBuilder,
}[] = [
...AbsentObjectTestCases.map((testCase) => ({
name: `throws if arguments is ${testCase.valueName}`,
context: testCase.absentValue,
expectedError: 'missing context',
})),
{
name: 'throws when some of the required args are not provided',
sut: (i: ExpressionBuilder) => i.withParameterNames(['a', 'b', 'c'], false),
sutBuilder: (i: ExpressionBuilder) => i.withParameterNames(['a', 'b', 'c'], false),
context: new ExpressionEvaluationContextStub()
.withArgs(new FunctionCallArgumentCollectionStub().withArgument('b', 'provided')),
expectedError: 'argument values are provided for required parameters: "a", "c"',
},
{
name: 'throws when none of the required args are not provided',
sut: (i: ExpressionBuilder) => i.withParameterNames(['a', 'b'], false),
sutBuilder: (i: ExpressionBuilder) => i.withParameterNames(['a', 'b'], false),
context: new ExpressionEvaluationContextStub()
.withArgs(new FunctionCallArgumentCollectionStub().withArgument('c', 'unrelated')),
expectedError: 'argument values are provided for required parameters: "a", "b"',
@@ -103,8 +116,8 @@ describe('Expression', () => {
it(testCase.name, () => {
// arrange
const sutBuilder = new ExpressionBuilder();
if (testCase.sut) {
testCase.sut(sutBuilder);
if (testCase.sutBuilder) {
testCase.sutBuilder(sutBuilder);
}
const sut = sutBuilder.build();
// act

View File

@@ -5,19 +5,23 @@ import { IReadOnlyFunctionCallArgumentCollection } from '@/application/Parser/Sc
import { IPipelineCompiler } from '@/application/Parser/Script/Compiler/Expressions/Pipes/IPipelineCompiler';
import { FunctionCallArgumentCollectionStub } from '@tests/unit/stubs/FunctionCallArgumentCollectionStub';
import { PipelineCompilerStub } from '@tests/unit/stubs/PipelineCompilerStub';
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('ExpressionEvaluationContext', () => {
describe('ctor', () => {
describe('args', () => {
it('throws if args are undefined', () => {
// arrange
const expectedError = 'undefined args';
const builder = new ExpressionEvaluationContextBuilder()
.withArgs(undefined);
// act
const act = () => builder.build();
// assert
expect(act).throw(expectedError);
describe('throws if args is missing', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing args, send empty collection instead.';
const args = absentValue;
// act
const act = () => new ExpressionEvaluationContextBuilder()
.withArgs(args)
.build();
// assert
expect(act).throw(expectedError);
});
});
it('sets as expected', () => {
// arrange

View File

@@ -5,30 +5,21 @@ import { IExpressionParser } from '@/application/Parser/Script/Compiler/Expressi
import { ExpressionStub } from '@tests/unit/stubs/ExpressionStub';
import { ExpressionParserStub } from '@tests/unit/stubs/ExpressionParserStub';
import { FunctionCallArgumentCollectionStub } from '@tests/unit/stubs/FunctionCallArgumentCollectionStub';
import { itEachAbsentObjectValue, itEachAbsentStringValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('ExpressionsCompiler', () => {
describe('compileExpressions', () => {
describe('returns code when it is empty or undefined', () => {
// arrange
const testCases = [{
name: 'empty',
value: '',
}, {
name: 'undefined',
value: undefined,
},
];
for (const test of testCases) {
it(`given ${test.name}`, () => {
const expected = test.value;
const sut = new SystemUnderTest();
const args = new FunctionCallArgumentCollectionStub();
// act
const value = sut.compileExpressions(test.value, args);
// assert
expect(value).to.equal(expected);
});
}
describe('returns code when it is absent', () => {
itEachAbsentStringValue((absentValue) => {
// arrange
const expected = absentValue;
const sut = new SystemUnderTest();
const args = new FunctionCallArgumentCollectionStub();
// act
const value = sut.compileExpressions(absentValue, args);
// assert
expect(value).to.equal(expected);
});
});
describe('combines expressions as expected', () => {
// arrange
@@ -100,16 +91,18 @@ describe('ExpressionsCompiler', () => {
expect(expressions[1].callHistory).to.have.lengthOf(1);
expect(expressions[1].callHistory[0].args).to.equal(expected);
});
it('throws if arguments is undefined', () => {
// arrange
const expectedError = 'undefined args, send empty collection instead';
const args = undefined;
const expressionParserMock = new ExpressionParserStub();
const sut = new SystemUnderTest(expressionParserMock);
// act
const act = () => sut.compileExpressions('code', args);
// assert
expect(act).to.throw(expectedError);
describe('throws if arguments is missing', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing args, send empty collection instead.';
const args = absentValue;
const expressionParserMock = new ExpressionParserStub();
const sut = new SystemUnderTest(expressionParserMock);
// act
const act = () => sut.compileExpressions('code', args);
// assert
expect(act).to.throw(expectedError);
});
});
});
describe('throws when expected argument is not provided but used in code', () => {

View File

@@ -4,18 +4,30 @@ import { IExpression } from '@/application/Parser/Script/Compiler/Expressions/Ex
import { IExpressionParser } from '@/application/Parser/Script/Compiler/Expressions/Parser/IExpressionParser';
import { CompositeExpressionParser } from '@/application/Parser/Script/Compiler/Expressions/Parser/CompositeExpressionParser';
import { ExpressionStub } from '@tests/unit/stubs/ExpressionStub';
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('CompositeExpressionParser', () => {
describe('ctor', () => {
it('throws if one of the parsers is undefined', () => {
it('throws if null parsers given', () => {
// arrange
const expectedError = 'undefined leaf';
const parsers: readonly IExpressionParser[] = [undefined, mockParser()];
const expectedError = 'missing leafs';
const parsers = null;
// act
const act = () => new CompositeExpressionParser(parsers);
// assert
expect(act).to.throw(expectedError);
});
describe('throws if one of the parsers is undefined', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing leaf';
const parsers: readonly IExpressionParser[] = [absentValue, mockParser()];
// act
const act = () => new CompositeExpressionParser(parsers);
// assert
expect(act).to.throw(expectedError);
});
});
});
describe('findExpressions', () => {
describe('returns result from parsers as expected', () => {

View File

@@ -4,32 +4,20 @@ import { ExpressionEvaluator } from '@/application/Parser/Script/Compiler/Expres
import { IPrimitiveExpression, RegexParser } from '@/application/Parser/Script/Compiler/Expressions/Parser/Regex/RegexParser';
import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition';
import { FunctionParameterStub } from '@tests/unit/stubs/FunctionParameterStub';
import { itEachAbsentStringValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('RegexParser', () => {
describe('findExpressions', () => {
describe('throws when code is unexpected', () => {
// arrange
const testCases = [
{
name: 'undefined',
value: undefined,
expectedError: 'undefined code',
},
{
name: 'empty',
value: '',
expectedError: 'undefined code',
},
];
for (const testCase of testCases) {
it(`given ${testCase.name}`, () => {
const sut = new RegexParserConcrete(/unimportant/);
// act
const act = () => sut.findExpressions(testCase.value);
// assert
expect(act).to.throw(testCase.expectedError);
});
}
describe('throws when code is absent', () => {
itEachAbsentStringValue((absentValue) => {
// arrange
const expectedError = 'missing code';
const sut = new RegexParserConcrete(/unimportant/);
// act
const act = () => sut.findExpressions(absentValue);
// assert
expect(act).to.throw(expectedError);
});
});
it('throws when position is invalid', () => {
// arrange

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 |'),

View File

@@ -1,6 +1,7 @@
import 'mocha';
import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition';
import { WithParser } from '@/application/Parser/Script/Compiler/Expressions/SyntaxParsers/WithParser';
import { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
import { SyntaxParserTestsRunner } from './SyntaxParserTestsRunner';
describe('WithParser', () => {
@@ -84,20 +85,13 @@ describe('WithParser', () => {
describe('renders scope conditionally', () => {
describe('does not render scope if argument is undefined', () => {
runner.expectResults(
{
name: 'does not render when value is undefined',
...AbsentStringTestCases.map((testCase) => ({
name: `does not render when value is "${testCase.valueName}"`,
code: '{{ with $parameter }}dark{{ end }} ',
args: (args) => args
.withArgument('parameter', undefined),
.withArgument('parameter', testCase.absentValue),
expected: [''],
},
{
name: 'does not render when value is empty',
code: '{{ with $parameter }}dark {{.}}{{ end }}',
args: (args) => args
.withArgument('parameter', ''),
expected: [''],
},
})),
{
name: 'does not render when argument is not provided',
code: '{{ with $parameter }}dark{{ end }}',