From d1446a8435a3cf06371eb6d4ebe25d6491612f4d Mon Sep 17 00:00:00 2001 From: stuebinm Date: Wed, 22 May 2024 00:04:30 +0200 Subject: a generic, multi-source config interface --- test/Main.hs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/Main.hs (limited to 'test/Main.hs') diff --git a/test/Main.hs b/test/Main.hs new file mode 100644 index 0000000..fa6a3fb --- /dev/null +++ b/test/Main.hs @@ -0,0 +1,37 @@ +{-# LANGUAGE OverloadedLists #-} +{-# LANGUAGE OverloadedStrings #-} +module Main (main) where + +import Conftrack +import Conftrack.Value +import Conftrack.Source + +import qualified Data.Map.Strict as M +import Data.Text (Text) +import Control.Exception (assert) + +data TestType = TestType { testFoo :: Text, testBar :: Integer } + deriving (Show, Eq) + +instance Config TestType where + readConfig = TestType + <$> readValue (Key ["foo"]) + <*> readValue (Key ["bar"]) + +main :: IO () +main = do + let trivial = Trivial (M.fromList [(Key ["foo"], ConfigText "foo"), (Key ["bar"], ConfigInteger 10)]) + Right (config :: TestType, _, _) <- runFetchConfig [SomeSource (trivial, [])] + () <- assert (config == TestType "foo" 10) (pure ()) + + let trivial = Trivial (M.fromList [(Key ["fo"], ConfigText "foo"), (Key ["bar"], ConfigInteger 10)]) + Left [NotPresent] <- runFetchConfig @TestType [SomeSource (trivial, [])] + + let stack1 = Trivial (M.fromList [(Key ["foo"], ConfigText "foo")]) + let stack2 = Trivial (M.fromList [(Key ["bar"], ConfigInteger 10), (Key ["foo"], ConfigText "blub")]) + Right (config :: TestType, origins, warnings) <- runFetchConfig [SomeSource (stack1, []), SomeSource (stack2, [])] + () <- assert (config == TestType "foo" 11) (pure ()) + print origins + print warnings + + print config -- cgit v1.2.3