import { ParjsResult , ResultKind } from "../../lib/internal/result" ; import matches from "lodash/matches" ; import isPlainObject from "lodash/isPlainObject" ; declare global { namespace jasmine { interface Matchers < T > { toBeAnyOf ( options , failMessage ? ) ; toHaveType ( type , failMessage ? ) ; toHaveMember ( name , failMessage ? ) ; toBeLike ( obj ) ; } } } class CustomMatcherDefs { actual : any ; toBeAnyOf ( expecteds , failMessage ) { let result ; for ( let i = 0 , l = expecteds . length ; i < l ; i ++ ) { if ( this . actual === expecteds [ i ] ) { result = true ; break ; } } return { pass : result , message : result ? undefined : failMessage } ; } toBeLike ( o , failMessage ) { let pass = matches ( o ) ( this . actual ) ; return { pass , message : pass ? undefined : failMessage } ; } toHaveType ( type , failMessage ) { let pass = typeof this . actual === type ; return { pass , message : pass ? undefined : failMessage } ; } toHaveMember ( name , failMessage ) { let pass = name in this . actual ; return { pass , message : pass ? undefined : failMessage } ; } } export const CustomMatchers = { } as any ; let defs = CustomMatcherDefs . prototype ; for ( let prop of Object . getOwnPropertyNames ( defs ) ) { if ( prop === "constructor" ) continue ; CustomMatchers [ prop ] = function ( a , b ) { return { compare ( actual , ... rest ) { defs . actual = actual ; return defs [ prop ] ( ... rest ) ; } } ; } ; } param result param failType export function expectFailure ( result , failType ? ) { expect ( result . kind ) . toBeAnyOf ( [ ResultKind . FatalFail , ResultKind . HardFail , ResultKind . SoftFail ] , "expected kind to be a Fail" ) ; if ( result . kind === ResultKind . Ok ) return ; if ( failType !== undefined ) { expect ( result . kind ) . toBe ( failType ) ; } expect ( result . trace . reason ) . toHaveType ( "string" , "invaid 'reason' value" ) ; } param result param value param state export function expectSuccess < T > ( result , value ? , state ? ) { expect ( result . kind ) . toBe ( ResultKind . Ok , "kind wasn't OK" ) ; if ( result . kind !== ResultKind . Ok ) return ; expect ( result ) . toHaveMember ( "value" , "reason value" ) ; expect ( result ) . not . toHaveMember ( "reason" , "unexpected 'reason' attribute" ) ; if ( value !== undefined ) { if ( ! isPlainObject ( value ) ) { expect ( result . value ) . toEqual ( value ) ; } else { expect ( result . value ) . toBeLike ( value as any ) ; } } } beforeEach ( ( ) => { jasmine . addMatchers ( CustomMatchers ) ; } ) ;