-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5920 from IntersectMBO/ensure-query-test-complete…
…ness Ensure query test completeness
- Loading branch information
Showing
7 changed files
with
496 additions
and
100 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Testnet.TestEnumGenerator | ||
( genTestType | ||
, genAllConstructorsList | ||
) where | ||
|
||
import Language.Haskell.TH (Body (NormalB), Con (..), Dec (DataD, ValD), | ||
Exp (ConE, ListE), Info (TyConI), Name, Pat (VarP), Q, mkName, nameBase, reify) | ||
|
||
-- | Create a datatype with the same constructors as the given type, but with a "Test" prefix and no arguments. | ||
-- For example, if the input type is 'Maybe', the output type will be 'TestMaybe', with constructors 'TestNothing' | ||
-- and 'TestJust', but 'TestJust' will have no arguments. | ||
genTestType :: Name -> Q [Dec] | ||
genTestType typeName = do | ||
TyConI (DataD _ _ _ _ constructors _) <- reify typeName | ||
let newConstructors = map (makeSimpleConstructor . addTestPrefix) $ concatMap getConstructorName constructors | ||
return [DataD [] (addTestPrefix typeName) [] Nothing newConstructors []] | ||
where | ||
addTestPrefix :: Name -> Name | ||
addTestPrefix name = mkName $ "Test" ++ nameBase name | ||
|
||
makeSimpleConstructor :: Name -> Con | ||
makeSimpleConstructor n = NormalC n [] | ||
|
||
-- | Generate a declaration with a list of all constructors of a type. For example, if the input type is 'Maybe', | ||
-- the output will be 'allMaybeConstructors = [Nothing, Just]'. Obviously, this will only work if all constructors | ||
-- have types that can be unified, like is the case with nullary constructors like the ones generated by 'genNewType'. | ||
genAllConstructorsList :: Name -> Q [Dec] | ||
genAllConstructorsList typeName = do | ||
constructorList <- getAllConstructors typeName | ||
return [ValD (VarP $ mkName $ "all" ++ nameBase typeName ++ "Constructors") (NormalB constructorList) []] | ||
where | ||
getAllConstructors :: Name -> Q Exp | ||
getAllConstructors typeName' = do | ||
TyConI (DataD _ _ _ _ constructors _) <- reify typeName' | ||
return $ ListE $ map ConE $ concatMap getConstructorName constructors | ||
|
||
-- | Obtain the name or names from a constructor | ||
getConstructorName :: Con -> [Name] | ||
getConstructorName (NormalC na _) = [na] | ||
getConstructorName (RecC na _) = [na] | ||
getConstructorName (InfixC _ na _) = [na] | ||
getConstructorName (ForallC _ _ con) = getConstructorName con | ||
getConstructorName (GadtC nas _ _) = nas | ||
getConstructorName (RecGadtC nas _ _) = nas |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-# OPTIONS_GHC -Wno-missing-signatures #-} | ||
{-# OPTIONS_GHC -Wno-unused-top-binds #-} | ||
|
||
module Testnet.TestQueryCmds | ||
( TestQueryCmds(..) | ||
, forallQueryCommands | ||
) where | ||
|
||
import Cardano.CLI.EraBased.Commands.Query (QueryCmds (..)) | ||
|
||
import Testnet.TestEnumGenerator (genAllConstructorsList, genTestType) | ||
|
||
-- | A datatype with the same constructors as 'QueryCmds', but with a "Test" prefix and no arguments. | ||
-- The generated type is called 'TestQueryCmds'. | ||
$(genTestType ''QueryCmds) | ||
|
||
-- | A list of all constructors of 'TestQueryCmds', which are nullary. | ||
-- The generated list is called 'allTestQueryCmdsConstructors'. | ||
$(genAllConstructorsList ''TestQueryCmds) | ||
|
||
-- | Maps a function over all constructors of 'TestQueryCmds' and sequences the results over a monad. | ||
forallQueryCommands :: Monad m => (TestQueryCmds -> m a) -> m () | ||
forallQueryCommands f = mapM_ f allTestQueryCmdsConstructors |
Oops, something went wrong.