import 'mocha' ; import { expect } from 'chai' ; import { FunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/Function/Call/Argument/FunctionCallArgumentCollection' ; import { FunctionCallArgumentStub } from '@tests/unit/stubs/FunctionCallArgumentStub' ; describe ( 'FunctionCallArgumentCollection' , ( ) => { describe ( 'addArgument' , ( ) => { it ( 'throws if argument is undefined' , ( ) => { const errorMessage = 'undefined argument' ; const arg = undefined ; const sut = new FunctionCallArgumentCollection ( ) ; const act = ( ) => sut . addArgument ( arg ) ; expect ( act ) . to . throw ( errorMessage ) ; } ) ; it ( 'throws if parameter value is already provided' , ( ) => { const duplicateParameterName = 'duplicateParam' ; const errorMessage = `argument value for parameter ${ duplicateParameterName } is already provided` ; const arg1 = new FunctionCallArgumentStub ( ) . withParameterName ( duplicateParameterName ) ; const arg2 = new FunctionCallArgumentStub ( ) . withParameterName ( duplicateParameterName ) ; const sut = new FunctionCallArgumentCollection ( ) ; sut . addArgument ( arg1 ) ; const act = ( ) => sut . addArgument ( arg2 ) ; expect ( act ) . to . throw ( errorMessage ) ; } ) ; } ) ; describe ( 'getAllParameterNames' , ( ) => { it ( 'returns as expected' , ( ) => { const testCases = [ { name : 'no args' , args : [ ] , expected : [ ] , } , { name : 'with some args' , args : [ new FunctionCallArgumentStub ( ) . withParameterName ( 'a-param-name' ) , new FunctionCallArgumentStub ( ) . withParameterName ( 'b-param-name' ) ] , expected : [ 'a-param-name' , 'b-param-name' ] , } ] ; for ( const testCase of testCases ) { it ( testCase . name , ( ) => { const sut = new FunctionCallArgumentCollection ( ) ; for ( const arg of testCase . args ) { sut . addArgument ( arg ) ; } const actual = sut . getAllParameterNames ( ) ; expect ( actual ) . to . equal ( testCase . expected ) ; } ) ; } } ) ; } ) ; describe ( 'getArgument' , ( ) => { it ( 'throws if parameter name is undefined' , ( ) => { const expectedError = 'undefined parameter name' ; const undefinedValues = [ '' , undefined ] ; for ( const undefinedValue of undefinedValues ) { const sut = new FunctionCallArgumentCollection ( ) ; const act = ( ) => sut . getArgument ( undefinedValue ) ; expect ( act ) . to . throw ( expectedError ) ; } } ) ; it ( 'throws if argument does not exist' , ( ) => { const parameterName = 'nonExistingParam' ; const expectedError = `parameter does not exist: ${ parameterName }` ; const sut = new FunctionCallArgumentCollection ( ) ; const act = ( ) => sut . getArgument ( parameterName ) ; expect ( act ) . to . throw ( expectedError ) ; } ) ; it ( 'returns argument as expected' , ( ) => { const expected = new FunctionCallArgumentStub ( ) . withParameterName ( 'expectedName' ) . withArgumentValue ( 'expectedValue' ) ; const sut = new FunctionCallArgumentCollection ( ) ; sut . addArgument ( expected ) ; const actual = sut . getArgument ( expected . parameterName ) ; expect ( actual ) . to . equal ( expected ) ; } ) ; } ) ; describe ( 'hasArgument' , ( ) => { it ( 'throws if parameter name is undefined' , ( ) => { const expectedError = 'undefined parameter name' ; const undefinedValues = [ '' , undefined ] ; for ( const undefinedValue of undefinedValues ) { const sut = new FunctionCallArgumentCollection ( ) ; const act = ( ) => sut . hasArgument ( undefinedValue ) ; expect ( act ) . to . throw ( expectedError ) ; } } ) ; describe ( 'returns as expected' , ( ) => { const testCases = [ { name : 'argument exists' , parameter : 'existing-parameter-name' , args : [ new FunctionCallArgumentStub ( ) . withParameterName ( 'existing-parameter-name' ) , new FunctionCallArgumentStub ( ) . withParameterName ( 'unrelated-parameter-name' ) , ] , expected : true , } , { name : 'argument does not exist' , parameter : 'not-existing-parameter-name' , args : [ new FunctionCallArgumentStub ( ) . withParameterName ( 'unrelated-parameter-name-b' ) , new FunctionCallArgumentStub ( ) . withParameterName ( 'unrelated-parameter-name-a' ) , ] , expected : false , } ] ; for ( const testCase of testCases ) { it ( `"${ testCase . name }" returns "${ testCase . expected }"` , ( ) => { const sut = new FunctionCallArgumentCollection ( ) ; for ( const arg of testCase . args ) { sut . addArgument ( arg ) ; } const actual = sut . hasArgument ( testCase . parameter ) ; expect ( actual ) . to . equal ( testCase . expected ) ; } ) ; } } ) ; } ) ; } ) ;