Last active
May 12, 2020 08:05
-
-
Save symbiont-eric-torreborre/d470221096404c7cc456254073f33f3b to your computer and use it in GitHub Desktop.
tasty-tags.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
testCase "Example test case" $ do | |
assertBool "arithmetic is still sane" $ 2 + 2 == 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | Option describing if tests using the database are enabled or not | |
newtype Database = Database Bool deriving (Eq, Show) | |
instance IsOption Database where | |
defaultValue = Database False | |
parseValue = fmap Database . safeReadBool | |
optionName = pure "database" | |
optionHelp = pure "set the --database option to enable database tests" | |
optionCLParser = flagCLParser Nothing (Database True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
withDatabase :: TestTree -> TestTree | |
withDatabase t = askOption $ \Database doIt -> | |
if doIt then t else testGroup "empty" [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
withDatabase $ testCase "connect to the DB" $ do | |
connected <- connect "127.0.0.1" 5432 | |
assertBool True connected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sh> stack test --test-arguments '--database' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data CustomOptions | |
= CustomOptions | |
{ _database :: Database, | |
_tracing :: Tracing, | |
_slow :: Slow, | |
_combineWith :: CombineWith | |
} | |
deriving (Eq, Show) | |
data CombineWith = All | Any deriving (Eq, Show) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
instance IsOption CustomOptions where | |
-- we get the default values for each option | |
defaultValue = CustomOptions | |
{ _database = defaultValue, | |
_tracing = defaultValue, | |
_slow = defaultValue, | |
_combineWith = All | |
} | |
parseValue = | |
fmap (\b -> defaultValue {_combineWith = if b then Any else All}) . | |
safeReadBool | |
optionName = pure "any" | |
optionHelp = pure "set the --any option to run tests having any of the other flags set" | |
optionCLParser = | |
flag' defaultValue {_combineWith = Any} | |
( long (untag (optionName :: Tagged CustomOptions String)) | |
<> help (untag (optionHelp :: Tagged CustomOptions String)) | |
<> (foldMap short Nothing) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
withDatabase :: TestTree -> TestTree | |
withDatabase t = askOption $ \custom -> | |
localOption (custom {_database = Database True}) t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
withCustomOptions :: TestTree -> TestTree | |
withCustomOptions t = | |
askOption $ \(database :: Database) -> | |
askOption $ \(tracing :: Tracing) -> | |
askOption $ \(slow :: Slow) -> | |
askOption $ \CustomOptions {..} -> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- a list of conditions to be true when --any is not specified | |
allMatches = | |
[ case (_database, database) of (Database a, Database b) -> a == b, | |
case (_tracing, tracing) of (Tracing a, Tracing b) -> a == b, | |
case (_slow, slow) of (Slow a, Slow b) -> a == b | |
] | |
-- a list of conditions to be true when --any is specified | |
anyMatches = | |
[ database == Database True && _database == Database True, | |
tracing == Tracing True && _tracing == Tracing True, | |
slow == Slow True && _slow == Slow True | |
] | |
-- for OR any condition in the orMatches list must be true | |
-- for AND all the conditions in the andMatches list must be true | |
enabled = | |
case _combineWith of | |
All -> all (== True) allMatches | |
Any -> any (== True) anyMatches | |
if enabled then t else testGroup "empty" [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let defaultOptions = | |
[ database == Database False, | |
tracing == Tracing False, | |
slow == Slow False | |
] | |
defaultMatches = | |
[ _database == Database False, | |
_tracing == Tracing False, | |
_slow == Slow False | |
] | |
-- note that we also need a new option, `NoTag` set to True by default on all tests | |
-- and set to false as soon as another option is set | |
-- | |
-- then anyMatches becomes | |
anyMatches = | |
[ database == Database True && _database == Database True, | |
sailfish == Sailfish True && _sailfish == Sailfish True, | |
slow == Slow True && _slow == Slow True, | |
noTag == NoTag True && all (== True) defaultMatches | |
] | |
-- some code ... | |
enabled = | |
if all (== True) defaultOptions | |
then all (== True) defaultMatches | |
else | |
case _combineWith of | |
All -> all (== True) allMatches | |
Any -> any (== True) anyMatches |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
testSpec "testing lists" $` | |
describe "Prelude.head" $ do | |
it "returns the first element of a list" $ do | |
head [23 ..] `shouldBe` (23 :: Int) | |
it "throws an exception if the list is empty" $ | |
head [] `shouldThrow` anyException |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
testProperty "reverse a list" $ do | |
xs <- forAll $ Gen.list (Range.linear 0 100) Gen.alpha | |
reverse (reverse xs) === xs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tests :: TestTree | |
tests = testGroup "simple assertions" | |
[ testCase "2+2=4" $ | |
2+2 @?= 4 | |
, testCase "7 is even" $ | |
assertBool "Oops, 7 is odd" (even 7) | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sh> stack test --test-arguments '--pattern "simple assertions"' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
testGroup "One" [ testGroup "Two" [ testCase "Three" _ ] ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sh> stack test --test-arguments '--pattern "$0 ~ /Two/"' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sh> stack test --test-arguments '--pattern "$1 !~ /SKIP/"' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sh> stack test --test-arguments '--hedgehog-tests 10000' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment