package Ġtest . common Ċ Ċ import Ġorg . junit . Assert Ċ import Ġs late kit . common . results . Result Funcs . success Ċ import Ġs late kit . common . results . Result Funcs . failure Ċ import Ġs late kit . common . results . Result Funcs . un Authorized Ċ import Ġs late kit . common . results . Result Funcs . unexpected Error Ċ import Ġs late kit . common . results . Result Funcs . conflict Ċ import Ġs late kit . common . results . Result Funcs . not Found Ċ import Ġorg . junit . Test Ċ import Ġs late kit . common .* Ċ import Ġs late kit . common . Success Ċ import Ġs late kit . common . results .* Ċ import Ġs late kit . common . results . ResultCode . CONF LI CT Ċ import Ġs late kit . common . results . ResultCode . FAILURE Ċ import Ġs late kit . common . results . ResultCode . NOT _ FOUND Ċ import Ġs late kit . common . results . ResultCode . SUCCESS Ċ import Ġs late kit . common . results . ResultCode . UN AUTHORIZ ED Ċ import Ġs late kit . common . results . ResultCode . UN EXPECTED _ ERROR Ċ Ċ /** ĊĠ Ġ* Ġ< s late _ header > ĊĠ Ġ* Ġauthor : ĠK ish ore ĠRed dy ĊĠ Ġ* Ġurl : Ġhttps :// github . com / k ish orer ed dy / scala - s late ĊĠ Ġ* Ġcopyright : Ġ 2 0 1 5 ĠK ish ore ĠRed dy ĊĠ Ġ* Ġlicense : Ġhttps :// github . com / k ish orer ed dy / scala - s late / blob / master / LICENSE . md ĊĠ Ġ* Ġdesc : Ġa Ġscala Ġmicro - framework ĊĠ Ġ* Ġusage : ĠPlease Ġrefer Ġto Ġlicense Ġon Ġgithub Ġfor Ġmore Ġinfo . ĊĠ Ġ* Ġ</ s late _ header > ĊĠ Ġ*/ ĊĊ Ċ // object ĠMy Codes Ġ{ Ċ // Ċ // Ġ Ġval ĠUser Created Ġ= Ġs late kit . common . results . Success ( 2 0 0 , Ġ" Created Ġuser ") Ċ // Ġ Ġval ĠUser Updated Ġ= Ġs late kit . common . results . Success ( 2 0 1 , Ġ" Updated Ġuser ") Ċ // Ġ Ġval ĠUser Deleted Ġ= Ġs late kit . common . results . Success ( 2 0 0 , Ġ" Deleted Ġuser ") Ċ //} ĊĊ Ċ class ĠResult Tests Ġ{ ĊĊĊĠ Ġdata Ġclass ĠPerson ( val Ġname : String ) ĊĊĊĠ Ġ@ Test Ġfun Ġcan _ build _ result () Ġ{ ĊĠĠĠ Ġval Ġres Ġ= Ġ ĠSuccess (" user 0 1 ", Ġ 1 2 3 , Ġmsg Ġ= Ġ" created ") ĊĠĠĠ Ġassert ( Ġres . success Ġ Ġ) ĊĠĠĠ Ġassert ( Ġres . msg Ġ== Ġ" created " Ġ) ĊĠĠĠ Ġassert ( Ġres . code Ġ== Ġ 1 2 3 Ġ) ĊĠĠĠ Ġassert ( Ġres . getOrElse Ġ{ Ġ"" Ġ} Ġ== Ġ" user 0 1 ") ĊĠ Ġ} ĊĊĊĠ Ġ@ Test Ġfun Ġcan _ build _ success () Ġ{ ĊĠĠĠ Ġensure ( success ( true ), Ġtrue , ĠSUCCESS , Ġ" success ", Ġtrue ) ĊĠ Ġ} ĊĊĊĠ Ġ@ Test Ġfun Ġcan _ build _ success _ with _ type () Ġ{ ĊĠĠĠ Ġensure ( success ( 1 2 3 ), Ġtrue , ĠSUCCESS , Ġ" success ", Ġ 1 2 3 ) ĊĠ Ġ} ĊĊĊĠ Ġ@ Test Ġfun Ġcan _ build _ failure () Ġ{ ĊĠĠĠ Ġensure ( failure (), Ġfalse , ĠFAILURE , Ġ" failure ", Ġnull ) ĊĠ Ġ} ĊĊĊĠ Ġ@ Test Ġfun Ġcan _ build _ un authorized () Ġ{ ĊĠĠĠ Ġensure ( un Authorized (), Ġfalse , ĠU NA UTH ORIZ ED , Ġ" un authorized ", Ġnull ) ĊĠ Ġ} ĊĊĊĠ Ġ@ Test Ġfun Ġcan _ build _ un authorized 2 () Ġ{ ĊĠĠĠ Ġval Ġresult 1 Ġ= Ġun Authorized < Int >(" 1 ") ĊĠĠĠ Ġval Ġresult 2 Ġ= Ġun Authorized < Int , ĠErr >( Invalid ( 2 , Ġ" Unauthorized ")) Ċ Ċ // ĠĠĠ Ġval Ġresult 4 Ġ= Ġsuccess 2 < Person , ĠErr >( Person (" k ish ore ")) Ċ // ĠĠĠ Ġval Ġresult 5 Ġ= Ġsuccess 2 < Person , ĠErr >( Person (" k ish ore "), ĠMy Codes . User Created ) Ċ // ĠĠĠ Ġval Ġresult 6 Ġ= Ġfailure 2 < Person , ĠErr >( Invalid ( 3 0 0 , Ġ" Invalid Ġuser ")) ĊĠ Ġ} ĊĊĊĠ Ġ@ Test Ġfun Ġcan _ build _ unexpected () Ġ{ ĊĠĠĠĠĠ Ġval Ġresult Ġ= Ġunexpected Error < Person >( Exception (" Test Ġerror ")) ĊĠĠĠĠĠ Ġassert (! result . success ) ĊĠĠĠĠĠ Ġassert ( result . code Ġ== ĠUN EXPECTED _ ERROR ) ĊĠĠĠĠĠ Ġassert ( result . msg Ġ== Ġ" unexpected Ġerror ") ĊĠ Ġ} ĊĊĊĠ Ġ@ Test Ġfun Ġcan _ build _ conflict () Ġ{ ĊĠĠĠ Ġensure ( conflict (), Ġfalse , ĠCON FLI CT , Ġ" conflict ", Ġnull ) ĊĠ Ġ} ĊĊĊĠ Ġ@ Test Ġfun Ġcan _ build _ not _ found () Ġ{ ĊĠĠĠ Ġensure ( not Found (), Ġfalse , ĠNOT _ FOUND , Ġ" not Ġfound ", Ġnull ) ĊĠ Ġ} ĊĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest _ success (){ ĊĠĠĠ Ġval Ġres Ġ= Ġsuccess ( Person (" pe ter "), Ġmsg =" created ") ĊĠĠĠ ĠAssert . assertTrue ( res . success ) ĊĠĠĠ ĠAssert . assertEquals (" created ", Ġres . msg ) ĊĠĠĠ ĠAssert . assertEquals ( SUCCESS , Ġres . code ) ĊĠ Ġ} ĊĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest _ map _ success (){ ĊĠĠĠ Ġval Ġres 1 Ġ= Ġsuccess ( Person (" pe ter "), Ġmsg =" created ") ĊĠĠĠ Ġval Ġres Ġ= Ġres 1 . map Ġ{ Ġit Ġ-> ĠPair ( it , Ġ" spider - man ") Ġ} ĊĊĠĠĠ ĠAssert . assertTrue ( res . success ) ĊĠĠĠ ĠAssert . assertEquals (" created ", Ġres . msg ) ĊĠĠĠ ĠAssert . assertEquals ( SUCCESS , Ġres . code ) ĊĊĠĠĠ Ġval Ġuser Ġ= Ġres . getOrElse ( Ġ{ ĠPair ( Person (" k ish ore "), Ġ" re ddy ") Ġ}) ĊĠĠĠ ĠAssert . assertTrue ( user . first . name Ġ== Ġ" pe ter ") ĊĠĠĠ ĠAssert . assertTrue ( user . second Ġ== Ġ" spider - man ") ĊĠ Ġ} ĊĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest _ map _ failure (){ ĊĠĠĠ Ġval Ġres 1 Ġ= Ġfailure < Person >() ĊĠĠĠ Ġval Ġres Ġ= Ġres 1 . map Ġ{ Ġit Ġ-> ĠPair ( it , Ġ" spider - man ") Ġ} ĊĊĠĠĠ ĠAssert . assertFalse ( res . success ) ĊĠĠĠ ĠAssert . assertEquals (" failure ", Ġres . msg ) ĊĠĠĠ ĠAssert . assertEquals ( FAILURE , Ġres . code ) ĊĊĠĠĠ Ġval Ġuser Ġ= Ġres . getOrElse ( Ġ{ ĠPair ( Person (" k ish ore "), Ġ" re ddy ") Ġ}) ĊĠĠĠ ĠAssert . assertTrue ( user . first . name Ġ== Ġ" k ish ore ") ĊĠĠĠ ĠAssert . assertTrue ( user . second Ġ== Ġ" re ddy ") ĊĠ Ġ} ĊĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest _ flat _ map _ success (){ ĊĠĠĠ Ġval Ġres 1 Ġ= Ġsuccess ( Person (" pe ter "), Ġmsg =" created ") ĊĠĠĠ Ġval Ġres Ġ= Ġres 1 . flatMap Ġ{ Ġit Ġ-> Ġsuccess ( Pair ( it , Ġ" spider - man "), ĠSUCCESS , Ġ" alias ") Ġ} ĊĊĠĠĠ ĠAssert . assertTrue ( res . success ) ĊĠĠĠ ĠAssert . assertEquals (" alias ", Ġres . msg ) ĊĠĠĠ ĠAssert . assertEquals ( SUCCESS , Ġres . code ) ĊĊĠĠĠ Ġval Ġuser Ġ= Ġres . getOrElse ( Ġ{ ĠPair ( Person (" k ish ore "), Ġ" re ddy ") Ġ}) ĊĠĠĠ ĠAssert . assertTrue ( user . first . name Ġ== Ġ" pe ter ") ĊĠĠĠ ĠAssert . assertTrue ( user . second Ġ== Ġ" spider - man ") ĊĠ Ġ} ĊĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest _ flat _ map _ failure (){ ĊĠĠĠ Ġval Ġres 1 Ġ= Ġfailure < Person >(" setup ") ĊĠĠĠ Ġval Ġres Ġ= Ġres 1 . flatMap Ġ{ Ġit Ġ-> Ġfailure < Pair < Person , ĠString >>() Ġ} ĊĊĠĠĠ ĠAssert . assertFalse ( res . success ) ĊĠĠĠ ĠAssert . assertEquals (" setup ", Ġres . msg ) ĊĠĠĠ ĠAssert . assertEquals ( FAILURE , Ġres . code ) ĊĊĠĠĠ Ġval Ġuser Ġ= Ġres . getOrElse ( Ġ{ ĠPair ( Person (" k ish ore "), Ġ" re ddy ") Ġ}) ĊĠĠĠ ĠAssert . assertTrue ( user . first . name Ġ== Ġ" k ish ore ") ĊĠĠĠ ĠAssert . assertTrue ( user . second Ġ== Ġ" re ddy ") ĊĠ Ġ} ĊĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest _ of _ success (){ ĊĠĠĠ Ġval Ġres Ġ= ĠResult . of Ġ{ ĠPerson (" pe ter ") Ġ} ĊĊĠĠĠ ĠAssert . assertTrue ( res . success ) ĊĠĠĠ ĠAssert . assertEquals (" success ", Ġres . msg ) ĊĠĠĠ ĠAssert . assertEquals ( SUCCESS , Ġres . code ) ĊĠ Ġ} ĊĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest _ of _ failure (){ ĊĠĠĠ Ġval Ġres Ġ= ĠResult . of Ġ{ Ġthrow ĠException (" Test Ġerror ") Ġ} ĊĊĠĠĠ ĠAssert . assertFalse ( res . success ) ĊĠĠĠ ĠAssert . assertEquals (" Test Ġerror ", Ġres . msg ) ĊĠĠĠ ĠAssert . assertEquals ( FAILURE , Ġres . code ) ĊĠ Ġ} ĊĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest _ attempt _ failure (){ ĊĠĠĠ Ġval Ġres Ġ= ĠResult . attempt Ġ{ Ġthrow ĠException (" Test Ġerror ") Ġ} ĊĊĠĠĠ ĠAssert . assertFalse ( res . success ) ĊĠĠĠ ĠAssert . assertEquals (" Test Ġerror ", Ġres . msg ) ĊĠĠĠ ĠAssert . assertEquals ( UN EXPECTED _ ERROR , Ġres . code ) ĊĠ Ġ} ĊĊĊĠ Ġ@ Test ĊĠ Ġfun Ġtest _ fold (){ ĊĠĠĠ Ġval Ġres Ġ= ĠResult . of Ġ{ ĠPerson (" pe ter ") Ġ} ĊĠĠĠ Ġval Ġname Ġ= Ġres . fold ({ Ġit Ġ-> Ġ" Success ĠPeter " Ġ}, ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ{ Ġit Ġ-> Ġ" Error ĠPeter " ĠĠ Ġ}) ĊĊĠĠĠ ĠAssert . assertTrue ( res . success ) ĊĠĠĠ ĠAssert . assertEquals (" success ", Ġres . msg ) ĊĠĠĠ ĠAssert . assertEquals ( SUCCESS , Ġres . code ) ĊĠĠĠ ĠAssert . assertEquals (" Success ĠPeter ", Ġname ) ĊĠ Ġ} ĊĊĊĠ Ġprivate Ġfun Ġensure ( result : Result Msg < Any >, Ġsuccess : Boolean , Ġcode : Int , Ġmsg : String , Ġdata : Any ?) Ġ{ ĊĠĠĠ Ġassert ( result . success Ġ== Ġsuccess ) ĊĠĠĠ Ġassert ( result . code Ġ== Ġcode ) ĊĠĠĠ Ġassert ( result . msg Ġ== Ġmsg ) ĊĠĠĠ Ġif ( result . success ){ ĊĠĠĠĠĠ Ġassert ( result . getOrElse Ġ{ Ġ"" Ġ} Ġ== Ġdata ) ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } Ċ