import { Result , Ok , Err } from '../lib' ; describe ( 'result' , ( ) => { test ( 'An Ok can be assigned to any Result with a compatible Ok type' , ( ) => { const result = Ok ( 10 ) ; const result2 = Ok ( 10 ) ; function getResult ( ) { return Math . random ( ) > 0.4 ? Ok ( 33 ) : Err ( { code : 'oops' } ) ; } } ) ; test ( 'An Err can be assigned to any Result with a compatible Err type' , ( ) => { const result = Err ( 10 ) ; const result2 = Err ( 10 ) ; function getError ( ) { return Err ( { code : 'oops' } ) ; } } ) ; test ( 'Ok.isOk' , ( ) => { const ok = Ok ( 10 ) ; expect ( ok . isOk ( ) && ok . get ( ) === 10 ) . toBe ( true ) ; } ) ; test ( 'Err.isOk' , ( ) => { const err = Err ( 10 ) ; expect ( ! err . isOk ( ) && err . get ( ) === 10 ) . toBe ( true ) ; } ) ; test ( 'Ok.map' , ( ) => { const ok = Ok ( 10 ) . map ( x => x * 2 ) ; expect ( ok . type === 'ok' && ok . get ( ) === 20 ) . toBe ( true ) ; } ) ; test ( 'Err.map' , ( ) => { const err = Err ( 10 ) . map ( x => 10 ) ; expect ( err . type === 'err' && err . get ( ) === 10 ) . toBe ( true ) ; } ) ; test ( 'Ok.mapError' , ( ) => { const ok = Ok ( 10 ) ; const result = ok . mapError ( e => e * 10 ) ; expect ( result . type === 'ok' && result . get ( ) === 10 ) . toBe ( true ) ; } ) ; test ( 'Err.mapError' , ( ) => { const err = Err ( 10 ) ; const result = err . mapError ( e => e * 10 ) ; expect ( result . type === 'err' && result . get ( ) === 100 ) . toBe ( true ) ; } ) ; test ( 'Ok.flatMap -> Ok' , ( ) => { const ok = Ok ( 10 ) . flatMap ( x => Ok ( String ( x * 2 ) ) ) ; expect ( ok . get ( ) ) . toBe ( '20' ) ; } ) ; test ( 'Ok.flatMap -> Err' , ( ) => { const err = Ok ( 10 ) . flatMap ( _ => Err ( 'oops' ) ) ; expect ( err . get ( ) ) . toBe ( 'oops' ) ; } ) ; test ( 'Err.flatMap -> Ok' , ( ) => { const err = Err ( 'oops' ) . flatMap ( _ => Ok ( 10 ) ) ; expect ( err . get ( ) ) . toBe ( 'oops' ) ; } ) ; test ( 'Err.flatMap -> Err' , ( ) => { const err = Err ( 'oops' ) . flatMap ( _ => Err ( 10 ) ) ; expect ( err . get ( ) ) . toBe ( 'oops' ) ; } ) ; test ( 'Err.flatMap -> Err | Ok' , ( ) => { type ErrT = { tonton : string ; } ; type OkT = { tata : boolean ; } ; const err = Err ( 'oops' ) . flatMap < ErrT , OkT > ( _ => Math . random ( ) > 0.5 ? Ok ( { tata : true } ) : Err ( { tonton : 'oops' } ) ) ; expect ( err . get ( ) ) . toBe ( 'oops' ) ; } ) ; test ( 'Ok.fold' , ( ) => { const ok = Ok ( 10 ) . fold ( err => 20 , val => 30 ) ; expect ( ok ) . toBe ( 30 ) ; } ) ; test ( 'Err.fold' , ( ) => { const err = Err ( 10 ) . fold ( err => 20 , val => 30 ) ; expect ( err ) . toBe ( 20 ) ; } ) ; test ( 'Ok.toOption' , ( ) => { const option = Ok ( 10 ) . toOption ( ) ; expect ( option . isDefined ( ) && option . get ( ) ) . toBe ( 10 ) ; } ) ; test ( 'Err.toOption' , ( ) => { const option = Err ( 'error' ) . toOption ( ) ; expect ( option . isDefined ( ) ) . toBe ( false ) ; } ) ; test ( 'Result.all - 2 Ok' , ( ) => { const result = Result . all ( [ Ok ( 10 ) , Ok ( '20' ) ] ) ; expect ( result . isOk ( ) ) . toBe ( true ) ; expect ( result . get ( ) ) . toEqual ( [ 10 , '20' ] ) ; } ) ; test ( 'Result.all - 1 Ok, 1 Err' , ( ) => { const result = Result . all ( [ Ok ( 10 ) , Err ( '20' ) ] ) ; expect ( result . isOk ( ) ) . toBe ( false ) ; expect ( result . get ( ) ) . toEqual ( '20' ) ; } ) ; test ( 'Result.all - 1 Ok, 2 Err' , ( ) => { const result = Result . all ( [ Ok ( 10 ) , Err ( '20' ) , Err ( { error : 'oops' } ) ] ) ; expect ( result . isOk ( ) ) . toBe ( false ) ; expect ( result . get ( ) ) . toEqual ( '20' ) ; } ) ; test ( 'Result.all - 10 Ok' , ( ) => { const result = Result . all ( [ Ok ( 1 ) , Ok ( '2' ) , Ok ( 3 ) , Ok ( '4' ) , Ok ( 5 ) , Ok ( true ) , Ok ( 7 ) , Ok ( 8 ) , Ok ( 9 ) , Ok ( 10 ) ] ) ; const result2 = Result . all ( [ Ok ( 1 ) , Ok ( '2' ) , Ok ( 3 ) , Ok ( '4' ) , Ok ( 5 ) , Ok ( true ) ] ) ; expect ( result . get ( ) [ 2 ] + result . get ( ) [ 9 ] === 13 ) . toBe ( true ) ; expect ( result . get ( ) ) . toEqual ( [ 1 , '2' , 3 , '4' , 5 , true , 7 , 8 , 9 , 10 ] ) ; expect ( result2 . get ( ) [ 0 ] + result2 . get ( ) [ 4 ] === 6 ) . toBe ( true ) ; } ) ; } ) ;