import { expectTypeOf } from 'expect-type' ; import Maybe from 'true-myth/maybe' ; import Result from 'true-myth/result' ; import { transposeResult , transposeMaybe , toOkOrElseErr , toOkOrErr , fromResult , fromMaybe , } from 'true-myth/toolbelt' ; describe ( 'transposeResult' , ( ) => { test ( 'Ok(Just(T))' , ( ) => { let result = Result . ok < Maybe < number > , string > ( Maybe . just ( 12 ) ) ; let transposed = transposeResult ( result ) ; expect ( transposed ) . toStrictEqual ( Maybe . just ( Result . ok ( 12 ) ) ) ; expectTypeOf ( transposed ) . toEqualTypeOf < Maybe < Result < number , string > > > ( ) ; } ) ; test ( 'Ok(Nothing)' , ( ) => { let result = Result . ok < Maybe < number > , string > ( Maybe . nothing < number > ( ) ) ; let transposed = transposeResult ( result ) ; expect ( transposed ) . toStrictEqual ( Maybe . nothing ( ) ) ; expectTypeOf ( transposed ) . toEqualTypeOf < Maybe < Result < number , string > > > ( ) ; } ) ; test ( 'Err(E)' , ( ) => { let result = Result . err < Maybe < number > , string > ( 'hello' ) ; let transposed = transposeResult ( result ) ; expect ( transposed ) . toStrictEqual ( Maybe . just ( Result . err ( 'hello' ) ) ) ; expectTypeOf ( transposed ) . toEqualTypeOf < Maybe < Result < number , string > > > ( ) ; } ) ; } ) ; test ( 'fromMaybe' , ( ) => { const theValue = 'something' ; const errValue = 'what happened?' ; const aJust = Maybe . just ( theValue ) ; const anOk = Result . ok ( theValue ) ; expect ( fromMaybe ( errValue , aJust ) ) . toEqual ( anOk ) ; const aNothing = Maybe . nothing ( ) ; const anErr = Result . err ( errValue ) ; expect ( fromMaybe ( errValue , aNothing ) ) . toEqual ( anErr ) ; } ) ; describe ( 'transposeMaybe' , ( ) => { test ( 'Just(Ok(T))' , ( ) => { let maybe = Maybe . just ( Result . ok < number , string > ( 12 ) ) ; let transposed = transposeMaybe ( maybe ) ; expect ( transposed ) . toStrictEqual ( Result . ok ( Maybe . just ( 12 ) ) ) ; expectTypeOf ( transposed ) . toEqualTypeOf < Result < Maybe < number > , string > > ( ) ; } ) ; test ( 'Just(Err(E))' , ( ) => { let maybe = Maybe . just ( Result . err < number , string > ( 'whoops' ) ) ; let transposed = transposeMaybe ( maybe ) ; expect ( transposed ) . toStrictEqual ( Result . err ( 'whoops' ) ) ; expectTypeOf ( transposed ) . toEqualTypeOf < Result < Maybe < number > , string > > ( ) ; } ) ; test ( 'Nothing' , ( ) => { let maybe = Maybe . nothing < Result < number , string > > ( ) ; let transposed = transposeMaybe ( maybe ) ; expect ( transposed ) . toStrictEqual ( Result . ok ( Maybe . nothing ( ) ) ) ; expectTypeOf ( transposed ) . toEqualTypeOf < Result < Maybe < number > , string > > ( ) ; } ) ; } ) ; test ( '`toOkOrErr`' , ( ) => { const theValue = 'string' ; const theJust = Maybe . of ( theValue ) ; const errValue = { reason : 'such badness' } ; expect ( toOkOrErr ( errValue , theJust ) ) . toEqual ( Result . ok ( theValue ) ) ; expect ( toOkOrErr ( errValue , Maybe . nothing ( ) ) ) . toEqual ( Result . err ( errValue ) ) ; expect ( toOkOrErr < string , typeof errValue > ( errValue ) ( theJust ) ) . toEqual ( toOkOrErr ( errValue , theJust ) ) ; } ) ; test ( '`toOkOrElseErr`' , ( ) => { const theJust = Maybe . of ( 12 ) ; const errValue = 24 ; const getErrValue = ( ) => errValue ; expect ( toOkOrElseErr ( getErrValue , theJust ) ) . toEqual ( Result . ok ( 12 ) ) ; expect ( toOkOrElseErr ( getErrValue , Maybe . nothing ( ) ) ) . toEqual ( Result . err ( errValue ) ) ; expect ( toOkOrElseErr < number , number > ( getErrValue ) ( theJust ) ) . toEqual ( toOkOrElseErr ( getErrValue , theJust ) ) ; } ) ; test ( '`fromResult`' , ( ) => { const value = 1000 ; const anOk = Result . ok ( value ) ; expect ( fromResult ( anOk ) ) . toEqual ( Maybe . just ( value ) ) ; const reason = 'oh teh noes' ; const anErr = Result . err ( reason ) ; expect ( fromResult ( anErr ) ) . toEqual ( Maybe . nothing ( ) ) ; } ) ;