@@ -1,5 +1,6 @@
11{-# LANGUAGE GeneralizedNewtypeDeriving #-}
22{-# LANGUAGE OverloadedStrings #-}
3+{-# LANGUAGE RecordWildCards #-}
34{-# LANGUAGE ScopedTypeVariables #-}
45{-# LANGUAGE StandaloneDeriving #-}
56{-# OPTIONS_GHC -fno-warn-orphans #-}
@@ -11,6 +12,9 @@ module Katip.Tests
1112-------------------------------------------------------------------------------
1213import Control.Applicative
1314import Data.Aeson
15+import qualified Data.HashMap.Strict as HM
16+import Data.Monoid
17+import Data.Text (Text)
1418import qualified Data.Text.Lazy.Builder as B
1519import Data.Time
1620import Data.Time.Clock.POSIX
@@ -37,9 +41,49 @@ tests = testGroup "Katip"
3741 textToProcessID (processIDToText pid) === Just pid
3842 , testCase "processIDToText is just the number" $ do
3943 processIDToText 123 @?= "123"
44+ , logContextsTests
4045 ]
4146
4247
48+-------------------------------------------------------------------------------
49+logContextsTests :: TestTree
50+logContextsTests = testGroup "logContexts"
51+ [
52+ testCase "overwrites with the right-hand side (right-bias)" $ do
53+ let l1 = liftPayload (SimpleLogPayload [("foo", AnyLogPayload ("a" :: Text))])
54+ l2 = liftPayload (SimpleLogPayload [("foo", AnyLogPayload ("b" :: Text))])
55+ l3 = liftPayload (SimpleLogPayload [("foo", AnyLogPayload ("c" :: Text))])
56+ both = l1 <> l2 <> l3
57+ toObject both @?= HM.singleton "foo" (String "c")
58+ , testCase "respects payloadKeys for each constituent payload" $ do
59+ let everything = liftPayload (SimpleLogPayload [("foo", AnyLogPayload ("a" :: Text))])
60+ conservative = liftPayload (ConservativePayload "always" "rarely")
61+ both = everything <> conservative
62+ payloadKeys V2 both @?= SomeKeys ["often_shown", "rarely_shown", "foo"]
63+ payloadKeys V1 both @?= SomeKeys ["often_shown", "foo"]
64+ ]
65+
66+
67+-------------------------------------------------------------------------------
68+data ConservativePayload = ConservativePayload {
69+ oftenShown :: Text
70+ , rarelyShown :: Text
71+ }
72+
73+
74+instance ToJSON ConservativePayload where
75+ toJSON ConservativePayload {..} = object ["often_shown" .= oftenShown
76+ ,"rarely_shown" .= rarelyShown]
77+
78+
79+instance ToObject ConservativePayload
80+
81+
82+instance LogItem ConservativePayload where
83+ payloadKeys V1 _ = SomeKeys ["often_shown"]
84+ payloadKeys V0 _ = SomeKeys []
85+ payloadKeys _ _ = AllKeys
86+
4387-------------------------------------------------------------------------------
4488prop_json_cycle :: (ToJSON a, FromJSON a, Eq a, Show a) => a -> Property
4589prop_json_cycle a = eitherDecode (encode a) === Right a