File: test-hslua-module-text.hs

package info (click to toggle)
haskell-hslua-module-text 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 80 kB
  • sloc: haskell: 194; makefile: 6
file content (54 lines) | stat: -rw-r--r-- 1,691 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications  #-}
{-|
Module      : Main
Copyright   : © 2017-2024 Albert Krewinkel
License     : MIT
Maintainer  : Albert Krewinkel <tarleb@hslua.org>
Stability   : stable
Portability : Requires language extensions ForeignFunctionInterface,
              OverloadedStrings.

Tests for the `text` Lua module.
-}
module Main (main) where

import Control.Monad (void)
import HsLua.Core as Lua
import HsLua.Packaging
import HsLua.Module.Text (documentedModule)
import Test.Tasty (TestTree, defaultMain, testGroup)
import Test.Tasty.HUnit (assertEqual, testCase)
import Test.Tasty.Lua (translateResultsFromFile)

main :: IO ()
main = do
  luaTest <- run @Lua.Exception $ do
    openlibs
    registerModule documentedModule
    pop 1
    translateResultsFromFile "test/test-text.lua"
  defaultMain $ testGroup "hslua-module-text" [tests, luaTest]

-- | HSpec tests
tests :: TestTree
tests = testGroup "FromLuaStack"
  [ testCase "text module can be pushed to the stack" $
      run (void (pushModule documentedModule) :: Lua ())

  , testCase "text module can be added to the preloader" . run $ do
      openlibs
      preloadModule documentedModule
      assertEqual' "function not added to preloader" TypeFunction =<< do
        _ <- dostring "return package.preload.text"
        ltype top

  , testCase "text module can be loaded as hstext" . run $ do
      openlibs
      preloadModuleWithName documentedModule "hstext"
      assertEqual' "loading the module fails " OK =<<
        dostring "require 'hstext'"
  ]

assertEqual' :: (Show a, Eq a) => String -> a -> a -> Lua ()
assertEqual' msg expected = liftIO . assertEqual msg expected