From b4a1b66c58d56e5b8bb47ed66d86ff5efb309653 Mon Sep 17 00:00:00 2001 From: Robin Heggelund Hansen Date: Fri, 5 Aug 2022 14:50:15 +0200 Subject: [PATCH] Add tests for new syntax. --- gren.cabal | 1 + tests/Parse/RecordUpdateSpec.hs | 60 +++++++++++++++++++++++++++++++++ tests/Parse/SpaceSpec.hs | 2 +- 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/Parse/RecordUpdateSpec.hs diff --git a/gren.cabal b/gren.cabal index ae516540..8e7099be 100644 --- a/gren.cabal +++ b/gren.cabal @@ -238,6 +238,7 @@ Test-Suite gren-tests -- tests Parse.SpaceSpec + Parse.RecordUpdateSpec Build-Depends: hspec >= 2.7.10 && < 3 diff --git a/tests/Parse/RecordUpdateSpec.hs b/tests/Parse/RecordUpdateSpec.hs new file mode 100644 index 00000000..2633e862 --- /dev/null +++ b/tests/Parse/RecordUpdateSpec.hs @@ -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 diff --git a/tests/Parse/SpaceSpec.hs b/tests/Parse/SpaceSpec.hs index d587de4b..456b0ab9 100644 --- a/tests/Parse/SpaceSpec.hs +++ b/tests/Parse/SpaceSpec.hs @@ -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'")