Skip to content

Commit

Permalink
GHC 8 compat (close #25)
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Sep 6, 2016
1 parent aecd8b7 commit c385076
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ env:
- CABALVER=1.18 GHCVER=7.6.3
- CABALVER=1.18 GHCVER=7.8.4
- CABALVER=1.22 GHCVER=7.10.2
- CABALVER=1.24 GHCVER=8.0.1

addons:
apt:
Expand All @@ -13,8 +14,10 @@ addons:
- ghc-7.6.3
- ghc-7.8.4
- ghc-7.10.2
- ghc-8.0.1
- cabal-install-1.18
- cabal-install-1.22
- cabal-install-1.24

before_install:
- export PATH=/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH
Expand Down
22 changes: 21 additions & 1 deletion hspec-expectations.cabal
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- This file has been generated from package.yaml by hpack version 0.5.4.
-- This file has been generated from package.yaml by hpack version 0.14.1.
--
-- see: https://github.com/sol/hpack

Expand Down Expand Up @@ -33,4 +33,24 @@ library
Test.Hspec.Expectations.Contrib
other-modules:
Test.Hspec.Expectations.Matcher
Paths_hspec_expectations
default-language: Haskell2010

test-suite spec
type: exitcode-stdio-1.0
main-is: Spec.hs
hs-source-dirs:
test
, src
ghc-options: -Wall
build-depends:
base == 4.*
, HUnit
, nanospec
other-modules:
Test.Hspec.Expectations.MatcherSpec
Test.Hspec.ExpectationsSpec
Test.Hspec.Expectations
Test.Hspec.Expectations.Contrib
Test.Hspec.Expectations.Matcher
default-language: Haskell2010
9 changes: 9 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ library:
exposed-modules:
- Test.Hspec.Expectations
- Test.Hspec.Expectations.Contrib

tests:
spec:
main: Spec.hs
source-dirs:
- test
- src
dependencies:
- nanospec
6 changes: 5 additions & 1 deletion src/Test/Hspec/Expectations.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ actual `shouldBe` expected = expectTrue ("expected: " ++ show expected ++ "\n bu
with_loc(shouldSatisfy, (Show a) => a -> (a -> Bool) -> Expectation)
v `shouldSatisfy` p = expectTrue ("predicate failed on: " ++ show v) (p v)

with_loc(compareWith, (Show a, Eq a) => (a -> a -> Bool) -> String -> a -> a -> Expectation)
with_loc(compareWith, Show a => (a -> a -> Bool) -> String -> a -> a -> Expectation)
compareWith comparator errorDesc result expected = expectTrue errorMsg (comparator expected result)
where
errorMsg = show result ++ " " ++ errorDesc ++ " " ++ show expected
Expand Down Expand Up @@ -183,7 +183,11 @@ anyErrorCall :: Selector ErrorCall
anyErrorCall = const True

errorCall :: String -> Selector ErrorCall
#if MIN_VERSION_base(4,9,0)
errorCall s (ErrorCallWithLocation msg _) = s == msg
#else
errorCall s (ErrorCall msg) = s == msg
#endif

anyIOException :: Selector IOException
anyIOException = const True
Expand Down
12 changes: 11 additions & 1 deletion test/Spec.hs
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
module Main where

import Test.Hspec

import qualified Test.Hspec.ExpectationsSpec
import qualified Test.Hspec.Expectations.MatcherSpec

main :: IO ()
main = hspec $ do
describe "Test.Hspec.ExpectationsSpec" Test.Hspec.ExpectationsSpec.spec
describe "Test.Hspec.Expectations.MatcherSpec" Test.Hspec.Expectations.MatcherSpec.spec
10 changes: 5 additions & 5 deletions test/Test/Hspec/ExpectationsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import Test.Hspec.Expectations

#ifdef HAS_SOURCE_LOCATIONS

#if !(MIN_VERSION_base(4,9,0))
import GHC.SrcLoc
#endif

import GHC.Stack

expectationFailed :: (?loc :: CallStack) => String -> HUnitFailure -> Bool
Expand Down Expand Up @@ -119,11 +122,8 @@ spec = do
it "fails, if a required specific exception is not thrown" $ do
(throwIO Overflow `shouldThrow` (== DivideByZero)) `shouldThrow` expectationFailed "predicate failed on expected exception: ArithException (arithmetic overflow)"

it "fails, if any exception is required, but no exception occurs" $ do
it "fails, if any exception is required, but no exception is thrown" $ do
(return () `shouldThrow` anyException) `shouldThrow` expectationFailed "did not get expected exception: SomeException"

it "fails, if a required exception of a specific type is not thrown" $ do
it "fails, if an exception of a specific type is required, but no exception is thrown" $ do
(return () `shouldThrow` anyErrorCall) `shouldThrow` expectationFailed "did not get expected exception: ErrorCall"

it "fails, if a required specific exception is not thrown" $ do
(error "foo" `shouldThrow` errorCall "foobar") `shouldThrow` expectationFailed "predicate failed on expected exception: ErrorCall (foo)"

0 comments on commit c385076

Please sign in to comment.