File: ErrorTests.hs

package info (click to toggle)
haskell-hslua-core 2.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 288 kB
  • sloc: haskell: 2,243; makefile: 3
file content (79 lines) | stat: -rw-r--r-- 2,778 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
{-# LANGUAGE DeriveDataTypeable   #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications     #-}
{-| Tests for error handling.
-}
module HsLua.Core.ErrorTests (tests) where

import Control.Applicative ((<|>), empty)
import Control.Exception
import Data.ByteString (ByteString)
import Data.Typeable (Typeable)
import Data.Either (isLeft)
import HsLua.Core (Lua, failLua)
import HsLua.Core.Error ( LuaError, changeErrorType, popErrorMessage
                        , throwTypeMismatchError)
import HsLua.Core.Types (liftLua)
import Test.Tasty.HsLua ( (=:), (?:), shouldBeResultOf, shouldHoldForResultOf
                        , shouldBeErrorMessageOf)
import Test.Tasty (TestTree, testGroup)

import qualified HsLua.Core as Lua
import qualified HsLua.Core.Utf8 as Utf8

-- | Specifications for Attributes parsing functions.
tests :: TestTree
tests = testGroup "Error"
  [ "try catches errors" =:
    isLeft `shouldHoldForResultOf` Lua.try
      (failLua "test" :: Lua ())

  , "second alternative is used when first fails" ?:
    ((failLua "test" :: Lua Bool) <|> return True)

  , "Applicative.empty implementation throws an exception" =:
    isLeft `shouldHoldForResultOf` Lua.try (empty :: Lua ())

  , testGroup "changeErrorType"
    [ "catches error as different type in argument operation" =:
      Left (SampleException "message") `shouldBeResultOf`
      changeErrorType (Lua.try @SampleException @() $ failLua "message")

    , "passes value through on success" =:
      Just "plant" `shouldBeResultOf` do
        Lua.pushstring "plant"
        changeErrorType (Lua.tostring Lua.top)
    ]

  , testGroup "type mismatch"
    [ "got string" =:
      "number expected, got string" `shouldBeErrorMessageOf` do
        Lua.pushstring "moin"
        throwTypeMismatchError "number" Lua.top :: Lua ()
    , "got unnamed userdata" =:
      "number expected, got userdata" `shouldBeErrorMessageOf` do
        Lua.newhsuserdatauv () 0
        throwTypeMismatchError "number" Lua.top :: Lua ()
    , "named userdata" =:
      "Bar expected, got Foo" `shouldBeErrorMessageOf` do
        Lua.newhsuserdatauv () 0
        Lua.newudmetatable "Foo"
        Lua.setmetatable (Lua.nth 2)
        throwTypeMismatchError "Bar" Lua.top :: Lua ()

    , "missing value" =:
      "boolean expected, got no value" `shouldBeErrorMessageOf` do
        curtop <- Lua.gettop
        throwTypeMismatchError "boolean" (curtop + 1) :: Lua ()
    ]
  ]

newtype SampleException = SampleException ByteString
  deriving (Eq, Typeable, Show)

instance Exception SampleException

instance LuaError SampleException where
  popException = SampleException <$> liftLua popErrorMessage
  pushException (SampleException msg) = Lua.pushstring msg
  luaException = SampleException . Utf8.fromString