packages feed

antelude-0.1.0: test/Main.hs

module Main
    ( main
    ) where

import safe           Antelude
import safe qualified Antelude.Array         as AArray
import safe qualified Antelude.Bool          as ABool
import safe qualified Antelude.Either        as AEither
import safe qualified Antelude.Function      as AFunction
import safe qualified Antelude.List          as AList
import safe qualified Antelude.List.NonEmpty as ANonEmpty
import safe qualified Antelude.Maybe         as AMaybe
import safe qualified Antelude.Result        as AResult
import safe qualified Antelude.String        as AString
import safe qualified Antelude.String.Case   as ACase
import safe qualified Antelude.Tuple.Pair    as APair
import safe qualified Antelude.Tuple.Trio    as ATrio

import safe           Test.HUnit
    ( Test (..)
    , assertEqual
    , runTestTTAndExit
    )


main :: IO ()
main = runTestTTAndExit tests


-- https://hackage.haskell.org/package/HUnit
tests :: Test
tests =
  TestList
    [ testArray
    , testBool
    , testEither
    , testFunction
    , testList
    , testMaybe
    , testMonad
    , testNonEmpty
    , testResult
    , testString
    , testStringCase
    , testTuplesPair
    , testTuplesTrio
    , testAnteludeBase
    ]


-- A lot of the contents are just renames, only test what isn't a rename
testArray :: Test
testArray =
  TestList
    [ TestCase (assertEqual "array from nonempty" (AArray.array (1, 1) [(1, "1")] :: Array Int String) (AArray.fromNonEmpty (1, 1) ("1" ANonEmpty.:| [])))
    , TestCase (assertEqual "array update" (AArray.array (1, 1) [(1, "1")] :: Array Int String) (AArray.update [(1, "1")] (AArray.array (1, 1) [(1, "0")] :: Array Int String)))
    , TestCase (assertEqual "array at index 1" (Just "1") (AArray.atIndex 1 (AArray.array (1, 1) [(1, "1")] :: Array Int String)))
    , TestCase (assertEqual "array at index 2" Nothing (AArray.atIndex 2 (AArray.array (1, 1) [(1, "1")] :: Array Int String)))
    , TestCase (assertEqual "array at index 3" Nothing (AArray.atIndex 0 (AArray.array (1, 1) [(1, "1")] :: Array Int String)))
    ]


testBool :: Test
testBool =
  TestList
    [ TestCase (assertEqual "bool word and" True (True `ABool.and` True))
    , TestCase (assertEqual "bool word or" True (True `ABool.or` True))
    , TestCase (assertEqual "bool word xor" True (True `ABool.xor` False))
    ]


testEither :: Test
testEither =
  TestList
    [ TestCase (assertEqual "either from result 1" (Right "string") (AEither.fromResult (Ok "string") :: Either String String))
    , TestCase (assertEqual "either from result 2" (Left "string") (AEither.fromResult (Err "string") :: Either String String))
    , TestCase (assertEqual "either from maybe 1" (Right "string") (AEither.fromMaybe (Just "string")))
    , TestCase (assertEqual "either from maybe 2" (Left ()) (AEither.fromMaybe Nothing :: Either () String))
    , TestCase (assertEqual "either map left maps left" (Left "hello world") (AEither.mapLeft (<> " world") (Left "hello") :: Either String String))
    , TestCase (assertEqual "either map left ignores right" (Right "hello") (AEither.mapLeft (<> " world") (Right "hello") :: Either String String))
    , TestCase (assertEqual "either map right maps right" (Right "hello world") (AEither.mapRight (<> " world") (Right "hello") :: Either String String))
    , TestCase (assertEqual "either map right ignores left" (Left "hello") (AEither.mapRight (<> " world") (Left "hello") :: Either String String))
    , TestCase (assertEqual "either swap 1" (Right 0) (AEither.swap (Left 0) :: Either Int Int))
    , TestCase (assertEqual "either swap 2" (Left 0) (AEither.swap (Right 0) :: Either Int Int))
    , TestCase (assertEqual "either left with default 1" "left" (AEither.leftWithDefault "default" (Left "left" :: Either String String)))
    , TestCase (assertEqual "either left with default 2" "default" (AEither.leftWithDefault "default" (Right "right" :: Either String String)))
    , TestCase (assertEqual "either right with default 1" "right" (AEither.rightWithDefault "default" (Right "right" :: Either String String)))
    , TestCase (assertEqual "either right with default 2" "default" (AEither.rightWithDefault "default" (Left "left" :: Either String String)))
    , TestCase (assertEqual "either filter lefts" ["1", "3"] (AEither.filterLefts [Left "1", Right "2", Left "3", Right "4"]))
    , TestCase (assertEqual "either filter rights" ["2", "4"] (AEither.filterRights [Left "1", Right "2", Left "3", Right "4"]))
    , TestCase (assertEqual "either partition" (["1", "3"], ["2", "4"]) (AEither.partition [Left "1", Right "2", Left "3", Right "4"]))
    ]


testFunction :: Test
testFunction =
  TestList
    [ TestCase (assertEqual "function identity" "string" (AFunction.identity "string"))
    , TestCase (assertEqual "function constant" "always" (AFunction.constant "always" "never"))
    , TestCase (assertEqual "function flip" "hello world" (AFunction.flip (<>) "world" "hello "))
    , TestCase (assertEqual "function left arrow" "left arrow" (AFunction.identity <| "left arrow"))
    , TestCase (assertEqual "function right arrow" "right arrow" ("right arrow" |> AFunction.identity))
    , TestCase (assertEqual "function left compose" "composed" ((AFunction.identity <. AFunction.identity) "composed"))
    , TestCase (assertEqual "function right compose" "composed" ((AFunction.identity .> AFunction.identity) "composed"))
    ]


testList :: Test
testList =
  TestList
    [ TestCase (assertEqual "list head 1" (Just "1") (AList.head ["1", "2", "3"]))
    , TestCase (assertEqual "list head 2" Nothing (AList.head ([] :: List String)))
    , TestCase (assertEqual "list last 1" (Just "3") (AList.last ["1", "2", "3"]))
    , TestCase (assertEqual "list last 2" Nothing (AList.last ([] :: List String)))
    , TestCase (assertEqual "list tail 1" (Just ["2", "3"]) (AList.tail ["1", "2", "3"]))
    , TestCase (assertEqual "list tail 2" Nothing (AList.tail ([] :: List String)))
    , TestCase (assertEqual "list init 1" (Just ["1", "2"]) (AList.init ["1", "2", "3"]))
    , TestCase (assertEqual "list init 2" Nothing (AList.init ([] :: List String)))
    , TestCase (assertEqual "list at index 1" (Just "1") (AList.atIndex 0 ["1", "2", "3"]))
    , TestCase (assertEqual "list at index 2" Nothing (AList.atIndex (-1) ["1", "2", "3"]))
    , TestCase (assertEqual "list at index 3" Nothing (AList.atIndex 3 ["1", "2", "3"]))
    , TestCase (assertEqual "list at index 4" Nothing (AList.atIndex (-4) ["1", "2", "3"]))
    , TestCase (assertEqual "list append" ["1", "2", "3"] (AList.append "3" ["1", "2"]))
    , TestCase (assertEqual "list prepend" ["1", "2", "3"] (AList.prepend "1" ["2", "3"]))
    , TestCase (assertEqual "list cons" ["1", "2", "3"] (AList.cons "1" ["2", "3"]))
    ]


testMaybe :: Test
testMaybe =
  TestList
    [ TestCase (assertEqual "maybe map 1" (Just "hello world") (AMaybe.map (<> " world") (Just "hello")))
    , TestCase (assertEqual "maybe map 2" Nothing (AMaybe.map (<> " world") Nothing))
    , TestCase (assertEqual "maybe with default 1" "just" (AMaybe.withDefault "default" (Just "just")))
    , TestCase (assertEqual "maybe with default 2" "default" (AMaybe.withDefault "default" Nothing))
    , TestCase (assertEqual "maybe from result 1" (Just "ok") (AMaybe.fromResult (Ok "ok")))
    , TestCase (assertEqual "maybe from result 2" Nothing (AMaybe.fromResult (Err "err" :: Result String String)))
    , TestCase (assertEqual "maybe from either right 1" (Just "right") (AMaybe.fromEitherRight (Right "right" :: Either String String)))
    , TestCase (assertEqual "maybe from either right 2" Nothing (AMaybe.fromEitherRight (Left "left" :: Either String String)))
    , TestCase (assertEqual "maybe from either left 1" (Just "left") (AMaybe.fromEitherLeft (Left "left" :: Either String String)))
    , TestCase (assertEqual "maybe from either left 2" Nothing (AMaybe.fromEitherLeft (Right "right" :: Either String String)))
    ]


testMonad :: Test
testMonad =
  TestList
    [ TestCase (assertEqual "monad left doublepointy" (Just "a string") (Just "a string" << Just "nope"))
    ]


testNonEmpty :: Test
testNonEmpty =
  TestList
    [ TestCase (assertEqual "nonempty prepend" ("1" :| ["2", "3"]) (ANonEmpty.prepend ("2" :| ["3"]) ("1" :| [])))
    , TestCase (assertEqual "nonempty append" ("2" :| ["3", "1"]) (ANonEmpty.append ("2" :| ["3"]) ("1" :| [])))
    , TestCase (assertEqual "nonempty appendList" ("2" :| ["3", "1"]) (ANonEmpty.appendList ["1"] ("2" :| ["3"])))
    , TestCase (assertEqual "nonempty at index 1" (Just "1") (ANonEmpty.atIndex 0 ("1" :| ["2", "3"])))
    , TestCase (assertEqual "nonempty at index 2" Nothing (ANonEmpty.atIndex (-2) ("1" :| ["2", "3"])))
    , TestCase (assertEqual "nonempty at index 3" (Just "3") (ANonEmpty.atIndex 2 ("1" :| ["2", "3"])))
    , TestCase (assertEqual "nonempty at index 4" Nothing (ANonEmpty.atIndex 5 ("1" :| ["2", "3"])))
    , TestCase (assertEqual "nonempty at index 5" Nothing (ANonEmpty.atIndex (-5) ("1" :| ["2", "3"])))
    -- Only in base 4.21, comment cause flaky on old versions
    -- , TestCase (assertEqual "nonempty sort on" ('1' :| ['2', '3', '4']) (ANonEmpty.sortOn Char.ord ('3' :| ['4', '2', '1'])))
    ]


testResult :: Test
testResult =
  TestList
    [ TestCase (assertEqual "result from either 1" (Ok "ok") (AResult.fromEither (Right "ok" :: Either String String)))
    , TestCase (assertEqual "result from either 2" (Err "ok") (AResult.fromEither (Left "ok" :: Either String String)))
    , TestCase (assertEqual "result from maybe 1" (Ok "this") (AResult.fromMaybe (Just "this")))
    , TestCase (assertEqual "result from maybe 2" (Err () :: Result () String) (AResult.fromMaybe Nothing))
    , TestCase (assertEqual "result result 1" "this is ok" (AResult.result (<> " is an err") (<> " is ok") (Ok "this")))
    , TestCase (assertEqual "result result 2" "this is an err" (AResult.result (<> " is an err") (<> " is ok") (Err "this")))
    , TestCase (assertEqual "result map ok 1" (Ok "hello world") (AResult.mapOk (<> " world") (Ok "hello") :: Result String String))
    , TestCase (assertEqual "result map ok 2" (Err "hello") (AResult.mapOk (<> " world") (Err "hello") :: Result String String))
    , TestCase (assertEqual "result map err 1" (Err "hello world") (AResult.mapErr (<> " world") (Err "hello") :: Result String String))
    , TestCase (assertEqual "result map err 2" (Ok "hello") (AResult.mapErr (<> " world") (Ok "hello") :: Result String String))
    , TestCase (assertEqual "result is ok 1" True (AResult.isOk (Ok "this")))
    , TestCase (assertEqual "result is ok 2" False (AResult.isOk (Err "this")))
    , TestCase (assertEqual "result is err 1" False (AResult.isErr (Ok "this")))
    , TestCase (assertEqual "result is err 2" True (AResult.isErr (Err "this")))
    , TestCase (assertEqual "result with default err 1" "ok" (AResult.errWithDefault "default" (Ok "ok")))
    , TestCase (assertEqual "result with default err 2" "default" (AResult.errWithDefault "default" (Err "err")))
    , TestCase (assertEqual "result with default ok 1" "default" (AResult.okWithDefault "default" (Ok "ok")))
    , TestCase (assertEqual "result with default ok 2" "err" (AResult.okWithDefault "default" (Err "err")))
    , TestCase (assertEqual "result filter ok" ["1", "3"] (AResult.filterOks [Ok "1", Err "2", Ok "3", Err "4"]))
    , TestCase (assertEqual "result filter err" ["2", "4"] (AResult.filterErrs [Ok "1", Err "2", Ok "3", Err "4"]))
    , TestCase (assertEqual "result filter err" (["1", "3"], ["2", "4"]) (AResult.partition [Ok "1", Err "2", Ok "3", Err "4"]))
    ]


testString :: Test
testString =
  TestList
    [ TestCase (assertEqual "string split 1" ["00", "01", "10", "11"] (AString.split "--" "00--01--10--11"))
    , TestCase (assertEqual "string split 2" ["00", "01"] (AString.split "--" "00--01--"))
    , TestCase (assertEqual "string split 3" ["00", "01"] (AString.split "--" "--00--01"))
    , TestCase (assertEqual "string split 4" ["-00", "-01-"] (AString.split "--" "-00---01-"))
    , TestCase (assertEqual "string replace 1" "this sentence is fixed" (AString.replace "{replace me}" "fixed" "this sentence is {replace me}"))
    , TestCase (assertEqual "string replace 2" "" (AString.replace "{replace me}" "fixed" ""))
    , TestCase (assertEqual "string replace 3" "this sentence is {replace me}" (AString.replace "" "fixed" "this sentence is {replace me}"))
    , TestCase (assertEqual "string replace 4" "this sentence is " (AString.replace "{replace me}" "" "this sentence is {replace me}"))
    , TestCase (assertEqual "string replace 5" "this sentence is fixed" (AString.replace "{replace me}" "this" "{replace me} sentence is fixed"))
    ]

testStringCase :: Test
testStringCase =
    TestList
        [ TestCase (assertEqual "case identify screaming sentence" (Just ACase.ScreamingSentenceCase) (ACase.identifyCase "SCREAMING SENTENCE"))
        , TestCase (assertEqual "case identify upper sentence" (Just ACase.UpperSentenceCase) (ACase.identifyCase "Upper Sentence"))
        , TestCase (assertEqual "case identify title sentence" (Just ACase.TitleSentenceCase) (ACase.identifyCase "Title sentence"))
        , TestCase (assertEqual "case identify lower sentence" (Just ACase.LowerSentenceCase) (ACase.identifyCase "lower sentence"))
        , TestCase (assertEqual "case identify screaming hyphen" (Just ACase.ScreamingHyphenCase) (ACase.identifyCase "SCREAMING-HYPHEN"))
        , TestCase (assertEqual "case identify upper hyphen" (Just ACase.UpperHyphenCase) (ACase.identifyCase "Upper-Hyphen"))
        , TestCase (assertEqual "case identify lower hyphen" (Just ACase.LowerHyphenCase) (ACase.identifyCase "lower-hyphen"))
        , TestCase (assertEqual "case identify screaming snake" (Just ACase.ScreamingSnakeCase) (ACase.identifyCase "SCREAMING_SNAKE"))
        , TestCase (assertEqual "case identify upper snake" (Just ACase.UpperSnakeCase) (ACase.identifyCase "Upper_Snake"))
        , TestCase (assertEqual "case identify lower snake" (Just ACase.LowerSnakeCase) (ACase.identifyCase "lower_snake"))
        , TestCase (assertEqual "case identify screaming dot" (Just ACase.ScreamingDotCase) (ACase.identifyCase "SCREAMING.DOT"))
        , TestCase (assertEqual "case identify upper dot" (Just ACase.UpperDotCase) (ACase.identifyCase "Upper.Dot"))
        , TestCase (assertEqual "case identify lower dot" (Just ACase.LowerDotCase) (ACase.identifyCase "lower.dot"))
        , TestCase (assertEqual "case identify screaming fpath" (Just ACase.ScreamingForwardPathCase) (ACase.identifyCase "SCREAMING/FORWARD/PATH"))
        , TestCase (assertEqual "case identify upper fpath" (Just ACase.UpperForwardPathCase) (ACase.identifyCase "Upper/Forward/Path"))
        , TestCase (assertEqual "case identify lower fpath" (Just ACase.LowerForwardPathCase) (ACase.identifyCase "lower/forward/path"))
        , TestCase (assertEqual "case identify screaming bpath" (Just ACase.ScreamingBackPathCase) (ACase.identifyCase "SCREAMING\\BACK\\PATH"))
        , TestCase (assertEqual "case identify upper bpath" (Just ACase.UpperBackPathCase) (ACase.identifyCase "Upper\\Back\\Path"))
        , TestCase (assertEqual "case identify lower bpath" (Just ACase.LowerBackPathCase) (ACase.identifyCase "lower\\back\\path"))
        , TestCase (assertEqual "case identify pascal" (Just ACase.PascalCase) (ACase.identifyCase "PascalCase"))
        , TestCase (assertEqual "case identify camel" (Just ACase.CamelCase) (ACase.identifyCase "camelCase"))
        , TestCase (assertEqual "case identify indeterminate" Nothing (ACase.identifyCase "123456789"))
        , TestCase (assertEqual "case recase screaming sentence" (Just "SCREAMING SENTENCE") (ACase.recase ACase.ScreamingSentenceCase "screaming sentence"))
        , TestCase (assertEqual "case recase upper sentence" (Just "Upper Sentence") (ACase.recase ACase.UpperSentenceCase "UPPER_SENTENCE"))
        , TestCase (assertEqual "case recase title sentence" (Just "Title sentence") (ACase.recase ACase.TitleSentenceCase "TitleSentence"))
        , TestCase (assertEqual "case recase lower sentence" (Just "lower sentence") (ACase.recase ACase.LowerSentenceCase "Lower.Sentence"))
        , TestCase (assertEqual "case recase screaming hyphen" (Just "SCREAMING-HYPHEN") (ACase.recase ACase.ScreamingHyphenCase "Screaming Hyphen"))
        , TestCase (assertEqual "case recase upper hyphen" (Just "Upper-Hyphen") (ACase.recase ACase.UpperHyphenCase "upper/hyphen"))
        , TestCase (assertEqual "case recase lower hyphen" (Just "lower-hyphen") (ACase.recase ACase.LowerHyphenCase "LOWER\\HYPHEN"))
        , TestCase (assertEqual "case recase screaming snake" (Just "SCREAMING_SNAKE") (ACase.recase ACase.ScreamingSnakeCase "ScreamingSnake"))
        , TestCase (assertEqual "case recase upper snake" (Just "Upper_Snake") (ACase.recase ACase.UpperSnakeCase "upper.snake"))
        , TestCase (assertEqual "case recase lower snake" (Just "lower_snake") (ACase.recase ACase.LowerSnakeCase "LOWER/SNAKE"))
        , TestCase (assertEqual "case recase screaming dot" (Just "SCREAMING.DOT") (ACase.recase ACase.ScreamingDotCase "Screaming_Dot"))
        , TestCase (assertEqual "case recase upper dot" (Just "Upper.Dot") (ACase.recase ACase.UpperDotCase "Upper dot"))
        , TestCase (assertEqual "case recase lower dot" (Just "lower.dot") (ACase.recase ACase.LowerDotCase "lower.dot"))
        , TestCase (assertEqual "case recase screaming fpath" (Just "SCREAMING/FORWARD/PATH") (ACase.recase ACase.ScreamingForwardPathCase "SCREAMING-FORWARD-PATH"))
        , TestCase (assertEqual "case recase upper fpath" (Just "Upper/Forward/Path") (ACase.recase ACase.UpperForwardPathCase "upper/forward/path"))
        , TestCase (assertEqual "case recase lower fpath" (Just "lower/forward/path") (ACase.recase ACase.LowerForwardPathCase "Lower forward path"))
        , TestCase (assertEqual "case recase screaming bpath" (Just "SCREAMING\\BACK\\PATH") (ACase.recase ACase.ScreamingBackPathCase "screaming.back.path"))
        , TestCase (assertEqual "case recase upper bpath" (Just "Upper\\Back\\Path") (ACase.recase ACase.UpperBackPathCase "upper_back_path"))
        , TestCase (assertEqual "case recase lower bpath" (Just "lower\\back\\path") (ACase.recase ACase.LowerBackPathCase "LOWER BACK PATH"))
        , TestCase (assertEqual "case recase pascal" (Just "PascalCase") (ACase.recase ACase.PascalCase "pascalCase"))
        , TestCase (assertEqual "case recase camel" (Just "camelCase") (ACase.recase ACase.CamelCase "Camel-Case"))
        , TestCase (assertEqual "case recase indeterminate" Nothing (ACase.recase ACase.UpperDotCase "123456789"))
        -- No need to test 'uncase' or 'buildCase', it's clearly working in 'recase'.
        -- Technically don't even need to test 'identifyCase', but why not.
        ]


testTuplesPair :: Test
testTuplesPair =
  TestList
    [ TestCase (assertEqual "tuple pair first" "1" (APair.first ("1", "2")))
    , TestCase (assertEqual "tuple pair second" "2" (APair.second ("1", "2")))
    , TestCase (assertEqual "tuple pair map first" ("13", "2") (APair.mapFirst (<> "3") ("1", "2")))
    , TestCase (assertEqual "tuple pair map second" ("1", "23") (APair.mapSecond (<> "3") ("1", "2")))
    , TestCase (assertEqual "tuple pair swap" ("2", "1") (APair.swap ("1", "2")))
    , TestCase (assertEqual "tuple pair pack" ("1", "2") (APair.pack "1" "2"))
    ]


testTuplesTrio :: Test
testTuplesTrio =
  TestList
    [ TestCase (assertEqual "tuple trio first" "1" (ATrio.first ("1", "2", "3")))
    , TestCase (assertEqual "tuple trio second" "2" (ATrio.second ("1", "2", "3")))
    , TestCase (assertEqual "tuple trio third" "3" (ATrio.third ("1", "2", "3")))
    , TestCase (assertEqual "tuple trio map first" ("14", "2", "3") (ATrio.mapFirst (<> "4") ("1", "2", "3")))
    , TestCase (assertEqual "tuple trio map second" ("1", "24", "3") (ATrio.mapSecond (<> "4") ("1", "2", "3")))
    , TestCase (assertEqual "tuple trio map third" ("1", "2", "34") (ATrio.mapThird (<> "4") ("1", "2", "3")))
    , TestCase (assertEqual "tuple trio cycle clockwise" ("3", "1", "2") (ATrio.cycleCW ("1", "2", "3")))
    , TestCase (assertEqual "tuple trio cycle counterclockwise" ("2", "3", "1") (ATrio.cycleCCW ("1", "2", "3")))
    , TestCase (assertEqual "tuple trio reverse" ("3", "2", "1") (ATrio.reverse ("1", "2", "3")))
    , TestCase (assertEqual "tuple trio pack" ("1", "2", "3") (ATrio.pack "1" "2" "3"))
    ]


testAnteludeBase :: Test
testAnteludeBase =
  TestList
    [ TestCase (assertEqual "" () ())
    ]