packages feed

th-extras-0.1.0.0: test/Main.hs

{-# LANGUAGE OverloadedStrings, ScopedTypeVariables, TemplateHaskell #-}

module Main (main) where

import qualified Data.Map as Map
import qualified Data.Set as Set
import Control.Exception (ErrorCall, evaluate, try)
import Control.Monad (forM_)
import Fixtures (arityCases)
import Gen
import Hedgehog
import Hedgehog.Main (defaultMain)
import Language.Haskell.TH (mkName, nameBase)
import Language.Haskell.TH.Datatype.TyVarBndr (tvName)
import Language.Haskell.TH.Extras

main :: IO ()
main =
  defaultMain
    [ checkParallel $ Group "Language.Haskell.TH.Extras"
        [ ("prop_occursInType", prop_occursInType)
        , ("prop_namesBoundInPat", prop_namesBoundInPat)
        , ("prop_substVarsWith", prop_substVarsWith)
        , ("prop_headOfType", prop_headOfType)
        , ("prop_kindArity", prop_kindArity)
        , ("prop_substVarsWith_refined", prop_substVarsWith_refined)
        , ("prop_namesBoundInDec", prop_namesBoundInDec)
        , ("prop_genericalizeDecs", prop_genericalizeDecs)
        , ("prop_headOfType_builtin", prop_headOfType_builtin)
        , ("prop_substVarsWith_existential", prop_substVarsWith_existential)
        , ("prop_tyConArity", prop_tyConArity)
        , ("prop_decCons", prop_decCons)
        , ("prop_conAccessors", prop_conAccessors)
        , ("prop_substVarsWith_wrapped", prop_substVarsWith_wrapped)
        , ("prop_kindArity_anyArrow", prop_kindArity_anyArrow)
        , ("prop_headOfType_unresolved", prop_headOfType_unresolved)
        , ("prop_headOfType_any", prop_headOfType_any)
        ]
    ]

-- | 'occursInType' agrees with the reference free-variable function for
-- every kind of type.
prop_occursInType :: Property
prop_occursInType = withTests 1000 . property $ do
  ty <- forAll genType
  v <- forAll genVarName
  occursInType v ty === Set.member v (freeVars ty)

-- | 'namesBoundInPat' finds every variable bound by a pattern, in order.
prop_namesBoundInPat :: Property
prop_namesBoundInPat = withTests 1000 . property $ do
  pat <- forAll genPat
  namesBoundInPat pat === patVars pat

-- | 'substVarsWith' renames exactly the free variables of the argument type:
-- no free variable is missed and no substituted variable is captured by a
-- binder in the argument type.
prop_substVarsWith :: Property
prop_substVarsWith = withTests 1000 . property $ checkSubstCase =<< forAll genSubstCase

-- | As 'prop_substVarsWith', but for GADT constructors whose result type refines
-- some of the type constructor's parameters to concrete types.
prop_substVarsWith_refined :: Property
prop_substVarsWith_refined = withTests 1000 . property $ checkSubstCase =<< forAll genRefinedSubstCase

-- | As 'prop_substVarsWith', but the argument type also mentions existential
-- variables and the index variable, which are not parameters of the result
-- type and so must be left unchanged.
prop_substVarsWith_existential :: Property
prop_substVarsWith_existential = withTests 1000 . property $ checkSubstCase =<< forAll genExistentialSubstCase

-- | As 'prop_substVarsWith', but the result type is written with parentheses,
-- kind signatures and kind applications, which must not change the result.
prop_substVarsWith_wrapped :: Property
prop_substVarsWith_wrapped = withTests 1000 . property $ checkSubstCase =<< forAll genWrappedSubstCase

checkSubstCase :: SubstCase -> PropertyT IO ()
checkSubstCase sc = do
  let result = substVarsWith (scTopVars sc) (scResultType sc) (scArgType sc)
      rename v = Map.findWithDefault v v (scExpected sc)
  -- Force the whole result here, so that an 'error' call is reported as a
  -- failure of this property rather than escaping from 'annotateShow'.
  _ <- eval (length (show result))
  annotateShow result
  freeVars result === Set.map rename (freeVars (scArgType sc))

-- | 'headOfType' sees through applications, parentheses, kind signatures,
-- foralls and kind applications to the name at the head of a type.
prop_headOfType :: Property
prop_headOfType = withTests 1000 . property $ do
  (ty, name) <- forAll genHeadedType
  headOfType ty === name

-- | 'kindArity' counts every argument a kind takes, including visible
-- dependent quantification, looking through parentheses, kind signatures and
-- invisible foralls.
prop_kindArity :: Property
prop_kindArity = withTests 1000 . property $ do
  (k, n) <- forAll genKindWithArity
  kindArity k === n

-- | 'namesBoundInDec' finds every name a declaration brings into scope,
-- including data constructors, record fields, class methods, associated
-- families and pattern synonym fields.
prop_namesBoundInDec :: Property
prop_namesBoundInDec = withTests 1000 . property $ do
  dec <- forAll genDec
  namesBoundInDec dec === decNames dec

-- | 'genericalizeDecs' replaces every occurrence of every name bound at the top
-- level of the declarations with a generic name, and leaves all other names
-- (including fresh ones bound locally) alone.
prop_genericalizeDecs :: Property
prop_genericalizeDecs = withTests 1000 . property $ do
  decs <- forAll genQuotedDecs
  let bound = Set.fromList (concatMap decNames decs)
      expected n = if Set.member n bound then mkName (nameBase n) else n
  allNames (genericalizeDecs decs) === map expected (allNames decs)

-- | 'headOfType' finds the type constructor at the head of a type whose head is
-- a built-in type constructor with dedicated Template Haskell syntax, such as
-- unboxed sums, promoted lists and tuples, 'Type', 'Constraint', equality and
-- the multiplicity-polymorphic arrow.
prop_headOfType_builtin :: Property
prop_headOfType_builtin = withTests 1000 . property $ do
  (ty, name) <- forAll genBuiltinHeadedType
  headOfType ty === name

-- | 'tyConArity' gives the number of type arguments a data type or newtype
-- takes, however its parameters are declared: binders, kind signatures,
-- standalone kind signatures or @type data@. The arities are computed at
-- compile time by reifying the declarations in "Fixtures".
prop_tyConArity :: Property
prop_tyConArity = withTests 1 . property $
  forM_ ($(arityCases) :: [(String, Either String Int, Int)]) $ \(name, result, expected) ->
    (name, result) === (name, Right expected)

-- | 'decCons' returns the data constructors of every kind of declaration that
-- declares them: data, newtype and @type data@ declarations, and data and
-- newtype instances.
prop_decCons :: Property
prop_decCons = withTests 1000 . property $ do
  (dec, cons) <- forAll genDataLikeDec
  result <- eval (decCons dec)
  result === cons

-- | 'nameOfCon', 'argTypesOfCon', 'conArity' and 'varsBoundInCon' agree with a
-- reference for constructors of every form, through any number of foralls.
-- 'nameOfCon' is only checked for constructors declaring a single name, since
-- it is documented to fail otherwise.
prop_conAccessors :: Property
prop_conAccessors = withTests 1000 . property $ do
  con <- forAll genCon
  let ci = conInfo con
  argTypesOfCon con === ciArgTypes ci
  conArity con === length (ciArgTypes ci)
  map tvName (varsBoundInCon con) === ciBinders ci
  case ciNames ci of
    [name] -> nameOfCon con === name
    _ -> success

-- | As 'prop_kindArity', but with arrows written in any of the ways Template
-- Haskell can represent them, which must not change the arity.
prop_kindArity_anyArrow :: Property
prop_kindArity_anyArrow = withTests 1000 . property $ do
  (k, n) <- forAll genKindWithArityAnyArrow
  kindArity k === n

-- | 'headOfType' finds the operator at the head of a chain of unresolved infix
-- applications when every operator in the chain is the same, since the head
-- is then that operator whatever its fixity.
prop_headOfType_unresolved :: Property
prop_headOfType_unresolved = withTests 1000 . property $ do
  (ty, name) <- forAll genUnresolvedHeadedType
  result <- eval (headOfType ty)
  result === name

-- | On arbitrary types, 'headOfType' returns the head wherever the type has one,
-- and calls 'error' (rather than returning something wrong) wherever it does
-- not.
prop_headOfType_any :: Property
prop_headOfType_any = withTests 2000 . property $ do
  ty <- forAll genType
  result <- evalIO (try (evaluate (headOfType ty)))
  classify "has a head" (typeHead ty /= Nothing)
  classify "has no head" (typeHead ty == Nothing)
  case result of
    Left (_ :: ErrorCall) -> typeHead ty === Nothing
    Right name -> Just name === typeHead ty