import 'mocha' ; import { expect } from 'chai' ; import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition' ; import { IExpressionParser } from '@/application/Parser/Script/Compiler/Expressions/Parser/IExpressionParser' ; import { FunctionCallArgumentCollectionStub } from '@tests/unit/stubs/FunctionCallArgumentCollectionStub' ; import { ExpressionEvaluationContextStub } from '@tests/unit/stubs/ExpressionEvaluationContextStub' ; import { PipelineCompilerStub } from '@tests/unit/stubs/PipelineCompilerStub' ; export class SyntaxParserTestsRunner { constructor ( private readonly sut ) { } public expectPosition ( ... testCases ) { for ( const testCase of testCases ) { it ( testCase . name , ( ) => { const expressions = this . sut . findExpressions ( testCase . code ) ; const actual = expressions . map ( ( e ) => e . position ) ; expect ( actual ) . to . deep . equal ( testCase . expected ) ; } ) ; } return this ; } public expectResults ( ... testCases ) { for ( const testCase of testCases ) { it ( testCase . name , ( ) => { const args = testCase . args ( new FunctionCallArgumentCollectionStub ( ) ) ; const context = new ExpressionEvaluationContextStub ( ) . withArgs ( args ) ; const expressions = this . sut . findExpressions ( testCase . code ) ; const actual = expressions . map ( ( e ) => e . evaluate ( context ) ) ; expect ( actual ) . to . deep . equal ( testCase . expected ) ; } ) ; } return this ; } public expectPipeHits ( data ) { for ( const validPipePart of PipeTestCases . ValidValues ) { this . expectHitPipePart ( validPipePart , data ) ; } for ( const invalidPipePart of PipeTestCases . InvalidValues ) { this . expectMissPipePart ( invalidPipePart , data ) ; } } private expectHitPipePart ( pipeline , data ) { it ( `"${ pipeline }" hits` , ( ) => { const expectedPipePart = pipeline . trim ( ) ; const code = data . codeBuilder ( pipeline ) ; const args = new FunctionCallArgumentCollectionStub ( ) . withArgument ( data . parameterName , data . parameterValue ) ; const pipelineCompiler = new PipelineCompilerStub ( ) ; const context = new ExpressionEvaluationContextStub ( ) . withPipelineCompiler ( pipelineCompiler ) . withArgs ( args ) ; const expressions = this . sut . findExpressions ( code ) ; expressions [ 0 ] . evaluate ( context ) ; expect ( expressions ) . has . lengthOf ( 1 ) ; expect ( pipelineCompiler . compileHistory ) . has . lengthOf ( 1 ) ; const actualPipeNames = pipelineCompiler . compileHistory [ 0 ] . pipeline ; const actualValue = pipelineCompiler . compileHistory [ 0 ] . value ; expect ( actualPipeNames ) . to . equal ( expectedPipePart ) ; expect ( actualValue ) . to . equal ( data . parameterValue ) ; } ) ; } private expectMissPipePart ( pipeline , data ) { it ( `"${ pipeline }" misses` , ( ) => { const args = new FunctionCallArgumentCollectionStub ( ) . withArgument ( data . parameterName , data . parameterValue ) ; const pipelineCompiler = new PipelineCompilerStub ( ) ; const context = new ExpressionEvaluationContextStub ( ) . withPipelineCompiler ( pipelineCompiler ) . withArgs ( args ) ; const code = data . codeBuilder ( pipeline ) ; const expressions = this . sut . findExpressions ( code ) ; expressions [ 0 ] ?. evaluate ( context ) ; expect ( pipelineCompiler . compileHistory ) . has . lengthOf ( 0 ) ; } ) ; } } interface IExpectResultTestCase { name : string ; code : string ; args : ( builder : FunctionCallArgumentCollectionStub ) => FunctionCallArgumentCollectionStub ; expected : readonly string [ ] ; } interface IExpectPositionTestCase { name : string ; code : string ; expected : readonly ExpressionPosition [ ] ; } interface IExpectPipeHitTestData { codeBuilder : ( pipeline : string ) => string ; parameterName : string ; parameterValue : string ; } const PipeTestCases = { ValidValues : [ ' | pipe1' , ' |pipe1' , '|pipe1' , ' |pipe1' , '   |   pipe1' , ' | pipe1 | pipe2' , '| pipe1|pipe2' , '|pipe1|pipe2' , ' |pipe1 |pipe2' , '| pipe1 | pipe2| pipe3 |pipe4' , '| pip€' , '| pip{e} ' , ] , InvalidValues : [ ' pipe1  |pipe2' , ' pipe1' , ] , } ;