forked from Plutonomicon/cardano-transaction-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogging.purs
76 lines (73 loc) · 2.49 KB
/
Logging.purs
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
module Test.Ctl.Testnet.Logging
( suite
) where
import Prelude
import Contract.Log (logWarn')
import Contract.Test.Testnet (defaultTestnetConfig, runTestnetContract)
import Data.Log.Level (LogLevel(Error))
import Data.Maybe (Maybe(Just))
import Effect.Aff (Aff, try)
import Effect.Class (liftEffect)
import Effect.Exception (throw)
import Effect.Ref as Ref
import Mote (group, test)
import Mote.TestPlanM (TestPlanM)
import Test.Spec.Assertions (shouldEqual)
suite :: TestPlanM (Aff Unit) Unit
suite = do
group "Logging" do
test "Logs that are not suppressed really appear" do
hasLogged <- liftEffect $ Ref.new false
let
config' =
defaultTestnetConfig
{ customLogger = Just
\_ _ -> liftEffect $ Ref.write true hasLogged
, suppressLogs = false
}
runTestnetContract config' unit \_ -> do
logWarn' ""
hasLoggedResult <- liftEffect $ Ref.read hasLogged
hasLoggedResult `shouldEqual` true
test "Suppressed logs do not appear when no error" do
hasLogged <- liftEffect $ Ref.new false
let
config' =
defaultTestnetConfig
{ customLogger = Just
\_ _ -> liftEffect $ Ref.write true hasLogged
, suppressLogs = true
}
runTestnetContract config' unit \_ -> do
logWarn' ""
hasLoggedResult <- liftEffect $ Ref.read hasLogged
hasLoggedResult `shouldEqual` false
test "Suppressed logs appear on error" do
hasLogged <- liftEffect $ Ref.new false
let
config' =
defaultTestnetConfig
{ customLogger = Just
\_ _ -> liftEffect $ Ref.write true hasLogged
, suppressLogs = true
}
void $ try $ runTestnetContract config' unit \_ -> do
logWarn' ""
liftEffect $ throw "Exception"
hasLoggedResult <- liftEffect $ Ref.read hasLogged
hasLoggedResult `shouldEqual` true
test "CustomLogger, filtered by LogLevel, does not log" do
hasLogged <- liftEffect $ Ref.new false
let
config' =
defaultTestnetConfig
{ customLogger = Just writeLog
, suppressLogs = false
, logLevel = Error
}
writeLog lgl m = liftEffect $ when (m.level >= lgl) $ do
Ref.write true hasLogged
runTestnetContract config' unit \_ -> do
logWarn' ""
hasLoggedResult <- liftEffect $ Ref.read hasLogged
hasLoggedResult `shouldEqual` false