Add tests for new syntax.

This commit is contained in:
Robin Heggelund Hansen 2022-08-05 14:50:15 +02:00
parent 9982635f8c
commit b4a1b66c58
3 changed files with 62 additions and 1 deletions

View File

@ -238,6 +238,7 @@ Test-Suite gren-tests
-- tests
Parse.SpaceSpec
Parse.RecordUpdateSpec
Build-Depends:
hspec >= 2.7.10 && < 3

View File

@ -0,0 +1,60 @@
{-# LANGUAGE OverloadedStrings #-}
module Parse.RecordUpdateSpec where
import AST.Source qualified as Src
import Data.ByteString qualified as BS
import Helpers.Instances ()
import Parse.Expression (expression)
import Parse.Primitives qualified as P
import Reporting.Annotation qualified as A
import Test.Hspec
data ParseError
= ExprError P.Row P.Col
| OtherError String P.Row P.Col
deriving (Show, Eq)
spec :: Spec
spec = do
describe "record update" $ do
it "basic case" $
parse "{ record | prop = 1 }"
it "qualified var" $
parse "{ Module.record | prop = 1 }"
it "nested var" $
parse "{ Module.record.nested | prop = 1 }"
it "literal var" $
parse "{ { prop = 2 } | prop = 1 }"
it "if statement" $
parse "{ if 1 == 2 then { prop = 2 } else { prop = 3 } | prop = 1 }"
it "if statement with || operator" $
parse "{ if left || right then { prop = 2 } else { prop = 3 } | prop = 1 }"
it "parenthesized if statement" $
parse "{ (if 1 == 2 then { prop = 2 } else { prop = 3 }) | prop = 1 }"
it "parenthesized if statement with || operator" $
parse "{ (if left || right then { prop = 2 } else { prop = 3 }) | prop = 1 }"
--
parse :: BS.ByteString -> IO ()
parse str =
( P.fromByteString
(P.specialize (\_ row col -> ExprError row col) expression)
(OtherError "fromByteString failed")
str
)
`shouldSatisfy` isUpdateExpr
isUpdateExpr :: Either x (Src.Expr, A.Position) -> Bool
isUpdateExpr result =
case result of
Right (A.At _ (Src.Update _ _), _) -> True
_ -> False

View File

@ -68,7 +68,7 @@ spec = do
parse :: P.Parser (ParseError x) a -> BS.ByteString -> Either (ParseError x) a
parse parser =
P.fromByteString parser (OtherError "fromBytString failed")
P.fromByteString parser (OtherError "fromByteString failed")
a :: P.Parser (ParseError x) ()
a = P.word1 0x61 {- a -} (OtherError "Expected 'a'")