import 'mocha' ; import { expect } from 'chai' ; import { getEnumNames , getEnumValues , createEnumParser , assertInRange , } from '@/application/Common/Enum' ; import { scrambledEqual } from '@/application/Common/Array' ; import { EnumRangeTestRunner } from './EnumRangeTestRunner' ; describe ( 'Enum' , ( ) => { describe ( 'createEnumParser' , ( ) => { enum ParsableEnum { Value1 , value2 } describe ( 'parses as expected' , ( ) => { const testCases = [ { name : 'case insensitive' , value : 'vALuE1' , expected : ParsableEnum . Value1 , } , { name : 'exact match' , value : 'value2' , expected : ParsableEnum . value2 , } , ] ; for ( const testCase of testCases ) { it ( testCase . name , ( ) => { const parser = createEnumParser ( ParsableEnum ) ; const actual = parser . parseEnum ( testCase . value , 'non-important' ) ; expect ( actual ) . to . equal ( testCase . expected ) ; } ) ; } } ) ; describe ( 'throws as expected' , ( ) => { const enumName = 'ParsableEnum' ; const testCases = [ { name : 'undefined' , value : undefined , expectedError : `undefined ${ enumName }` , } , { name : 'empty' , value : '' , expectedError : `undefined ${ enumName }` , } , { name : 'out of range' , value : 'value3' , expectedError : `unknown ${ enumName }: "value3"` , } , { name : 'out of range' , value : 'value3' , expectedError : `unknown ${ enumName }: "value3"` , } , { name : 'unexpected type' , value : 55 as never , expectedError : `unexpected type of ${ enumName }: "number"` , } , ] ; for ( const testCase of testCases ) { it ( testCase . name , ( ) => { const parser = createEnumParser ( ParsableEnum ) ; const act = ( ) => parser . parseEnum ( testCase . value , enumName ) ; expect ( act ) . to . throw ( testCase . expectedError ) ; } ) ; } } ) ; } ) ; describe ( 'getEnumNames' , ( ) => { it ( 'parses as expected' , ( ) => { enum TestEnum { TestValue1 , testValue2 , testvalue3 , TESTVALUE4 } const expected = [ 'TestValue1' , 'testValue2' , 'testvalue3' , 'TESTVALUE4' ] ; const actual = getEnumNames ( TestEnum ) ; expect ( scrambledEqual ( expected , actual ) ) ; } ) ; } ) ; describe ( 'getEnumValues' , ( ) => { it ( 'parses as expected' , ( ) => { enum TestEnum { Red , Green , Blue } const expected = [ TestEnum . Red , TestEnum . Green , TestEnum . Blue ] ; const actual = getEnumValues ( TestEnum ) ; expect ( scrambledEqual ( expected , actual ) ) ; } ) ; } ) ; describe ( 'assertInRange' , ( ) => { enum TestEnum { Red , Green , Blue } const validValue = TestEnum . Red ; const act = ( value ) => assertInRange ( value , TestEnum ) ; new EnumRangeTestRunner ( act ) . testOutOfRangeThrows ( ) . testUndefinedValueThrows ( ) . testValidValueDoesNotThrow ( validValue ) ; } ) ; } ) ;