assert4hs-hspec (empty) → 0.1.0
raw patch · 7 files changed
+189/−0 lines, 7 filesdep +HUnitdep +assert4hs-coredep +assert4hs-hspecsetup-changed
Dependencies added: HUnit, assert4hs-core, assert4hs-hspec, base, hspec
Files
- ChangeLog.md +3/−0
- LICENSE.md +21/−0
- README.md +3/−0
- Setup.hs +2/−0
- assert4hs-hspec.cabal +56/−0
- src/Test/Fluent/Hspec.hs +76/−0
- test/Spec.hs +28/−0
+ ChangeLog.md view
@@ -0,0 +1,3 @@+# Changelog for assert4hs-hspec++## Unreleased changes
+ LICENSE.md view
@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2021 Pawel Nosal++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ README.md view
@@ -0,0 +1,3 @@+# assert4hs-hspec++This library allows using assert4hs-core assertions with [hspec](https://hackage.haskell.org/package/hspec)
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ assert4hs-hspec.cabal view
@@ -0,0 +1,56 @@+cabal-version: 1.22+name: assert4hs-hspec+version: 0.1.0+license: MIT+license-file: LICENSE.md+copyright: 2021 Pawel Nosal+maintainer: p.nosal1986@gmail.com+author: Pawel Nosal+homepage: https://github.com/paweln1986/assert4hs-hspec#readme+bug-reports: https://github.com/paweln1986/assert4hs-hspec/issues+synopsis: integration point of assert4hs and hspec+description:+ Please see the README on GitHub at <https://github.com/githubuser/assert4hs-hspec#readme>++category: Testing+build-type: Simple+extra-source-files:+ README.md+ ChangeLog.md++source-repository head+ type: git+ location: https://github.com/paweln1986/assert4hs-hspec++library+ exposed-modules: Test.Fluent.Hspec+ reexported-modules:+ Test.Fluent.Assertions,+ Test.Fluent.Assertions.Maybe,+ Test.Fluent.Assertions.List,+ Test.Fluent.Assertions.Exceptions,+ Test.Fluent.Assertions.Either,+ Test.Fluent.Diff++ hs-source-dirs: src+ other-modules: Paths_assert4hs_hspec+ default-language: Haskell2010+ build-depends:+ HUnit >=1.6.1.0,+ assert4hs-core >=0.1.0,+ base >=4.7 && <5,+ hspec >=2.7.8++test-suite assert4hs-hspec-test+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ hs-source-dirs: test+ other-modules: Paths_assert4hs_hspec+ default-language: Haskell2010+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ build-depends:+ HUnit >=1.6.1.0,+ assert4hs-core >=0.1.0,+ assert4hs-hspec -any,+ base >=4.7 && <5,+ hspec >=2.7.8
+ src/Test/Fluent/Hspec.hs view
@@ -0,0 +1,76 @@+{-# LANGUAGE ScopedTypeVariables #-}++-- |+-- Module : Test.Fluent.Assertions.Core+-- Description : Set util function for HSpec to execute assertions against given value.+-- Copyright : (c) Pawel Nosal, 2021+-- License : MIT+-- Maintainer : p.nosal1986@gmail.com+-- Stability : experimental+module Test.Fluent.Hspec+ ( -- ** Assertion util functions for HSpec+ assertThat,+ assertThat',+ assertThatIO,+ assertThatIO',+ assertThrown,+ assertThrown',+ assertThrows,+ assertThrows',+ )+where++import Control.Exception (Exception, throwIO, try)+import Data.List (intercalate)+import GHC.Exception (prettySrcLoc)+import GHC.Stack (HasCallStack)+import Test.Fluent.Assertions (Assertion', AssertionConfig, FluentTestFailure)+import qualified Test.Fluent.Assertions.Core as FC+import Test.Fluent.Assertions.Exceptions (ExceptionSelector)+import Test.Fluent.Internal.Assertions (FluentTestFailure (FluentTestFailure))+import Test.HUnit.Lang+ ( FailureReason (Reason),+ HUnitFailure (HUnitFailure),+ )++-- | Verify if given `IO` action throws expected exception.+assertThrows :: (HasCallStack, Exception e) => IO a -> ExceptionSelector e -> IO ()+assertThrows given selector = toHspecError $ FC.assertThrows given selector++assertThrows' :: (HasCallStack, Exception e) => AssertionConfig -> IO a -> ExceptionSelector e -> IO ()+assertThrows' config given selector = toHspecError $ FC.assertThrows' config given selector++assertThrown' :: (HasCallStack, Exception e) => AssertionConfig -> IO a -> ExceptionSelector e -> Assertion' e b -> IO ()+assertThrown' config given selector assertions = toHspecError $ FC.assertThrown' config given selector assertions++-- | Execute assertions against selected exception+assertThrown :: (HasCallStack, Exception e) => IO a -> ExceptionSelector e -> Assertion' e b -> IO ()+assertThrown given selector assertions = toHspecError $ FC.assertThrown given selector assertions++-- | Execute assertions against given subject under test extracted from IO action.+assertThatIO :: HasCallStack => IO a -> Assertion' a b -> IO ()+assertThatIO given assertions = toHspecError $ FC.assertThatIO given assertions++-- | A variant of `assertThatIO` which allow to pass additional configuration.+assertThatIO' :: HasCallStack => AssertionConfig -> IO a -> Assertion' a b -> IO ()+assertThatIO' config given assertions = toHspecError $ FC.assertThatIO' config given assertions++-- | A variant of `assertThat` which allow to pass additional configuration.+assertThat' :: HasCallStack => AssertionConfig -> a -> Assertion' a b -> IO ()+assertThat' config given assertions = toHspecError $ FC.assertThat' config given assertions++-- | Execute assertions against given subject under test.+assertThat :: HasCallStack => a -> Assertion' a b -> IO ()+assertThat given assertions = toHspecError $ FC.assertThat given assertions++toHspecError :: HasCallStack => IO () -> IO ()+toHspecError a = do+ res :: Either FluentTestFailure () <- try a+ case res of+ Left (FluentTestFailure srcLoc msg _ _) -> do+ let assertionMessages = intercalate "\n" (fmap formatMsg msg)+ throwIO $ HUnitFailure srcLoc (Reason assertionMessages)+ Right () -> pure ()+ where+ formatMsg (message, Just srcLoc) = prettySrcLoc srcLoc <> "\n" <> message+ formatMsg (message, Nothing) = message
+ test/Spec.hs view
@@ -0,0 +1,28 @@+{-# LANGUAGE ScopedTypeVariables #-}++import Control.Exception (throwIO, try)+import Data.List (intercalate)+import GHC.Exception (SrcLoc, prettySrcLoc)+import Test.Fluent.Assertions+ ( Assertion',+ FluentTestFailure (FluentTestFailure),+ focus,+ isEqualTo,+ )+ +import Test.HUnit.Lang+ ( FailureReason (Reason),+ HUnitFailure (HUnitFailure),+ )+import Test.Hspec (HasCallStack, describe, hspec, it)++main :: IO ()+main = hspec $+ describe "Prelude.head" $ do+ it "returns the first element of a list" $ do+ assertThatHspec [23 ..] $ + focus head + . isEqualTo 21+ . isEqualTo 21+ . isEqualTo 21+ . isEqualTo 21