import 'mocha' ; import { expect } from 'chai' ; import { CodeBuilder } from '@/application/Context/State/Code/Generation/CodeBuilder' ; describe ( 'CodeBuilder' , ( ) => { class CodeBuilderConcrete extends CodeBuilder { private commentDelimiter = '//' ; public withCommentDelimiter ( delimiter ) { this . commentDelimiter = delimiter ; return this ; } protected getCommentDelimiter ( ) { return this . commentDelimiter ; } protected writeStandardOut ( text ) { return text ; } } describe ( 'appendLine' , ( ) => { it ( 'when empty appends empty line' , ( ) => { const sut = new CodeBuilderConcrete ( ) ; sut . appendLine ( ) . appendLine ( ) . appendLine ( ) ; expect ( sut . toString ( ) ) . to . equal ( '\n\n' ) ; } ) ; it ( 'when not empty append string in new line' , ( ) => { const sut = new CodeBuilderConcrete ( ) ; const expected = 'str' ; sut . appendLine ( ) . appendLine ( expected ) ; const result = sut . toString ( ) ; const lines = getLines ( result ) ; expect ( lines [ 1 ] ) . to . equal ( 'str' ) ; } ) ; } ) ; it ( 'appendFunction' , ( ) => { const sut = new CodeBuilderConcrete ( ) ; const functionName = 'function' ; const code = 'code' ; sut . appendFunction ( functionName , code ) ; const result = sut . toString ( ) ; expect ( result ) . to . include ( functionName ) ; expect ( result ) . to . include ( code ) ; } ) ; it ( 'appendTrailingHyphensCommentLine' , ( ) => { const commentDelimiter = '//' ; const sut = new CodeBuilderConcrete ( ) . withCommentDelimiter ( commentDelimiter ) ; const totalHyphens = 5 ; const expected = `${ commentDelimiter } ${ '-' . repeat ( totalHyphens ) }` ; sut . appendTrailingHyphensCommentLine ( totalHyphens ) ; const result = sut . toString ( ) ; const lines = getLines ( result ) ; expect ( lines [ 0 ] ) . to . equal ( expected ) ; } ) ; it ( 'appendCommentLine' , ( ) => { const commentDelimiter = '//' ; const sut = new CodeBuilderConcrete ( ) . withCommentDelimiter ( commentDelimiter ) ; const comment = 'comment' ; const expected = `${ commentDelimiter } comment` ; const result = sut . appendCommentLine ( comment ) . toString ( ) ; const lines = getLines ( result ) ; expect ( lines [ 0 ] ) . to . equal ( expected ) ; } ) ; it ( 'appendCommentLineWithHyphensAround' , ( ) => { const commentDelimiter = '//' ; const sut = new CodeBuilderConcrete ( ) . withCommentDelimiter ( commentDelimiter ) ; const sectionName = 'section' ; const totalHyphens = sectionName . length + 3 * 2 ; const expected = `${ commentDelimiter } ---section---` ; const result = sut . appendCommentLineWithHyphensAround ( sectionName , totalHyphens ) . toString ( ) ; const lines = getLines ( result ) ; expect ( lines [ 1 ] ) . to . equal ( expected ) ; } ) ; describe ( 'currentLine' , ( ) => { it ( 'no lines returns zero' , ( ) => { const sut = new CodeBuilderConcrete ( ) ; expect ( sut . currentLine ) . to . equal ( 0 ) ; } ) ; it ( 'single line returns one' , ( ) => { const sut = new CodeBuilderConcrete ( ) ; sut . appendLine ( ) ; expect ( sut . currentLine ) . to . equal ( 1 ) ; } ) ; it ( 'multiple lines returns as expected' , ( ) => { const sut = new CodeBuilderConcrete ( ) ; sut . appendLine ( '1' ) . appendCommentLine ( '2' ) . appendLine ( ) ; expect ( sut . currentLine ) . to . equal ( 3 ) ; } ) ; it ( 'multiple lines in code' , ( ) => { const sut = new CodeBuilderConcrete ( ) ; sut . appendLine ( 'hello\ncode-here\nwith-3-lines' ) ; expect ( sut . currentLine ) . to . equal ( 3 ) ; } ) ; } ) ; } ) ; function getLines ( text ) { return text . split ( /\r\n|\r|\n/ ) ; }