import { ExpectationError } from './expectationError' ; import { ToBeSorted } from './toBeSorted' ; /**
 * utilitary class with assertion methods.
 */ class Expect { private notFlag : boolean ; /**
     * inverts the following assertion.
     */ public get not ( ) : Expect { const expect = new Expect ( ) ; expect . notFlag = ! this . notFlag ; return expect ; } /**
     * returns a ToBeSorted assertion builder
     */ public get toBeSorted ( ) : ToBeSorted { return new ToBeSorted ( this . notFlag ) ; } param actual param expected param message public toBeEqual < T > ( actual , expected , message ? ) { if ( this . notFlag ? actual === expected : actual !== expected ) { throw new ExpectationError ( message || `Expected ${ this . stringify ( actual ) } ${ this . notFlag ? 'not ' : '' }to equal ${ this . stringify ( expected ) }.` ) ; } } param actual param expected param message public arraysToBeEqual < T extends readonly unknown [ ] > ( actual , expected , message ? ) { if ( actual . length !== expected . length ) { throw new ExpectationError ( message || 'Expected sequences to be equal, but their lenghts were different.' ) ; } for ( const i in actual ) { if ( ! actual . hasOwnProperty ( i ) ) { continue ; } this . toBeEqual ( actual [ i ] , expected [ i ] , message ) ; } } param actual param expected param message public toBeGreater ( actual , expected , message ? ) { if ( ( ! this . notFlag && actual <= expected ) || ( this . notFlag && actual > expected ) ) { throw new ExpectationError ( message || `Expected ${ actual } ${ this . notFlag ? 'not ' : '' }to be greater than ${ expected }.` ) ; } } param actual param expected param message public toBeGreaterOrEqual ( actual , expected , message ? ) { if ( ( ! this . notFlag && actual < expected ) || ( this . notFlag && actual >= expected ) ) { throw new ExpectationError ( message || `Expected ${ actual } ${ this . notFlag ? 'not ' : '' }to be greater than or equal to ${ expected }.` ) ; } } param actual param expected param message public toBeLess ( actual , expected , message ? ) { if ( ( ! this . notFlag && actual >= expected ) || ( this . notFlag && actual < expected ) ) { throw new ExpectationError ( message || `Expected ${ actual } ${ this . notFlag ? 'not ' : '' }to be less than ${ expected }.` ) ; } } param actual param expected param message public toBeLessOrEqual ( actual , expected , message ? ) { if ( ( ! this . notFlag && actual > expected ) || ( this . notFlag && actual <= expected ) ) { throw new ExpectationError ( message || `Expected ${ actual } ${ this . notFlag ? 'not ' : '' }to be less than or equal to ${ expected }.` ) ; } } param value param message public toBeDefined < T > ( value , message ? ) { if ( this . notFlag ? value !== undefined : value === undefined ) { throw new ExpectationError ( message || `Expected value ${ this . notFlag ? 'not' : '' } to be defined.` ) ; } } param func param message public toThrow ( func , message ? ) { try { func ( ) ; } catch ( err ) { if ( this . notFlag ) { throw new ExpectationError ( message || 'Expected function not to throw' ) ; } return ; } if ( this . notFlag ) { return ; } throw new ExpectationError ( message || 'Expected function to throw' ) ; } param func param message public async toThrowAsync ( func , message ? ) { try { await func ( ) ; } catch ( err ) { if ( this . notFlag ) { throw new ExpectationError ( message || 'Expected function not to throw' ) ; } return ; } if ( this . notFlag ) { return ; } throw new ExpectationError ( message || 'Expected function to throw' ) ; } param value param message public toBeTrue ( value , message ? ) { if ( this . notFlag ? value === true : value !== true ) { throw new ExpectationError ( message || `Expected ${ value } to be true.` ) ; } } param value param message public toBeFalse ( value , message ? ) { if ( this . notFlag ? value === false : value !== false ) { throw new ExpectationError ( message || `Expected ${ value } to be false.` ) ; } } param value param message public toBeTruthy < T > ( value , message ? ) { if ( this . notFlag ? value : ! value ) { throw new ExpectationError ( message || `Expected ${ value } to be truthy.` ) ; } } param value param message public toBeFalsy < T > ( value , message ? ) { if ( this . notFlag ? ! value : value ) { throw new ExpectationError ( message || `Expected ${ value } to be falsy.` ) ; } } param str param regex param message public toMatch ( str , regex , message ? ) { const matches = regex . test ( str ) ; if ( this . notFlag ? matches : ! matches ) { throw new ExpectationError ( message || `Expected "${ str }" to match ${ regex }.` ) ; } } param item param array param message public toBeIn < T > ( item , array , message ? ) { const isIn = array . find ( ( x ) => x === item ) ; if ( this . notFlag ? isIn : ! isIn ) { throw new ExpectationError ( message || `Expected ${ item } to be in [${ array . map ( ( x ) => this . stringify ( x ) ) . join ( ', ' ) }]` ) ; } } /**
     * returns a string version of the given value. For objects, returns JSON.stringify(val).
     * For numbers and strings, returns `${val}`.
     */ private stringify ( val ) { return typeof val === 'object' ? JSON . stringify ( val ) : `${ val }` ; } } /**
 * utilitary class with assertion methods.
 */ export const expect = new Expect ( ) ;