2022-10-28 23:27:31 +03:00
|
|
|
module Test.Main where
|
|
|
|
|
2022-10-30 02:31:32 +03:00
|
|
|
import Prelude (Unit, ($), (==), (>>=), unit, pure, bind, const)
|
2022-10-29 19:26:19 +03:00
|
|
|
import Control.Apply ((*>))
|
2022-10-30 04:11:19 +03:00
|
|
|
import Control.Monad.Error.Class (throwError, liftMaybe)
|
2022-10-30 02:31:32 +03:00
|
|
|
import Control.Monad.Reader.Trans (ReaderT, ask, runReaderT)
|
2022-10-31 17:29:54 +03:00
|
|
|
import Control.Monad.Trans.Class (lift)
|
2022-10-28 23:27:31 +03:00
|
|
|
import Effect
|
2022-10-29 03:13:54 +03:00
|
|
|
import Effect.Aff (Aff, bracket)
|
|
|
|
import Effect.Class (liftEffect)
|
2022-10-30 04:11:19 +03:00
|
|
|
import Effect.Exception (error)
|
2022-10-29 19:26:19 +03:00
|
|
|
import HTTPure.Request (Request)
|
2022-10-29 03:13:54 +03:00
|
|
|
import HTTPure.Response (ok)
|
|
|
|
import HTTPure.Server (serve)
|
|
|
|
import HTTPure.Body (class Body)
|
2022-10-29 19:26:19 +03:00
|
|
|
import HTTPure.Lookup ((!!))
|
2022-10-30 02:31:32 +03:00
|
|
|
import HTTPure.Method (Method(Post))
|
2022-10-29 03:13:54 +03:00
|
|
|
import Test.Unit (suite, test)
|
|
|
|
import Test.Unit.Main (runTest)
|
|
|
|
import Test.Unit.Assert (equal)
|
2022-10-31 17:29:54 +03:00
|
|
|
import Main (Settings, Code, runCompiler)
|
2022-10-29 03:13:54 +03:00
|
|
|
|
2022-10-31 17:29:54 +03:00
|
|
|
type ServerMock = ReaderT Code (ReaderT Request Aff)
|
|
|
|
|
|
|
|
askCode :: ServerMock Code
|
|
|
|
askCode = ask
|
|
|
|
|
|
|
|
askReq :: ServerMock Request
|
|
|
|
askReq = lift ask
|
2022-10-30 02:31:32 +03:00
|
|
|
|
2022-10-30 04:11:19 +03:00
|
|
|
settings :: Settings
|
|
|
|
settings = { protocol: "http", hostname: "localhost", port: 3000 }
|
|
|
|
|
2022-10-31 17:29:54 +03:00
|
|
|
validatePath :: ServerMock Unit
|
2022-10-30 02:31:32 +03:00
|
|
|
validatePath = do
|
2022-10-29 19:26:19 +03:00
|
|
|
let invalidPath = error "invalid path"
|
|
|
|
missingPath = error "missing path"
|
2022-10-31 17:29:54 +03:00
|
|
|
req <- askReq
|
2022-10-30 01:51:50 +03:00
|
|
|
p <- liftMaybe missingPath $ req.path !! 0
|
|
|
|
case p == "compile" of
|
2022-10-29 19:26:19 +03:00
|
|
|
true -> pure unit
|
|
|
|
false -> throwError invalidPath
|
|
|
|
|
2022-10-31 17:29:54 +03:00
|
|
|
validateMethod :: ServerMock Unit
|
|
|
|
validateMethod = askReq >>= case _ of
|
2022-10-30 02:31:32 +03:00
|
|
|
{ method: Post } -> pure unit
|
|
|
|
_ -> throwError $ error "invalid method"
|
|
|
|
|
2022-10-31 17:29:54 +03:00
|
|
|
runServerMock :: Code -> Request -> Aff Unit
|
|
|
|
runServerMock res req = runReaderT (runReaderT (validatePath *> validateMethod) res) req
|
2022-10-30 02:31:32 +03:00
|
|
|
|
2022-10-31 17:29:54 +03:00
|
|
|
launchServerMock :: Code -> Aff (Effect Unit)
|
|
|
|
launchServerMock res = do
|
|
|
|
close <- liftEffect $ serve settings.port (\req -> runServerMock res req *> ok res) $ pure unit
|
2022-10-29 03:13:54 +03:00
|
|
|
pure $ close $ pure unit
|
|
|
|
|
2022-10-31 17:29:54 +03:00
|
|
|
setupSrv :: forall b. Code -> Aff b -> Aff b
|
|
|
|
setupSrv code act = bracket (launchServerMock code) liftEffect (const act)
|
2022-10-28 23:27:31 +03:00
|
|
|
|
|
|
|
main :: Effect Unit
|
2022-10-29 03:13:54 +03:00
|
|
|
main = runTest do
|
|
|
|
suite "compile" do
|
|
|
|
test "produces expected output" do
|
2022-10-31 16:58:19 +03:00
|
|
|
let expected = "compiled-code"
|
2022-10-30 04:11:19 +03:00
|
|
|
compile = runCompiler settings
|
2022-10-31 16:58:19 +03:00
|
|
|
actual <- setupSrv expected (compile "code")
|
2022-10-30 01:51:50 +03:00
|
|
|
expected `equal` actual
|