From f16db81fd6bead015286192cc982f993f9c1e574 Mon Sep 17 00:00:00 2001 From: jjanzen Date: Sat, 22 Mar 2025 10:18:11 -0500 Subject: [PATCH] implemented more testing --- app/Main.hs | 1 - src/Lexer.hs | 3 +- test/Main.hs | 79 +++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 61 insertions(+), 22 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 22b2d3d..f05211f 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -4,5 +4,4 @@ import qualified Lib (someFunc) main :: IO () main = do - putStrLn "Hello, Haskell!" Lib.someFunc diff --git a/src/Lexer.hs b/src/Lexer.hs index f622664..2a445d5 100644 --- a/src/Lexer.hs +++ b/src/Lexer.hs @@ -13,7 +13,7 @@ data Token = TokLPar | TokCharacter Char | TokSymbol String | TokErr String - deriving Eq + deriving (Eq, Show) data StrState = StrStart | StrInside @@ -145,7 +145,6 @@ lexStr ('"':xs) = case identifyString ('"':xs) StrStart (ys, Just b) -> TokStr b:lexStr ys lexStr ('\'':"") = [TokErr "End of file reached before matching '"] lexStr ('\'':'(':xs) = TokQuote : lexStr ('(':xs) -lexStr ('\'':_:"") = [TokErr "End of file reached before matching '"] lexStr ('\'':x:'\'':xs) = TokCharacter x : lexStr xs lexStr (' ':xs) = lexStr xs lexStr ('\t':xs) = lexStr xs diff --git a/test/Main.hs b/test/Main.hs index 5483b06..db17f42 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -1,33 +1,74 @@ module Main (main) where -import System.Exit (exitFailure, exitSuccess) -import Control.Monad - import Test.HUnit - import Lib -test1 :: Test -test1 = TestCase (assertEqual "2==2" (2.0 :: Double) (2 :: Double)) +testPars :: Test +testPars = TestCase (assertEqual "() -> [TokLPar,TokRPar]" + [Lib.TokLPar, Lib.TokRPar] + (lexStr "()")) + +testQuotedPars :: Test +testQuotedPars = TestCase (assertEqual "'() -> [TokQuote,TokLPar,TokRPar]" + [Lib.TokQuote, Lib.TokLPar, Lib.TokRPar] + (lexStr "'()")) + +testCharacter :: Test +testCharacter = TestCase (assertEqual "'a' -> [TokCharacter 'a']" + [Lib.TokCharacter 'a'] + (lexStr "'a'")) + +testUnicodeCharacter :: Test +testUnicodeCharacter = TestCase (assertEqual "'📎' -> [TokCharacter '📎']" + [Lib.TokCharacter '📎'] + (lexStr "'📎'")) + +testSimpleIntegers :: Test +testSimpleIntegers = TestCase (assertEqual "42 -> [TokNumber 42]" + [Lib.TokNumber 42] + (lexStr "42")) + +testFractionalNumber :: Test +testFractionalNumber = TestCase (assertEqual "42.123 -> [TokNumber 42.123]" + [Lib.TokNumber 42.123] + (lexStr "42.123")) + +testExponentialInteger :: Test +testExponentialInteger = TestCase (assertEqual "42e123 -> [TokNumber 42e123]" + [Lib.TokNumber 42e123] + (lexStr "42e123")) + +testExponential :: Test +testExponential = TestCase (assertEqual "42.03E+12 -> [TokNumber 42.03e12]" + [Lib.TokNumber 42.03e12] + (lexStr "42.03E+12")) + +testNegativeNumber :: Test +testNegativeNumber = TestCase (assertEqual "-42.03e-12 -> [TokNumber -42.03e-12]" + [Lib.TokNumber (-42.03e-12)] + (lexStr "-42.03e-12")) + +testStringsAreRecognized :: Test +testStringsAreRecognized = TestCase (assertEqual + "\"\\uabcdasdf\\b\\f\\r\\n\\t\\\\\\/\" -> [Lib.TokStr \"\43981asdf\b\f\r\n\t\\/\"" + [Lib.TokStr "\43981asdf\b\f\r\n\t\\/"] + (lexStr "\"\\uabcdasdf\\b\\f\\r\\n\\t\\\\\\/\"")) tests :: Test tests = TestList [ - TestLabel "test 1" test1 + TestLabel "test paretheses are detected" testPars, + TestLabel "test quoted parentheses are detected" testQuotedPars, + TestLabel "test characters are recognized" testCharacter, + TestLabel "test unicode characters are recognized" testUnicodeCharacter, + TestLabel "test simple integers are recognized" testSimpleIntegers, + TestLabel "test fractional numbers are recognized" testFractionalNumber, + TestLabel "test integers with exponential component are recognized" testExponentialInteger, + TestLabel "test exponential number is recognized" testExponential, + TestLabel "test negative numbers are recognized" testNegativeNumber, + TestLabel "test strings are recognized" testStringsAreRecognized ] main :: IO () main = do - when (lexStr "'('a' -1.01e12 \r \"\\uabcdasdf\\b\\f\\r\\n\\t\\\\\\/\" )\"asdf" - /= [ - Lib.TokQuote, - Lib.TokLPar, - Lib.TokCharacter 'a', - Lib.TokNumber (-1.01e12), - Lib.TokStr "\43981asdf\b\f\r\n\t\\/", - Lib.TokRPar, - Lib.TokErr "End of file reached before matching \"" - ]) - exitFailure c <- runTestTT tests putStrLn (showCounts c) - exitSuccess