Skip to content

Commit

Permalink
Add test to make sure Bson.look uses MonadFail correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-audm committed Mar 18, 2020
1 parent e7f0957 commit 664210b
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions tests/Data/Bson/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances, OverloadedStrings, TypeSynonymInstances #-}

module Data.Bson.Tests
( tests
Expand All @@ -7,6 +7,9 @@ module Data.Bson.Tests
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>), (<*>))
#endif
#if MIN_VERSION_base(4,9,0)
import Control.Monad.Fail(MonadFail(..))
#endif
import Data.Int (Int32, Int64)
import Data.Time.Calendar (Day(ModifiedJulianDay))
import Data.Time.Clock.POSIX (POSIXTime)
Expand All @@ -16,7 +19,7 @@ import qualified Data.ByteString as S
import Data.Text (Text)
import Test.Framework (Test, testGroup)
import Test.Framework.Providers.QuickCheck2 (testProperty)
import Test.QuickCheck (Arbitrary(..), elements, oneof)
import Test.QuickCheck (Arbitrary(..), elements, oneof, Property, (===))
import qualified Data.Text as T

import Data.Bson (Val(cast', val), ObjectId(..), MinMaxKey(..), MongoStamp(..),
Expand Down Expand Up @@ -106,6 +109,17 @@ testVal a = case cast' . val $ a of
Nothing -> False
Just a' -> a == a'

#if MIN_VERSION_base(4,9,0)
instance MonadFail (Either String) where
fail = Left

testLookMonadFail :: Property
testLookMonadFail =
(Bson.look "key" [] :: Either String Value)
-- This is as opposed to an exception thrown from Prelude.fail:
=== Left "expected \"key\" in []"
#endif

tests :: Test
tests = testGroup "Data.Bson.Tests"
[ testProperty "Val Bool" (testVal :: Bool -> Bool)
Expand All @@ -131,4 +145,8 @@ tests = testGroup "Data.Bson.Tests"
, testProperty "Val Binary" (testVal :: Binary -> Bool)
-- , testProperty "Val Document" (testVal :: Document -> Bool)
, testProperty "Val Text" (testVal :: Text -> Bool)

#if MIN_VERSION_base(4,9,0)
, testProperty "'look' uses MonadFail.fail" testLookMonadFail
#endif
]

0 comments on commit 664210b

Please sign in to comment.