implemented more testing

This commit is contained in:
jjanzen 2025-03-22 10:18:11 -05:00
parent 742df02638
commit f16db81fd6
3 changed files with 61 additions and 22 deletions

View file

@ -4,5 +4,4 @@ import qualified Lib (someFunc)
main :: IO ()
main = do
putStrLn "Hello, Haskell!"
Lib.someFunc

View file

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

View file

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