Skip to content

Commit

Permalink
assists #21 -- stack test gives prettier error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mengwong committed Oct 19, 2021
1 parent e00f74e commit 8afebc6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions mengwong/mp/src/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import Data.Vector (imap, foldl')
import qualified Data.Text.Lazy as Text
import Control.Arrow ((>>>))

data Custom = MyCustom String
deriving (Eq, Show, Ord)

instance ShowErrorComponent Custom where
showErrorComponent (MyCustom str) = str

-- custom version of https://hackage.haskell.org/package/megaparsec-9.2.0/docs/src/Text.Megaparsec.Error.html#errorBundlePretty
errorBundlePrettyCustom ::
forall e .
Expand Down
26 changes: 25 additions & 1 deletion mengwong/mp/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,40 @@
module Main where

import Test.Hspec
import Test.Hspec.Megaparsec
import Test.Hspec.Megaparsec hiding (shouldParse)
import Text.Megaparsec
import Lib
import AnyAll
import Types
import Error
import qualified Data.ByteString.Lazy as BS
import Control.Monad.Reader (ReaderT(runReaderT))
import System.Environment (lookupEnv)



-- | Create an expectation by saying what the result should be.
--
-- > parse letterChar "" "x" `shouldParse` 'x'
shouldParse ::
( HasCallStack,
ShowErrorComponent e,
Show a,
Eq a
) =>
-- | Result of parsing as returned by function like 'parse'
Either (ParseErrorBundle MyStream e) a ->
-- | Desired result
a ->
Expectation
r `shouldParse` v = case r of
Left e ->
expectationFailure $
"expected: " ++ show v
++ "\nbut parsing failed with error:\n"
++ errorBundlePrettyCustom e
Right x -> x `shouldBe` v

main :: IO ()
main = do
mpd <- lookupEnv "MP_DEBUG"
Expand Down

0 comments on commit 8afebc6

Please sign in to comment.