aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoris2016-03-19 17:22:12 +0100
committerJoris2016-03-19 17:24:13 +0100
commit89a1cbdbb440a339552e76a79d3a0acb000e5705 (patch)
tree2f66bbfd8dea855ffa528ac2e0500ebd35fccee9
parent4c67e2c9dc817fb9d63b0951fbdc59252e6b8941 (diff)
Fix lookupDefault
-rw-r--r--Data/ConfigManager.hs5
-rw-r--r--tests/Test.hs7
2 files changed, 10 insertions, 2 deletions
diff --git a/Data/ConfigManager.hs b/Data/ConfigManager.hs
index edc2e93..038276e 100644
--- a/Data/ConfigManager.hs
+++ b/Data/ConfigManager.hs
@@ -35,6 +35,7 @@ import Control.Monad (join)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.HashMap.Strict as M
+import Data.Maybe (fromMaybe)
import qualified Data.ConfigManager.Reader as R
import Data.ConfigManager.Types
@@ -52,8 +53,8 @@ lookup name config = join . fmap (readMaybe . T.unpack) $ M.lookup name (hashMap
-- | Lookup for the value associated to a name and return the default value if
-- no binding exists with the given name.
-lookupDefault :: Value -> Name -> Config -> Value
-lookupDefault defaultValue name config = M.lookupDefault defaultValue name (hashMap config)
+lookupDefault :: Read a => a -> Name -> Config -> a
+lookupDefault defaultValue name config = fromMaybe defaultValue $ lookup name config
-- $format
--
diff --git a/tests/Test.hs b/tests/Test.hs
index 03dc979..03f9d39 100644
--- a/tests/Test.hs
+++ b/tests/Test.hs
@@ -25,6 +25,7 @@ main = defaultMain tests
tests :: [Test]
tests =
[ testCase "binding" bindingAssertion
+ , testCase "lookupDefault" lookupDefaultAssertion
, testCase "name" nameAssertion
, testCase "value" valueAssertion
, testCase "skip" skipAssertion
@@ -57,6 +58,12 @@ bindingAssertion = do
assertEqual "overlapping bindings count" 2 (M.size . hashMap $ overlappingBindings)
assertEqual "overlapping bindings redefinition" (Just "baz") (lookup "x" overlappingBindings :: Maybe String)
+lookupDefaultAssertion :: Assertion
+lookupDefaultAssertion = do
+ config <- forceGetConfig "x = 5"
+ assertEqual "x" 5 (lookupDefault 10 "x" config :: Int)
+ assertEqual "y" 10 (lookupDefault 10 "y" config :: Int)
+
nameAssertion :: Assertion
nameAssertion = do
validNames <- forceGetConfig $ T.unlines