Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ in upstream
-}
let upstream =
https://raw.githubusercontent.com/purescript/package-sets/prepare-0.15/src/packages.dhall
sha256:0dc2211060c4f5fac8dc6797b0eb9e192aa19494b07b30952c64c4f7881f4574

in upstream
with metadata.version = "v0.15.0-alpha-02"
1 change: 1 addition & 0 deletions src/Test/Spec/Assertions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const unsafeStringify = x => JSON.stringify(x, null, 2)
65 changes: 42 additions & 23 deletions src/Test/Spec/Assertions.purs
Original file line number Diff line number Diff line change
@@ -1,99 +1,118 @@
module Test.Spec.Assertions
( fail
, shouldEqual
, shouldNotEqual
( NoShow(..)
, expectError
, fail
, shouldContain
, shouldEqual
, shouldNotContain
, shouldNotEqual
, shouldNotReturn
, shouldNotSatisfy
, shouldSatisfy
, expectError
, shouldReturn
, shouldNotReturn
) where
, shouldSatisfy
, class Printable
, print
)
where

import Prelude

import Control.Monad.Error.Class (class MonadError, class MonadThrow, throwError, try)
import Data.Either (Either(..))
import Data.Foldable (class Foldable, notElem, elem)
import Data.Newtype (class Newtype)
import Effect.Exception (Error, error)

fail :: forall m. MonadThrow Error m => String -> m Unit
fail = throwError <<< error

foreign import unsafeStringify :: forall a. a -> String

newtype NoShow a = NoShow a
instance Newtype (NoShow a) a
derive newtype instance (Eq a) => Eq (NoShow a)

class Printable a where
print :: a -> String

instance Printable (NoShow a) where
print = unsafeStringify
else instance Show a => Printable a where
print = show

shouldEqual
:: forall m t
. MonadThrow Error m
=> Show t
=> Printable t
=> Eq t
=> t
-> t
-> m Unit
shouldEqual v1 v2 =
when (v1 /= v2) $
fail $ show v1 <> " ≠ " <> show v2
fail $ print v1 <> " ≠ " <> print v2

shouldNotEqual
:: forall m t
. MonadThrow Error m
=> Show t
=> Printable t
=> Eq t
=> t
-> t
-> m Unit
shouldNotEqual v1 v2 =
when (v1 == v2) $
fail $ show v1 <> " = " <> show v2
fail $ print v1 <> " = " <> print v2

shouldSatisfy
:: forall m t
. MonadThrow Error m
=> Show t
=> Printable t
=> t
-> (t -> Boolean)
-> m Unit
shouldSatisfy v pred =
unless (pred v) $
fail $ show v <> " doesn't satisfy predicate"
fail $ print v <> " doesn't satisfy predicate"

shouldNotSatisfy
:: forall m t
. MonadThrow Error m
=> Show t
=> Printable t
=> t
-> (t -> Boolean)
-> m Unit
shouldNotSatisfy v pred =
when (pred v) $
fail $ show v <> " satisfies predicate, but should not"
fail $ print v <> " satisfies predicate, but should not"

shouldContain
:: forall m f a
. MonadThrow Error m
=> Show a
=> Printable a
=> Eq a
=> Show (f a)
=> Printable (f a)
=> Foldable f
=> f a
-> a
-> m Unit
shouldContain c e =
when (e `notElem` c) $
fail $ (show e) <> " ∉ " <> (show c)
fail $ (print e) <> " ∉ " <> (print c)

shouldNotContain
:: forall m f a
. MonadThrow Error m
=> Show a
=> Printable a
=> Eq a
=> Show (f a)
=> Printable (f a)
=> Foldable f
=> f a
-> a
-> m Unit
shouldNotContain c e =
when (e `elem` c) $
fail $ (show e) <> " ∈ " <> (show c)
fail $ (print e) <> " ∈ " <> (print c)

expectError
:: forall m t
Expand All @@ -111,7 +130,7 @@ shouldReturn
:: forall m t
. MonadThrow Error m
=> Eq t
=> Show t
=> Printable t
=> m t
-> t
-> m Unit
Expand All @@ -122,7 +141,7 @@ shouldNotReturn
:: forall m t
. MonadThrow Error m
=> Eq t
=> Show t
=> Printable t
=> m t
-> t
-> m Unit
Expand Down
11 changes: 10 additions & 1 deletion test/Test/Spec/AssertionSpec.purs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
module Test.Spec.AssertionSpec where

import Prelude
import Effect.Exception (error)

import Control.Monad.Error.Class (throwError)
import Effect.Exception (error)
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (NoShow(..))
import Test.Spec.Assertions as A
import Test.Spec.Assertions.String as AS

newtype MyInt = MyInt Int
derive newtype instance Eq MyInt

assertionSpec :: Spec Unit
assertionSpec =
describe "Test" $
Expand Down Expand Up @@ -90,3 +95,7 @@ assertionSpec =
f `A.shouldNotContain` notcontained
it "rejects f that contains a" $
A.expectError $ f `A.shouldNotContain` contained

describe "shouldEqualAny" do
it "any type with an Eq instance" $
(NoShow $ MyInt 3) `A.shouldEqual` (NoShow $ MyInt 3)