import 'mocha' ; import { expect } from 'chai' ; import { ScriptingDefinition } from '@/domain/ScriptingDefinition' ; import { ScriptingLanguage } from '@/domain/ScriptingLanguage' ; import { getEnumValues } from '@/application/Common/Enum' ; describe ( 'ScriptingDefinition' , ( ) => { describe ( 'language' , ( ) => { describe ( 'sets as expected' , ( ) => { const expectedValues = getEnumValues ( ScriptingLanguage ) ; expectedValues . forEach ( ( expected ) => { it ( ScriptingLanguage [ expected ] , ( ) => { const sut = new ScriptingDefinitionBuilder ( ) . withLanguage ( expected ) . build ( ) ; expect ( sut . language ) . to . equal ( expected ) ; } ) ; } ) ; } ) ; it ( 'throws if unknown' , ( ) => { const unknownValue = 666 ; const errorMessage = `unsupported language: ${ unknownValue }` ; const act = ( ) => new ScriptingDefinitionBuilder ( ) . withLanguage ( unknownValue ) . build ( ) ; expect ( act ) . to . throw ( errorMessage ) ; } ) ; } ) ; describe ( 'fileExtension' , ( ) => { describe ( 'returns expected for each language' , ( ) => { const testCases = new Map < ScriptingLanguage , string > ( [ [ ScriptingLanguage . batchfile , 'bat' ] , [ ScriptingLanguage . shellscript , 'sh' ] , ] ) ; for ( const test of testCases . entries ( ) ) { const language = test [ 0 ] ; const expectedExtension = test [ 1 ] ; it ( `${ ScriptingLanguage [ language ] } has ${ expectedExtension }` , ( ) => { const sut = new ScriptingDefinitionBuilder ( ) . withLanguage ( language ) . build ( ) ; expect ( sut . fileExtension , expectedExtension ) ; } ) ; } } ) ; } ) ; describe ( 'startCode' , ( ) => { it ( 'sets as expected' , ( ) => { const expected = 'REM start-code' ; const sut = new ScriptingDefinitionBuilder ( ) . withStartCode ( expected ) . build ( ) ; expect ( sut . startCode ) . to . equal ( expected ) ; } ) ; it ( 'throws when undefined' , ( ) => { const expectedError = 'undefined start code' ; const undefinedValues = [ '' , undefined ] ; for ( const undefinedValue of undefinedValues ) { const act = ( ) => new ScriptingDefinitionBuilder ( ) . withStartCode ( undefinedValue ) . build ( ) ; expect ( act ) . to . throw ( expectedError ) ; } } ) ; } ) ; describe ( 'endCode' , ( ) => { it ( 'sets as expected' , ( ) => { const expected = 'REM end-code' ; const sut = new ScriptingDefinitionBuilder ( ) . withEndCode ( expected ) . build ( ) ; expect ( sut . endCode ) . to . equal ( expected ) ; } ) ; it ( 'throws when undefined' , ( ) => { const expectedError = 'undefined end code' ; const undefinedValues = [ '' , undefined ] ; for ( const undefinedValue of undefinedValues ) { const act = ( ) => new ScriptingDefinitionBuilder ( ) . withEndCode ( undefinedValue ) . build ( ) ; expect ( act ) . to . throw ( expectedError ) ; } } ) ; } ) ; } ) ; class ScriptingDefinitionBuilder { private language = ScriptingLanguage . shellscript ; private startCode = 'REM start-code' ; private endCode = 'REM end-code' ; public withLanguage ( language ) { this . language = language ; return this ; } public withStartCode ( startCode ) { this . startCode = startCode ; return this ; } public withEndCode ( endCode ) { this . endCode = endCode ; return this ; } public build ( ) { return new ScriptingDefinition ( this . language , this . startCode , this . endCode ) ; } }