parse/unparse test
This commit is contained in:
@@ -30,7 +30,6 @@ library
|
|||||||
, Core.TH
|
, Core.TH
|
||||||
, Core.HindleyMilner
|
, Core.HindleyMilner
|
||||||
, Control.Monad.Errorful
|
, Control.Monad.Errorful
|
||||||
, Data.Monoid.Utils
|
|
||||||
|
|
||||||
other-modules: Data.Heap
|
other-modules: Data.Heap
|
||||||
, Data.Pretty
|
, Data.Pretty
|
||||||
@@ -99,9 +98,12 @@ test-suite rlp-test
|
|||||||
, text
|
, text
|
||||||
, pretty
|
, pretty
|
||||||
, microlens-platform
|
, microlens-platform
|
||||||
|
|
||||||
other-modules: Arith
|
other-modules: Arith
|
||||||
, GMSpec
|
, GMSpec
|
||||||
, CoreSyntax
|
, CoreSyntax
|
||||||
, Core.HindleyMilnerSpec
|
, Core.HindleyMilnerSpec
|
||||||
|
, Core.ParseSpec
|
||||||
|
|
||||||
build-tool-depends: hspec-discover:hspec-discover
|
build-tool-depends: hspec-discover:hspec-discover
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ module Compiler.JustRun
|
|||||||
( justLexSrc
|
( justLexSrc
|
||||||
, justParseSrc
|
, justParseSrc
|
||||||
, justTypeCheckSrc
|
, justTypeCheckSrc
|
||||||
|
, RlpcError
|
||||||
|
, Program'
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
----------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------
|
||||||
|
|||||||
39
tst/Core/ParseSpec.hs
Normal file
39
tst/Core/ParseSpec.hs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
module Core.ParseSpec
|
||||||
|
( spec
|
||||||
|
)
|
||||||
|
where
|
||||||
|
----------------------------------------------------------------------------------
|
||||||
|
import CoreSyntax
|
||||||
|
import Core.Syntax
|
||||||
|
import Compiler.JustRun
|
||||||
|
import Compiler.RlpcError
|
||||||
|
import Control.Monad ((<=<))
|
||||||
|
import Data.Coerce
|
||||||
|
import Data.Text qualified as T
|
||||||
|
import Data.Functor.Classes (Eq1(..))
|
||||||
|
import Test.Hspec
|
||||||
|
import Test.QuickCheck
|
||||||
|
----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
spec :: Spec
|
||||||
|
spec = do
|
||||||
|
it "should be a right-inverse to the unparser\
|
||||||
|
\up to source code congruency" $
|
||||||
|
property $ \p -> (unparse <=< parse) p ~== Right p
|
||||||
|
|
||||||
|
-- TODO: abitrary ASTs
|
||||||
|
-- it "should be a right-inverse to the unparser\
|
||||||
|
-- \up to source code congruency" $
|
||||||
|
-- property $ \p -> (parse <=< unparse) p == Right p
|
||||||
|
|
||||||
|
(~==) :: (Eq1 f) => f ProgramSrc -> f ProgramSrc -> Bool
|
||||||
|
(~==) = liftEq congruentSrc
|
||||||
|
|
||||||
|
infix 4 ~==
|
||||||
|
|
||||||
|
parse :: ProgramSrc -> Either RlpcError Program'
|
||||||
|
parse (ProgramSrc s) = justParseSrc (T.unpack s)
|
||||||
|
|
||||||
|
unparse :: Program' -> Either RlpcError ProgramSrc
|
||||||
|
unparse = Right . unparseCoreProg
|
||||||
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
{-# LANGUAGE OverloadedStrings, LambdaCase, GeneralisedNewtypeDeriving #-}
|
{-# LANGUAGE OverloadedStrings, LambdaCase, GeneralisedNewtypeDeriving #-}
|
||||||
module CoreSyntax
|
module CoreSyntax
|
||||||
( ProgramSrc(..)
|
( ProgramSrc(..)
|
||||||
, (~==)
|
|
||||||
, congruentSrc
|
, congruentSrc
|
||||||
, unparseCoreProg
|
, unparseCoreProg
|
||||||
)
|
)
|
||||||
@@ -203,12 +202,8 @@ ws1 = elements [" ", " "]
|
|||||||
|
|
||||||
-- | Two bodies of source code are considered congruent iff the parser produces
|
-- | Two bodies of source code are considered congruent iff the parser produces
|
||||||
-- identical ASTs for both.
|
-- identical ASTs for both.
|
||||||
(~==) :: ProgramSrc -> ProgramSrc -> Bool
|
|
||||||
(~==) = (==) `on` (justParseSrc . T.unpack . coerce)
|
|
||||||
|
|
||||||
-- | Prefix synonym for @(~==)@
|
|
||||||
congruentSrc :: ProgramSrc -> ProgramSrc -> Bool
|
congruentSrc :: ProgramSrc -> ProgramSrc -> Bool
|
||||||
congruentSrc = (~==)
|
congruentSrc = (==) `on` (justParseSrc . T.unpack . coerce)
|
||||||
|
|
||||||
----------------------------------------------------------------------------------
|
----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user