@@ -0,0 +1,48 @@
1+module ViTest.Expect.Either where
5+import Data.Either (Either(..), either)
6+import Effect.Aff (Aff)
7+import ViTest.Expect (expectToBe, expectToEqual)
9+-- | Assert that a value is Right with a specific value
10+expectRight :: forall e a. Eq a => Show e => a -> Either e a -> Aff Unit
11+expectRight expected result = case result of
12+ Right actual -> expectToEqual expected actual
13+ Left err -> expectToBe true false -- Always fails, shows error in output
15+-- | Assert that a value is Right (don't care about the value)
16+expectIsRight :: forall e a. Show e => Either e a -> Aff Unit
17+expectIsRight result = case result of
18+ Right _ -> expectToBe true true
19+ Left err -> expectToBe true false
21+-- | Assert that a value is Left with a specific error
22+expectLeft :: forall e a. Eq e => Show a => e -> Either e a -> Aff Unit
23+expectLeft expected result = case result of
24+ Left actual -> expectToEqual expected actual
25+ Right val -> expectToBe true false
27+-- | Assert that a value is Left (don't care about the error)
28+expectIsLeft :: forall e a. Show a => Either e a -> Aff Unit
29+expectIsLeft result = case result of
30+ Left _ -> expectToBe true true
31+ Right val -> expectToBe true false
33+-- | Assert that a Left contains a specific substring in its error
34+expectLeftContains :: forall a. Show a => String -> Either String a -> Aff Unit
35+expectLeftContains needle result = case result of
37+ if contains needle err
38+ then expectToBe true true
39+ else expectToBe true false -- Fails with message about not containing
40+ Right val -> expectToBe true false
42+ contains :: String -> String -> Boolean
43+ contains n h = indexOf n h >= 0
45+ indexOf :: String -> String -> Int
46+ indexOf = indexOfImpl
48+foreign import indexOfImpl :: String -> String -> Int