Skip to content

Commit

Permalink
Add tests for shouldBeNear
Browse files Browse the repository at this point in the history
  • Loading branch information
erikd committed Sep 7, 2016
1 parent 6b0c303 commit 3386a41
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/Test/Hspec/ExpectationsSpec.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE CPP #-}

#if MIN_VERSION_base(4,8,1)
#define HAS_SOURCE_LOCATIONS
{-# LANGUAGE ImplicitParams #-}
Expand Down Expand Up @@ -43,6 +44,25 @@ spec = do
it "fails if arguments are not equal" $ do
("foo" `shouldBe` "bar") `shouldThrow` expectationFailed "expected: \"bar\"\n but got: \"foo\""

describe "shouldBeNear" $ do
it "succeeds if arguments are equal" $ do
1.23456789 `shouldBeNear` (1.23456789 :: Float)

it "fails if arguments are not equal" $ do
(1.0 `shouldBe` (2.0 :: Float)) `shouldThrow` expectationFailed "expected: 2.0\n but got: 1.0"

it "succeeds if one argument is zero and the other less than epsilon" $ do
0.0 `shouldBeNear` (-1e-16 :: Float)

it "succeeds for large values near one another" $ do
1e20 `shouldBeNear` (1e20 + 1 :: Float)

#if __GLASGOW_HASKELL__ != 710
-- For some reason this fails with ghc-7.10.2 and 7.10.3.
it "fails for large values not near each other" $ do
(1.1e20 `shouldBeNear` (2.1e20 :: Double)) `shouldThrow` expectationFailed "expected: 2.1e20\n but got: 1.1e20"
#endif

describe "shouldSatisfy" $ do
it "succeeds if value satisfies predicate" $ do
"" `shouldSatisfy` null
Expand Down

0 comments on commit 3386a41

Please sign in to comment.