File: test-tasty-lua.hs

package info (click to toggle)
haskell-tasty-lua 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104 kB
  • sloc: haskell: 289; makefile: 3
file content (85 lines) | stat: -rw-r--r-- 2,588 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-|
Module      : Main
Copyright   : © 2019-2024 Albert Krewinkel
License     : MIT
Maintainer  : Albert Krewinkel <tarleb@hslua.org>

Tests for the @tasty@ Lua module.
-}

import Control.Monad (void)
import HsLua.Core (Lua)
import Lua.Arbitrary ()
import System.Directory (withCurrentDirectory)
import System.FilePath ((</>))
import Test.QuickCheck (Arbitrary (arbitrary))
import Test.Tasty (TestTree, defaultMain, testGroup)
import Test.Tasty.HUnit (assertEqual, testCase)
import Test.Tasty.Lua
  (pushModule, registerArbitrary, testLuaFile, translateResultsFromFile)

import qualified HsLua.Core as Lua
import qualified HsLua.Marshalling as Lua

main :: IO ()
main = do
  luaTest <- withCurrentDirectory "test" . Lua.run @Lua.Exception $ do
    registerCustom
    translateResultsFromFile "test-tasty.lua"
  defaultMain $ testGroup "tasty-hslua" [luaTest, tests]

-- | HSpec tests for the Lua 'system' module
tests :: TestTree
tests = testGroup "HsLua tasty module"
  [ testCase "can be pushed to the stack" . Lua.run $ do
      Lua.openlibs
      void pushModule :: Lua ()

  , testCase "can be added to the preloader" . Lua.run $ do
      Lua.openlibs
      Lua.preloadhs "tasty" pushModule
      assertEqual' "function not added to preloader" Lua.TypeFunction =<< do
        Lua.loadstring "return package.preload.tasty" *> Lua.call 0 1
        Lua.ltype (-1)

  , testCase "can be loaded as tasty" . Lua.run $ do
      Lua.openlibs
      Lua.requirehs "tasty" (const $ void pushModule)
      assertEqual' "loading the module fails " Lua.OK =<<
        Lua.dostring "require 'tasty'"

  , testGroup "testFileWith"
    [ testLuaFile
      (\x -> Lua.run @Lua.Exception $ do
        registerCustom
        x)
      "test-tasty.lua" ("test" </> "test-tasty.lua")
    ]
  ]

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

registerCustom :: Lua ()
registerCustom = do
  registerArbitrary "custom" pushCustom nopeek
  registerArbitrary @[Integer] "integer_list"
    (Lua.pushList Lua.pushIntegral) (Lua.peekList Lua.peekIntegral)

-- | Custom type used for to check property testing.
newtype Custom = Custom Lua.Integer

instance Arbitrary Custom where
  arbitrary = Custom <$> arbitrary

pushCustom :: Lua.LuaError e => Lua.Pusher e Custom
pushCustom (Custom i) = do
  Lua.newtable
  Lua.pushName "int"
  Lua.pushinteger i
  Lua.rawset (Lua.nth 3)

nopeek :: Lua.Peeker e a
nopeek = const $ Lua.failPeek "nope"  -- do not allow peeking