should-not-typecheck 2.0.1 → 2.1.0
raw patch · 4 files changed
+22/−8 lines, 4 files
Files
- CHANGELOG.md +3/−0
- README.md +3/−3
- should-not-typecheck.cabal +2/−2
- src/Test/ShouldNotTypecheck.hs +14/−3
CHANGELOG.md view
@@ -1,5 +1,8 @@ # `should-not-typecheck` changelog +## 2.1.0+* Support GHC 8.0.1 (see https://github.com/CRogers/should-not-typecheck/pull/6).+ ## 2.0.1 * Support HUnit 1.3
README.md view
@@ -4,7 +4,7 @@ ## Example (hspec) -The secret sauce is the [Deferred Type Errors GHC extension](https://downloads.haskell.org/~ghc/7.10.1/docs/html/users_guide/defer-type-errors.html). This allows you to write a ill-typed expression which will throw an exception at run time (rather than erroring out at compile time). `shouldNotTypecheck` tries to catch that exception and fails the test if no deferred type error is caught.+The secret sauce is the [Deferred Type Errors GHC extension](https://downloads.haskell.org/~ghc/7.10.1/docs/html/users_guide/defer-type-errors.html). This allows you to write an ill-typed expression which will throw an exception at run time (rather than erroring out at compile time). `shouldNotTypecheck` tries to catch that exception and fails the test if no deferred type error is caught. ```haskell {-# OPTIONS_GHC -fdefer-type-errors #-} -- Very important!@@ -67,7 +67,7 @@ -- do not typecheck! ``` -If you forget to specify provide an `NFData` instance for a type `should-not-typecheck` should warn you.+If you forget to specify an `NFData` instance for a type `should-not-typecheck` should warn you. ## Motivation @@ -100,7 +100,7 @@ -- ... ``` -Will create a warning at compile time but not an error. All of the ill-typed expressions we are testing will also produce warnings and it will hard to immediately see which ones matter. The upside is that the test-suite will still fail if there are errors.+Will create a warning at compile time but not an error. All of the ill-typed expressions we are testing will also produce warnings and it will be hard to immediately see which ones matter. The upside is that the test-suite will still fail if there are errors. ### Workaround
should-not-typecheck.cabal view
@@ -1,5 +1,5 @@ name: should-not-typecheck-version: 2.0.1+version: 2.1.0 synopsis: A HUnit/hspec assertion library to verify that an expression does not typecheck description: For examples and an introduction to the library please take a look at the <https://github.com/CRogers/should-not-typecheck#should-not-typecheck- README> on github.@@ -16,7 +16,7 @@ , CHANGELOG.md , stack.yaml cabal-version: >=1.10-tested-with: GHC == 7.6.3, GHC == 7.8.4, GHC == 7.10.1+tested-with: GHC == 7.6.3, GHC == 7.8.4, GHC == 7.10.1, GHC == 8.0.1 library hs-source-dirs: src
src/Test/ShouldNotTypecheck.hs view
@@ -1,7 +1,13 @@+{-# LANGUAGE CPP, RankNTypes, GADTs #-}+#if __GLASGOW_HASKELL__ >= 800+#define TheExc TypeError+#else+#define TheExc ErrorCall+#endif module Test.ShouldNotTypecheck (shouldNotTypecheck) where import Control.DeepSeq (force, NFData)-import Control.Exception (evaluate, try, throwIO, ErrorCall(..))+import Control.Exception (evaluate, try, throwIO, TheExc(..)) import Data.List (isSuffixOf) import Test.HUnit.Lang (Assertion, assertFailure) @@ -19,13 +25,18 @@ See the <https://github.com/CRogers/should-not-typecheck#should-not-typecheck- README> for examples and more infomation. -}+#if __GLASGOW_HASKELL__ >= 800+shouldNotTypecheck :: NFData a => (() ~ () => a) -> Assertion+#else shouldNotTypecheck :: NFData a => a -> Assertion+#endif+-- The type for GHC-8.0.1 is a hack, see https://github.com/CRogers/should-not-typecheck/pull/6#issuecomment-211520177 shouldNotTypecheck a = do result <- try (evaluate $ force a) case result of Right _ -> assertFailure "Expected expression to not compile but it did compile"- Left (ErrorCall msg) -> case isSuffixOf "(deferred type error)" msg of+ Left e@(TheExc msg) -> case isSuffixOf "(deferred type error)" msg of True -> case isSubsequenceOf "No instance for" msg && isSubsequenceOf "NFData" msg of True -> assertFailure $ "Make sure the expression has an NFData instance! See docs at https://github.com/CRogers/should-not-typecheck#nfdata-a-constraint. Full error:\n" ++ msg False -> return ()- False -> throwIO (ErrorCall msg)+ False -> throwIO e