packages feed

aihc-parser-1.0.0.2: test/Test/Properties/ExprRoundTrip.hs

{-# LANGUAGE OverloadedStrings #-}

module Test.Properties.ExprRoundTrip
  ( prop_exprPrettyRoundTrip,
  )
where

import Aihc.Parser
import Aihc.Parser.Parens (addExprParens)
import Aihc.Parser.Pretty ()
import Aihc.Parser.Syntax
import Data.Text qualified as T
import Prettyprinter (Pretty (..), defaultLayoutOptions, layoutPretty)
import Prettyprinter.Render.Text (renderStrict)
import Test.Properties.Arb.Expr ()
import Test.Properties.Arb.Utils (requiredExtensions)
import Test.Properties.Coverage (assertCtorCoverage)
import Test.QuickCheck

exprConfig :: ParserConfig
exprConfig =
  defaultConfig
    { parserExtensions = requiredExtensions
    }

prop_exprPrettyRoundTrip :: Expr -> Property
prop_exprPrettyRoundTrip expr =
  let parenthesized = addExprParens expr
      source = renderStrict (layoutPretty defaultLayoutOptions (pretty parenthesized))
      expected = stripAnnotations parenthesized
   in assertCtorCoverage ["EAnn"] expr $
        counterexample (T.unpack source) $
          case parseExpr exprConfig source of
            ParseErr err ->
              counterexample (formatParseErrors (parserSourceName exprConfig) (Just source) err) False
            ParseOk parsed ->
              let actual = stripAnnotations parsed
               in counterexample ("expected: " <> show expected <> "\nactual: " <> show actual) (expected == actual)