From 23998303665d136d7fe0d4c4a7609cf41401ae31 Mon Sep 17 00:00:00 2001 From: James Eversole Date: Thu, 19 Dec 2024 20:26:46 -0600 Subject: [PATCH] Additional test coverage for lexing and parsing --- src/Parser.hs | 2 -- test/Spec.hs | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/Parser.hs b/src/Parser.hs index 611d35c..49bac82 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -123,8 +123,6 @@ parseListLiteral = do satisfy (== LCloseBracket) return (SList elements) ---parseListItem :: Parser SaplingAST ---parseListItem = parseGroupedItem <|> parseSingleItem parseListItem :: Parser SaplingAST parseListItem = choice [ parseGroupedItem -- Handle expressions inside parentheses diff --git a/test/Spec.hs b/test/Spec.hs index 117d08c..299a823 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -41,6 +41,22 @@ lexerTests = testGroup "Lexer Tests" case runParser saplingLexer "" input of Left _ -> return () Right _ -> assertFailure "Expected failure on invalid input" + + , testCase "Lex escaped characters in strings" $ do + let input = "\"hello\\nworld\"" + let expected = Right [LStringLiteral "hello\\nworld"] + runParser saplingLexer "" input @?= expected + + , testCase "Lex mixed literals" $ do + let input = "t \"string\" 42" + let expected = Right [LKeywordT, LStringLiteral "string", LIntegerLiteral 42] + runParser saplingLexer "" input @?= expected + + , testCase "Lex invalid token" $ do + let input = "$invalid" + case runParser saplingLexer "" input of + Left _ -> return () + Right _ -> assertFailure "Expected lexer to fail on invalid token" ] parserTests :: TestTree @@ -61,7 +77,7 @@ parserTests = testGroup "Parser Tests" parseSapling input @?= expected , testCase "Parse mixed list literals" $ do - -- You must put non-listliterals in parentheses + -- You must put non-list literals in parentheses let input = "[t (\"hello\") t]" let expected = SList [TLeaf, SStr "hello", TLeaf] parseSapling input @?= expected @@ -97,6 +113,28 @@ parserTests = testGroup "Parser Tests" let expected = SList [TLeaf, TLeaf] parseSapling input1 @?= expected parseSapling input2 @?= expected + + , testCase "Parse string in list" $ do + let input = "[(\"hello\")]" + let expected = SList [SStr "hello"] + parseSapling input @?= expected + + , testCase "Parse parentheses inside list" $ do + let input = "[t (t t)]" + let expected = SList [TLeaf, TStem TLeaf] + parseSapling input @?= expected + + -- Do I want to allow multi-line indentation-sensitive syntax? + -- Probably not. + --, testCase "Parse multi-line function definition" $ do + -- let input = "f x y =\n t t" + -- let expected = SFunc "f" ["x", "y"] (TStem TLeaf) + -- parseSapling input @?= expected + + , testCase "Parse nested parentheses in function body" $ do + let input = "f = t (t (t t))" + let expected = SFunc "f" [] (TStem (TStem (TStem TLeaf))) + parseSapling input @?= expected ] integrationTests :: TestTree @@ -191,3 +229,4 @@ propertyTests = testGroup "Property Tests" Left _ -> property True Right ast -> parseSapling input === ast ] +