From c1cf159fa67a107a395166c199c401aac0918c31 Mon Sep 17 00:00:00 2001 From: stuebinm Date: Fri, 7 Jun 2024 22:36:40 +0200 Subject: the Config interface should be Applicative, not Monad this allows for safer alternative uses for it, such as running it on an empty list of config sources to just get the list of keys it uses. --- test/Main.hs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/Main.hs b/test/Main.hs index 5c01f64..1af3d9a 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -2,6 +2,7 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE TemplateHaskell #-} +{-# LANGUAGE ApplicativeDo #-} module Main (main) where import Conftrack @@ -15,6 +16,7 @@ import Test.QuickCheck.Monadic import Test.QuickCheck.Instances () import System.Exit (exitFailure, exitSuccess) import qualified Data.Text.Encoding as BS +import Data.List ((\\)) data TestFlat = TestType { testFoo :: Text, testBar :: Integer } @@ -35,9 +37,10 @@ instance Config TestFlat where <*> readRequiredValue (Key ["bar"]) instance Config TestNested where - readConfig = TestNested - <$> readRequiredValue (Key ["foo"]) - <*> readNested (Key ["nested"]) + readConfig = do + a <- readRequiredValue (Key ["foo"]) + b <- readNested (Key ["nested"]) + pure (TestNested a b) testTypeToTrivial :: TestFlat -> SomeSource testTypeToTrivial (TestType foo bar) = mkTrivialSource @@ -81,6 +84,16 @@ prop_aeson_flat = roundtripVia testTypeToJson prop_aeson_nested :: TestNested -> Property prop_aeson_nested = roundtripVia nestedToJson +prop_flat_keys :: Property +prop_flat_keys = monadicIO $ do + keys <- run $ configKeysOf @TestFlat + assert (null (keys \\ [ Key ["foo"], Key ["bar"] ])) + +prop_nested_keys :: Property +prop_nested_keys = monadicIO $ do + keys <- run $ configKeysOf @TestNested + assert (null (keys \\ [ Key ["foo"], Key ["nested", "bar"], Key ["nested", "foo"] ])) + -- see quickcheck docs for why this return is here return [] runTests = $quickCheckAll -- cgit v1.2.3