crafting-interpreters/test/ScannerTest.hs
Sanchayan Maity 997a0d11f8 Implement scanning using megaparsec
This is for the fourth chapter of Crafting Interpreters.
https://craftinginterpreters.com/scanning.html

This is only a first pass. Still need to test complex expressions.
Basic scanning/parsing works. See test.
2023-06-20 18:58:52 +05:30

32 lines
1.7 KiB
Haskell

module ScannerTest where
import Data.Either (rights)
import Data.Text (pack)
import LoxTypes
import MyLib
import Test.Hspec
-- Test are taken from https://github.com/munificent/craftinginterpreters/tree/master/test/scanning
spec_scanner :: Spec
spec_scanner = do
describe "Correctly scans and parses keywords" $ do
it "all are Literal Identifier Keywords" $ do
res <- runLox $ pack "\"and\" \"class\" \"else\" \"false\" \"for\" \"fun\" \"if\" \"nil\" \"or\" \"return\" \"super\" \"this\" \"true\" \"var\" \"while\""
rights res `shouldBe` [[Lit (Str "and"),Lit (Str "class"),Lit (Str "else"),Lit (Str "false"),Lit (Str "for"),Lit (Str "fun"),Lit (Str "if"),Lit (Str "nil"),Lit (Str "or"),Lit (Str "return"),Lit (Str "super"),Lit (Str "this"),Lit (Str "true"),Lit (Str "var"),Lit (Str "while")]]
describe "Correctly scans and parses numbers" $ do
it "all are Literal Number's" $ do
res <- runLox $ pack "123.45 456.0 445.112345 123456"
rights res `shouldBe` [[Lit (Number 123.45),Lit (Number 456.0),Lit (Number 445.112345),Lit (Number 123456.0)]]
describe "Correctly scans and parses punctuators" $ do
it "all are char tokens and operators" $ do
res <- runLox $ pack "(){};,+-*!===<=>=!=<>/."
rights res `shouldBe` [[Char LeftParen,Char RightParen,Char LeftBrace,Char RightBrace,Char Semicolon,Char Comma,Char Plus,Char Minus,Char Star,Op BangEqual,Op EqualEqual,Op LessEqual,Op GreaterEqual,Op BangEqual,Op Less,Op Greater,Char Slash,Char Dot]]
describe "Correctly scans and parses strings" $ do
it "all are strings" $ do
res <- runLox $ pack "\"\" \"string\""
rights res `shouldBe` [[Lit (Str ""),Lit (Str "string")]]