packages feed

spade-0.1.0.0: test/Common.hs

module Common where

import "spade" Common
import Control.Monad.IO.Class
import Data.Text as T
import Parser
import Test.Common
import Test.Hspec.Hedgehog

import Compiler.AST.Parser.Common

specGenerateAndParse :: forall a.(Show a, Eq a, HasParser a, HasGen a, ToSource a) => Spec
specGenerateAndParse = modifyMaxSuccess (const 100) $ describe "Parser for element" $
  it "can parse generated code" $ hedgehog $ do
    e <- forAll (getGen @a)
    r <- liftIO $ runParser parser $ toTextWithOffset (toSource e)
    footnote $ "Failed source: " <> (T.unpack $ toSource e)
    footnote $ "Tokens: " <> (show e)
    r === (Just e)

specGenerateAndParseAst :: forall a.(Show a, Eq a, HasAstParser a, HasGen a, ToSource a) => Spec
specGenerateAndParseAst = modifyMaxSuccess (const 100) $ describe "Parser for code" $
  it "can parse generated code <> ast" $ hedgehog $ do
    e <- forAll (getGen @a)
    let src = toSource e
    (r, tokens) <- liftIO $ runParserEither parser (toTextWithOffset src) >>= \case
      Right tokens -> do
        r <- runParserEither astParser (mkAstParserState tokens)
        pure (r, tokens)
      Left err     -> error $ "Tokinzer failed" <> (show err)
    footnote $ "Failed source: " <> (T.unpack src)
    footnote $ "Tokens: " <> (show tokens)
    (r === (Right e))