Additional test coverage for lexing and parsing

This commit is contained in:
James Eversole 2024-12-19 20:26:46 -06:00
parent 51f3f8f21d
commit 2399830366
2 changed files with 40 additions and 3 deletions

View File

@ -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

View File

@ -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
]