-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.hs
72 lines (59 loc) · 2.4 KB
/
Utils.hs
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
module Utils where
import Data.Function ((&))
import Data.List.NonEmpty (NonEmpty (..))
import Effectful
import Effectful.Error.Static (Error)
import Effectful.Error.Static qualified as Error
import Effectful.FileSystem (FileSystem)
import Effectful.FileSystem qualified as FileSystem
import GHC.Stack
import Test.Tasty (TestTree)
import Test.Tasty qualified as Test
import Test.Tasty.HUnit qualified as Test
import Confer.CLI.Errors
import Confer.Effect.Symlink
type TestEff =
Eff [FileSystem, Error SymlinkError, IOE]
runTestEff
:: Eff [FileSystem, Error SymlinkError, IOE] a
-> IO a
runTestEff action = do
result <-
action
& FileSystem.runFileSystem
& Error.runErrorNoCallStack
& runEff
case result of
Left e -> Test.assertFailure $ show e
Right a -> pure a
assertFailure :: HasCallStack => MonadIO m => String -> m ()
assertFailure = liftIO . Test.assertFailure
assertWrongTarget :: HasCallStack => Either SymlinkError () -> TestEff ()
assertWrongTarget (Right _) = assertFailure "Did not return Left"
assertWrongTarget (Left (WrongTarget{})) = pure ()
assertWrongTarget (Left e) = assertFailure $ "Returned: " <> show e
assertDoesNotExist :: HasCallStack => Either SymlinkError () -> TestEff ()
assertDoesNotExist (Right _) = assertFailure "Did not return Left"
assertDoesNotExist (Left (DoesNotExist{})) = pure ()
assertDoesNotExist (Left e) = assertFailure $ "Returned: " <> show e
assertIsNotSymlink :: HasCallStack => Either SymlinkError () -> TestEff ()
assertIsNotSymlink (Right _) = assertFailure "Did not return Left"
assertIsNotSymlink (Left (IsNotSymlink{})) = pure ()
assertIsNotSymlink (Left e) = assertFailure $ "Returned: " <> show e
assertRight :: HasCallStack => Either a b -> TestEff b
assertRight (Left _a) = liftIO $ Test.assertFailure "Test return Left instead of Right"
assertRight (Right b) = pure b
testThis :: String -> TestEff () -> TestEff TestTree
testThis name assertion = do
let test = runTestEff assertion
pure $
Test.testCase name test
testThese :: String -> [TestEff TestTree] -> TestEff TestTree
testThese groupName tests = fmap (Test.testGroup groupName) newTests
where
newTests :: TestEff [TestTree]
newTests = sequenceA tests
assertBool :: Bool -> TestEff ()
assertBool boolean = liftIO $ Test.assertBool "" boolean
assertEqual :: (Eq a, Show a) => a -> a -> TestEff ()
assertEqual actual expected = liftIO $ Test.assertEqual "" actual expected