Skip to content

Instantly share code, notes, and snippets.

@mookerji
Last active December 17, 2015 05:04
Show Gist options
  • Save mookerji/d6c9fe48bc35e16bd569 to your computer and use it in GitHub Desktop.
Save mookerji/d6c9fe48bc35e16bd569 to your computer and use it in GitHub Desktop.
Environmental fails
import Test.QuickCheck
import Test.QuickCheck.Instances.Maybe
import Test.QuickCheck.Monadic
instance Arbitrary LogLevel where
arbitrary = do
n <- choose (0, 4) :: Gen Int
return $ case n of
0 -> LevelDebug
1 -> LevelInfo
2 -> LevelWarn
3 -> LevelError
4 -> LevelOther "other"
instance Arbitrary Conf where
arbitrary = Conf <$> maybeGen nonulls
<*> maybeGen arbitrary
<*> maybeGen arbitrary
<*> maybeGen arbitrary
where nonempty = getNonEmpty <$> arbitrary
nonulls = nonempty `suchThat` (not . ('\NUL' `elem`))
testEnvProperties :: TestTree
testEnvProperties = testGroup "(checked by QuickCheck)"
[ QC.testProperty "Log Level environmental configuration" $
\(x :: LogLevel) -> Just x == fromVar (toVar x)
, QC.testProperty "Configuration environmental configuration" $
\(c :: Conf) -> monadicIO $ do
res <- run $ do
let Conf{..} = c
set k v = maybe (unsetEnv k) (\x -> setEnv k $ (unpack . show) x) v
set "SKYLARK_CONFFILE" _cConfFile
set "SKYLARK_PORT" _cPort
set "SKYLARK_TIMEOUT" _cTimeout
set "SKYLARK_LOGLEVEL" _cLogLevel
decodeEnv
Test.QuickCheck.Monadic.assert $ res == Right c
]
testProperties :: TestTree
testProperties = testGroup "Properties" [ testEnvProperties ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment