import { ProtectableMap } from '../ProtectableMap' ; class ExampleApi { public clearedCount : number = 0 ; public deletedCount : number = 0 ; public setCount : number = 0 ; private _studentAgesByName : ProtectableMap < string , number > ; public constructor ( ) { this . _studentAgesByName = new ProtectableMap ( { onClear : ( source ) => { ++ this . clearedCount ; } , onDelete : ( source , key ) => { ++ this . deletedCount ; } , onSet : ( source , key , value ) => { ++ this . setCount ; if ( key . toUpperCase ( ) !== key ) { throw new Error ( 'The key must be all upper case: ' + key ) ; } return Math . max ( value , 0 ) ; } } ) ; } public get studentAgesByName ( ) : Map < string , number > { return this . _studentAgesByName . protectedView ; } public doUnprotectedOperations ( ) { this . _studentAgesByName . clear ( ) ; this . _studentAgesByName . set ( 'Dave' , - 123 ) ; } } describe ( 'ProtectableMap' , ( ) => { test ( 'Protected operations' , ( ) => { const exampleApi = new ExampleApi ( ) ; exampleApi . studentAgesByName . clear ( ) ; exampleApi . studentAgesByName . set ( 'ALICE' , 23 ) ; exampleApi . studentAgesByName . set ( 'BOB' , 21 ) ; exampleApi . studentAgesByName . set ( 'BOB' , - 1 ) ; exampleApi . studentAgesByName . set ( 'CHARLIE' , 22 ) ; exampleApi . studentAgesByName . delete ( 'CHARLIE' ) ; expect ( exampleApi . clearedCount ) . toEqual ( 1 ) ; expect ( exampleApi . setCount ) . toEqual ( 4 ) ; expect ( exampleApi . deletedCount ) . toEqual ( 1 ) ; expect ( exampleApi . studentAgesByName . get ( 'ALICE' ) ) . toEqual ( 23 ) ; expect ( exampleApi . studentAgesByName . get ( 'BOB' ) ) . toEqual ( 0 ) ; expect ( exampleApi . studentAgesByName . has ( 'CHARLIE' ) ) . toEqual ( false ) ; } ) ; test ( 'Unprotected operations' , ( ) => { const exampleApi = new ExampleApi ( ) ; exampleApi . doUnprotectedOperations ( ) ; expect ( exampleApi . clearedCount ) . toEqual ( 0 ) ; expect ( exampleApi . studentAgesByName . get ( 'Dave' ) ) . toEqual ( - 123 ) ; } ) ; test ( 'Error case' , ( ) => { const exampleApi = new ExampleApi ( ) ; expect ( ( ) => { exampleApi . studentAgesByName . set ( 'Jane' , 23 ) ; } ) . toThrowError ( 'The key must be all upper case: Jane' ) ; } ) ; } ) ;