purescript-email-address/test/Test.Main.purs

78 lines
2.6 KiB
Haskell
Raw Permalink Normal View History

2023-12-06 20:27:41 +00:00
module Test.Main where
import Prelude
import Control.Monad.Error.Class (class MonadThrow, liftEither, throwError, try)
import Control.Monad.Except (runExceptT)
import Data.Bifunctor (lmap)
import Data.Either (Either(..), isRight)
import Data.Email (Email)
import Data.Email as Email
import Data.Traversable (for_)
import Data.Tuple.Nested ((/\))
import Effect (Effect)
import Effect.Aff (launchAff_)
import Effect.Email as Effect.Email
import Effect.Exception (Error, error)
import Test.Spec (describe, it)
import Test.Spec.Assertions (shouldEqual)
import Test.Spec.Reporter (consoleReporter)
import Test.Spec.Runner (runSpec)
emailParse :: forall m. MonadThrow Error m => String -> m Email
emailParse = liftEither <<< lmap error <<< Email.parse
main :: Effect Unit
main = launchAff_ $ runSpec [ consoleReporter ] do
describe "Data.Email" do
describe "parse" do
for_
[ "orion@orionkindel.com" /\ true
, "foo.bar@gmail.com" /\ true
, "foo.bar123@gmail.com" /\ true
, "foo-bar@gmail.com" /\ true
, "foo_bar@gmail.com" /\ true
, "foo_@gmail.com" /\ true
, "foo.bar+1@gmail.com" /\ true
, "_foo@gmail.com" /\ true
, "foo,bar@gmail.com" /\ false
, "foo@gmail" /\ false
, "foo@" /\ false
, "foo.com" /\ false
, "@foo.com" /\ false
]
\(e /\ pass) ->
it
((if pass then "pass" else "fail") <> " > `" <> e <> "`")
(isRight (Email.parse e) `shouldEqual` pass)
describe "toString" do
for_
[ "orion@orionkindel.com"
, "foo.bar@gmail.com"
, "foo.bar123@gmail.com"
, "foo-bar@gmail.com"
, "foo_bar@gmail.com"
, "foo_@gmail.com"
, "foo.bar+1@gmail.com"
, "_foo@gmail.com"
]
\e -> it ("`" <> e <> "`") $ flip shouldEqual e <$> Email.toString =<< emailParse e
describe "Effect.Email" do
describe "deliverable" do
for_
[ "cakekindel@gmail.com" /\ true
, "george@thunderstrike.ai" /\ true
, "foobarbaz@thunderstrike.ai" /\ false
]
\(e /\ shouldPass) ->
it ((if shouldPass then "pass" else "fail") <> " > `" <> e <> "`") do
email <- emailParse e
result <- runExceptT $ Effect.Email.deliverable email
case result of
Right _
| not shouldPass -> throwError $ error "deliverable did not fail but should have!"
| otherwise -> pure unit
Left err
| shouldPass -> throwError $ error $ Effect.Email.emailErrorToString err
| otherwise -> pure unit