import 'mocha' ; import { expect } from 'chai' ; import { ExpressionRegexBuilder } from '@/application/Parser/Script/Compiler/Expressions/Parser/Regex/ExpressionRegexBuilder' ; describe ( 'ExpressionRegexBuilder' , ( ) => { describe ( 'expectCharacters' , ( ) => { describe ( 'escape single as expected' , ( ) => { const charactersToEscape = [ '.' , '$' ] ; for ( const character of charactersToEscape ) { it ( character , ( ) => { runRegExTest ( ( act ) => act . expectCharacters ( character ) , `\\${ character }` ) ; } ) ; } } ) ; it ( 'escapes multiple as expected' , ( ) => { runRegExTest ( ( act ) => act . expectCharacters ( '.I have no $$.' ) , '\\.I have no \\$\\$\\.' ) ; } ) ; it ( 'adds as expected' , ( ) => { runRegExTest ( ( act ) => act . expectCharacters ( 'return as it is' ) , 'return as it is' ) ; } ) ; } ) ; it ( 'expectOneOrMoreWhitespaces' , ( ) => { runRegExTest ( ( act ) => act . expectOneOrMoreWhitespaces ( ) , '\\s+' ) ; } ) ; it ( 'matchPipeline' , ( ) => { runRegExTest ( ( act ) => act . matchPipeline ( ) , '\\s*(\\|\\s*.+?)?' ) ; } ) ; it ( 'matchUntilFirstWhitespace' , ( ) => { runRegExTest ( ( act ) => act . matchUntilFirstWhitespace ( ) , '([^|\\s]+)' ) ; } ) ; it ( 'matchAnythingExceptSurroundingWhitespaces' , ( ) => { runRegExTest ( ( act ) => act . matchAnythingExceptSurroundingWhitespaces ( ) , '\\s*(.+?)\\s*' ) ; } ) ; it ( 'expectExpressionStart' , ( ) => { runRegExTest ( ( act ) => act . expectExpressionStart ( ) , '{{\\s*' ) ; } ) ; it ( 'expectExpressionEnd' , ( ) => { runRegExTest ( ( act ) => act . expectExpressionEnd ( ) , '\\s*}}' ) ; } ) ; describe ( 'buildRegExp' , ( ) => { it ( 'sets global flag' , ( ) => { const expected = 'g' ; const sut = new ExpressionRegexBuilder ( ) . expectOneOrMoreWhitespaces ( ) ; const actual = sut . buildRegExp ( ) . flags ; expect ( actual ) . to . equal ( expected ) ; } ) ; describe ( 'can combine multiple parts' , ( ) => { it ( 'with' , ( ) => { runRegExTest ( ( sut ) => sut . expectExpressionStart ( ) . expectCharacters ( 'with' ) . expectOneOrMoreWhitespaces ( ) . expectCharacters ( '$' ) . matchUntilFirstWhitespace ( ) . expectExpressionEnd ( ) . matchAnythingExceptSurroundingWhitespaces ( ) . expectExpressionStart ( ) . expectCharacters ( 'end' ) . expectExpressionEnd ( ) , '{{\\s*with\\s+\\$([^|\\s]+)\\s*}}\\s*(.+?)\\s*{{\\s*end\\s*}}' ) ; } ) ; it ( 'scoped substitution' , ( ) => { runRegExTest ( ( sut ) => sut . expectExpressionStart ( ) . expectCharacters ( '.' ) . matchPipeline ( ) . expectExpressionEnd ( ) , '{{\\s*\\.\\s*(\\|\\s*.+?)?\\s*}}' ) ; } ) ; it ( 'parameter substitution' , ( ) => { runRegExTest ( ( sut ) => sut . expectExpressionStart ( ) . expectCharacters ( '$' ) . matchUntilFirstWhitespace ( ) . matchPipeline ( ) . expectExpressionEnd ( ) , '{{\\s*\\$([^|\\s]+)\\s*(\\|\\s*.+?)?\\s*}}' ) ; } ) ; } ) ; } ) ; } ) ; function runRegExTest ( act , expected ) { const sut = new ExpressionRegexBuilder ( ) ; const actual = act ( sut ) . buildRegExp ( ) . source ; expect ( actual ) . to . equal ( expected ) ; }