From 2e411664ce8da96fd50340b1ecb9e6e2e16f6ca2 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 26 Mar 2016 20:29:15 +0100 Subject: Parse durations --- tests/Test.hs | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/Test.hs b/tests/Test.hs index 641e1e6..3956236 100644 --- a/tests/Test.hs +++ b/tests/Test.hs @@ -18,6 +18,8 @@ import Test.HUnit hiding (Test) import Data.ConfigManager import Data.ConfigManager.Types (Config(..)) import qualified Data.Text as T +import Data.Time.Clock (DiffTime) +import qualified Data.Time.Clock as Time import Helper (forceGetConfig, getConfig, eitherToMaybe) @@ -32,6 +34,7 @@ tests = , testCase "value" valueAssertion , testCase "skip" skipAssertion , testCase "import" importAssertion + , testCase "duration" durationAssertion ] bindingAssertion :: Assertion @@ -41,7 +44,7 @@ bindingAssertion = do oneBinding <- forceGetConfig "x = \"foo\"" assertEqual "one binding present" (Right "foo") (lookup "x" oneBinding) - assertBool "one binding missing" (isLeft $ (lookup "y" oneBinding :: Either Text Int)) + assertBool "one binding missing" (isLeft (lookup "y" oneBinding :: Either Text Int)) assertEqual "one binding count" 1 (M.size . hashMap $ oneBinding) multipleBindings <- forceGetConfig $ T.unlines @@ -133,3 +136,40 @@ importAssertion = do , "x = 4" ] assertEqual "missing optional config" (Right 4) (lookup "x" missingOptionalConfig) + +durationAssertion :: Assertion +durationAssertion = do + config <- forceGetConfig $ T.unlines + [ "a = 1 second" + , "b = 5 seconds" + , "c = 1 minute" + , "d = 10 minutes" + , "e = 1 hour" + , "f = 7 hours" + , "g = 1 day" + , "h = 2 days" + , "i = 1 week" + , "j = 9 weeks" + , "" + , "k = 1 minutes" + , "l = 20 weeks" + ] + + let second = 1 + let minute = 60 * second + let hour = 60 * minute + let day = 24 * hour + let week = 7 * day + + assertEqual "a" (Right (Time.secondsToDiffTime $ 1 * second)) (lookup "a" config) + assertEqual "b" (Right (Time.secondsToDiffTime $ 5 * second)) (lookup "b" config) + assertEqual "c" (Right (Time.secondsToDiffTime $ 1 * minute)) (lookup "c" config) + assertEqual "d" (Right (Time.secondsToDiffTime $ 10 * minute)) (lookup "d" config) + assertEqual "e" (Right (Time.secondsToDiffTime $ 1 * hour)) (lookup "e" config) + assertEqual "f" (Right (Time.secondsToDiffTime $ 7 * hour)) (lookup "f" config) + assertEqual "g" (Right (Time.secondsToDiffTime $ 1 * day)) (lookup "g" config) + assertEqual "h" (Right (Time.secondsToDiffTime $ 2 * day)) (lookup "h" config) + assertEqual "i" (Right (Time.secondsToDiffTime $ 1 * week)) (lookup "i" config) + assertEqual "j" (Right (Time.secondsToDiffTime $ 9 * week)) (lookup "j" config) + assertBool "k" (isLeft (lookup "k" config :: Either Text DiffTime)) + assertBool "l" (isLeft (lookup "l" config :: Either Text DiffTime)) -- cgit v1.2.3