th-test-utils 1.2.1 → 1.2.2
raw patch · 9 files changed
+72/−48 lines, 9 filesdep +textdep −bytestringdep ~template-haskell
Dependencies added: text
Dependencies removed: bytestring
Dependency ranges changed: template-haskell
Files
- CHANGELOG.md +4/−0
- LICENSE +0/−11
- LICENSE.md +11/−0
- README.md +2/−2
- src/Language/Haskell/TH/TestUtils.hs +4/−4
- src/Language/Haskell/TH/TestUtils/QState.hs +8/−9
- test/Main.hs +25/−7
- test/TH.hs +5/−2
- th-test-utils.cabal +13/−13
CHANGELOG.md view
@@ -1,3 +1,7 @@+# v1.2.2++* Support GHC 9.6 - 9.12+ # v1.2.1 * Support GHC 9.4
− LICENSE
@@ -1,11 +0,0 @@-Copyright 2019 LeapYear--Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:--1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.--2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.--3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.--THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ LICENSE.md view
@@ -0,0 +1,11 @@+Copyright © 2023-present Brandon Chinn++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.++3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
README.md view
@@ -1,7 +1,7 @@ # th-test-utils -[](https://github.com/LeapYear/th-test-utils/actions?query=branch%3Amain)-[](https://codecov.io/gh/LeapYear/th-test-utils)+[](https://github.com/brandonchinn178/th-test-utils/actions?query=branch%3Amain)+[](https://codecov.io/gh/brandonchinn178/th-test-utils) [](https://hackage.haskell.org/package/th-test-utils) This package implements `tryTestQ` and related helpers in order to better test Template Haskell code. It supports returning the actual error message that [`recover` doesn't currently return](https://gitlab.haskell.org/ghc/ghc/-/issues/2340) as well as mocking out `Q` actions, so that you can run Template Haskell code at runtime.
src/Language/Haskell/TH/TestUtils.hs view
@@ -8,9 +8,9 @@ {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} -{- |+{-| Module : Language.Haskell.TH.TestUtils-Maintainer : Brandon Chinn <brandon@leapyear.io>+Maintainer : Brandon Chinn <brandonchinn178@gmail.com> Stability : experimental Portability : portable @@ -43,7 +43,7 @@ import Language.Haskell.TH.TestUtils.QMode import Language.Haskell.TH.TestUtils.QState -runTestQ :: forall mode a. IsMockedMode mode => QState mode -> Q a -> TestQResult mode a+runTestQ :: forall mode a. (IsMockedMode mode) => QState mode -> Q a -> TestQResult mode a runTestQ state = fmapResult' (either error id) . tryTestQ state where fmapResult' = fmapResult @mode @(Either String a) @a@@ -54,7 +54,7 @@ fmapResult' = fmapResult @mode @(Either String a) @String mkMsg a = "Unexpected success: " ++ show a -tryTestQ :: forall mode a. IsMockedMode mode => QState mode -> Q a -> TestQResult mode (Either String a)+tryTestQ :: forall mode a. (IsMockedMode mode) => QState mode -> Q a -> TestQResult mode (Either String a) tryTestQ state = runResult @mode . runTestQMonad . runQ where runTestQMonad =
src/Language/Haskell/TH/TestUtils/QState.hs view
@@ -35,15 +35,14 @@ } deriving (Show, Lift) -{- | A helper for loading names for 'reifyInfo'-- Usage:-- > QState- > { reifyInfo = $(loadNames [''Int, ''Maybe])- > , ...- > }--}+-- | A helper for loading names for 'reifyInfo'+--+-- Usage:+--+-- > QState+-- > { reifyInfo = $(loadNames [''Int, ''Maybe])+-- > , ...+-- > } loadNames :: [Name] -> ExpQ loadNames names = listE $ flip map names $ \name -> do info <- reify name
test/Main.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE RankNTypes #-}@@ -8,9 +9,11 @@ import Control.Monad (join) import Control.Monad.IO.Class (liftIO)-import Data.ByteString.Lazy (ByteString)-import qualified Data.ByteString.Lazy.Char8 as Char8 import Data.List (intercalate)+import Data.Text (Text)+import qualified Data.Text as Text+import qualified Data.Text.Lazy as TextL+import qualified Data.Text.Lazy.Encoding as TextL import Language.Haskell.TH (AnnLookup (..), Q, Type (..), runIO) import Language.Haskell.TH.Syntax (Extension (..), ForeignSrcLang (..), Quasi (..)) import Test.Tasty@@ -204,7 +207,7 @@ } -- | Tests for both MockQ and MockQAllowIO, since they should behave exactly the same except for qRunIO.-testMockQ' :: forall mode. IsMockedMode mode => MockQTests mode -> TestTree+testMockQ' :: forall mode. (IsMockedMode mode) => MockQTests mode -> TestTree testMockQ' MockQTests{..} = testGroup (show qMode)@@ -379,14 +382,29 @@ ] -- force both MockQ and MockQAllowIO to resolve the same goldens- golden name = goldenVsString name ("test/goldens/MockQ_" ++ name ++ ".golden")+ golden name =+ goldenVsString name ("test/goldens/MockQ_" ++ name ++ ".golden")+ . fmap (TextL.encodeUtf8 . TextL.fromStrict . normalizeGhc910) - labelled :: Show a => [(String, IO a)] -> IO ByteString+ labelled :: (Show a) => [(String, IO a)] -> IO Text labelled vals = do let mkLine (label, getVal) = do val <- getVal return $ label ++ ": " ++ show val- Char8.pack . intercalate "\n" . map (++ "\n") <$> mapM mkLine vals+ Text.pack . intercalate "\n" . map (++ "\n") <$> mapM mkLine vals - runUnsupported :: Show a => QState mode -> Q a -> IO ByteString+ runUnsupported :: (Show a) => QState mode -> Q a -> IO Text runUnsupported state q = labelled [("Unsupported", runTestQWithErrors state q)]++{- HLINT ignore "Redundant id" -}+normalizeGhc910 :: Text -> Text+normalizeGhc910 =+ id+#if __GLASGOW_HASKELL__ < 910+ . replace "GHC.Show" "GHC.Internal.Show"+ . replace "GHC.Base" "GHC.Internal.Base"+ . replace "GHC.Num" "GHC.Internal.Num"+ . replace "System.IO" "GHC.Internal.System.IO"+ where+ replace old new = Text.replace (Text.pack old) (Text.pack new)+#endif
test/TH.hs view
@@ -35,14 +35,17 @@ tryIO m = first showError <$> try m where -- strip out call stack- showError = head . lines . displayException @SomeException+ showError e =+ case lines $ displayException @SomeException e of+ s : _ -> s+ [] -> error $ "Unexpectedly got empty string: " ++ show e -- | Same as tryIO, except in the Q monad. tryQ :: Q a -> Q (Either String a) tryQ q = runIO . tryIO . forceM . pure =<< q -- | Force the evaluation of @a@ when the monadic action is evaluated.-forceM :: Monad m => m a -> m a+forceM :: (Monad m) => m a -> m a forceM m = do a <- m a `seq` return a
th-test-utils.cabal view
@@ -1,21 +1,21 @@ cabal-version: >= 1.10 --- This file has been generated from package.yaml by hpack version 0.35.0.+-- This file has been generated from package.yaml by hpack version 0.37.0. -- -- see: https://github.com/sol/hpack name: th-test-utils-version: 1.2.1+version: 1.2.2 synopsis: Utility functions for testing Template Haskell code description: Utility functions for testing Template Haskell code, including functions for testing failures in the Q monad. category: Testing-homepage: https://github.com/LeapYear/th-test-utils#readme-bug-reports: https://github.com/LeapYear/th-test-utils/issues-author: Brandon Chinn <brandon@leapyear.io>-maintainer: Brandon Chinn <brandon@leapyear.io>+homepage: https://github.com/brandonchinn178/th-test-utils#readme+bug-reports: https://github.com/brandonchinn178/th-test-utils/issues+author: Brandon Chinn <brandonchinn178@gmail.com>+maintainer: Brandon Chinn <brandonchinn178@gmail.com> license: BSD3-license-file: LICENSE+license-file: LICENSE.md build-type: Simple extra-source-files: README.md@@ -23,7 +23,7 @@ source-repository head type: git- location: https://github.com/LeapYear/th-test-utils+ location: https://github.com/brandonchinn178/th-test-utils library exposed-modules:@@ -37,9 +37,9 @@ ghc-options: -Wall build-depends: base >=4.9 && <5- , template-haskell >=2.16 && <2.20+ , template-haskell >=2.16 && <2.24 , th-orphans >=0.13.4 && <0.14- , transformers >=0.5.2 && <0.6+ , transformers >=0.5.2 && <0.7 default-language: Haskell2010 if impl(ghc >= 8.0) ghc-options: -Wcompat -Wincomplete-record-updates -Wincomplete-uni-patterns -Wnoncanonical-monad-instances@@ -58,14 +58,14 @@ ghc-options: -Wall build-depends: base >=4.9 && <5- , bytestring , tasty , tasty-golden , tasty-hunit- , template-haskell >=2.16 && <2.20+ , template-haskell >=2.16 && <2.24+ , text , th-orphans >=0.13.4 && <0.14 , th-test-utils- , transformers >=0.5.2 && <0.6+ , transformers >=0.5.2 && <0.7 default-language: Haskell2010 if impl(ghc >= 8.0) ghc-options: -Wcompat -Wincomplete-record-updates -Wincomplete-uni-patterns -Wnoncanonical-monad-instances