packages feed

himari-1.1.6.4: test/Spec/Himari/Env/SimpleSpec.hs

{-# LANGUAGE TemplateHaskell #-}

module Spec.Himari.Env.SimpleSpec (tests) where

import Himari

-- sandwichの`logError`はTemplate Haskellを使わない別の関数で、
-- `Himari`がre-exportしているmonad-loggerの`logError`と名前が衝突します。
import Test.Sandwich hiding (logError)

tests :: TopSpec
tests = describe "Simple" $ do
  -- テスト実行時に実際のターミナルへログを出力すると、
  -- Errorなどがログディスプレイでハイライトされて紛らわしいので、
  -- ログの出力を検証しないテストでは`runSimpleNoLogEnv`で出力を破棄する。
  describe "logging" $ do
    it "outputs debug level logs" . runSimpleNoLogEnv $ do
      $(logDebug) "debug message"

    it "outputs info level logs" . runSimpleNoLogEnv $ do
      $(logInfo) "info message"

    it "outputs warning level logs" . runSimpleNoLogEnv $ do
      $(logWarn) "warning message"

    it "outputs error level logs" . runSimpleNoLogEnv $ do
      $(logError) "error message"

    it "handles multiple log calls" . runSimpleNoLogEnv $ do
      $(logInfo) "first log"
      $(logInfo) "second log"
      $(logDebug) "third log"

    it "works with monadic composition" $ do
      result <- runSimpleNoLogEnv $ do
        $(logInfo) "starting computation"
        let x = 20 :: Int
        $(logDebug) ("intermediate value: " <> convert (show x))
        let y = x + 22
        $(logInfo) "computation complete"
        pure y
      result `shouldBe` 42

    it "supports monad-logger functions" . runSimpleNoLogEnv $ do
      logInfoN "using logInfoN"
      logDebugN "using logDebugN"
      logWarnN "using logWarnN"

  describe "custom log output" $ do
    it "can capture log messages" $ do
      chan :: TChan Text <- newTChanIO
      let customLogOutput _loc _src _level msg = do
            let msgText = convert (fromLogStr msg)
            atomically $ writeTChan chan msgText
      runSimpleEnvWith customLogOutput $ do
        $(logInfo) "custom output test"
      message <- atomically $ readTChan chan
      message `shouldBe` "custom output test"

    it "captures log level information" $ do
      chan :: TChan (Text, LogLevel) <- newTChanIO
      let customLogOutput _loc _src level msg = do
            let msgText = convert (fromLogStr msg)
            atomically $ writeTChan chan (msgText, level)
      runSimpleEnvWith customLogOutput $ do
        $(logError) "error log"
        $(logWarn) "warning log"
      msg1 <- atomically $ readTChan chan
      msg2 <- atomically $ readTChan chan
      [msg1, msg2] `shouldBe` [("error log", LevelError), ("warning log", LevelWarn)]

  describe "basic execution" $ do
    it "executes actions successfully" $ do
      result <- runSimpleEnv $ pure (42 :: Int)
      result `shouldBe` 42

    it "can access and use the environment" $ do
      result <- runSimpleNoLogEnv $ do
        $(logInfo) "computing result"
        pure (10 + 32 :: Int)
      result `shouldBe` 42

  describe "Magnify instance" $ do
    it "works with magnify to access a sub-environment" $ do
      let
        -- 小さな環境 (Text) を要求するアクション
        subAction :: Himari Text Text
        subAction = do
          env <- ask
          pure $ env <> " world"
        -- 大きな環境 (Int, Text)
        bigEnv :: (Int, Text)
        bigEnv = (100, "hello")
      -- (Int, Text) の環境下で、_2 レンズを使って Text にズームして実行
      -- これがコンパイル・実行できれば Effect IO へのマッピングは成功している
      result <- runHimari bigEnv $ magnify _2 subAction
      result `shouldBe` "hello world"