quox/tests/Tests/Lexer.idr

155 lines
4.5 KiB
Idris
Raw Normal View History

2022-05-02 11:13:13 -04:00
module Tests.Lexer
import Quox.Lexer
import TAP
RealError = Quox.Lexer.Error
%hide Quox.Lexer.Error
2022-05-02 11:13:13 -04:00
export
ToInfo RealError where
2022-05-02 20:03:22 -04:00
toInfo (Err reason line col char) =
[("reason", show reason),
("line", show line),
("col", show col),
("char", show char)]
2022-05-02 11:13:13 -04:00
data Error
= LexerError RealError
| WrongAnswer (List Token) (List Token)
2022-05-02 20:03:22 -04:00
| TestFailed (List Token)
2022-05-02 11:13:13 -04:00
ToInfo Error where
toInfo (LexerError err) = toInfo err
2022-05-02 11:13:13 -04:00
toInfo (WrongAnswer exp got) =
[("expected", show exp), ("received", show got)]
2022-05-02 20:03:22 -04:00
toInfo (TestFailed got) =
[("failed", show got)]
2022-05-02 11:13:13 -04:00
lex' : String -> Either Error (List Token)
lex' = bimap LexerError (map val) . lex
2022-05-02 11:13:13 -04:00
parameters (label : String) (input : String)
acceptsSuchThat' : (List Token -> Maybe Error) -> Test
acceptsSuchThat' p = test label $ delay $ do
res <- bimap LexerError (map val) $ lex input
2022-05-02 11:13:13 -04:00
case p res of
Just err => throwError err
2022-05-02 11:13:13 -04:00
Nothing => pure ()
2022-05-02 20:03:22 -04:00
acceptsSuchThat : (List Token -> Bool) -> Test
2022-05-02 11:13:13 -04:00
acceptsSuchThat p = acceptsSuchThat' $ \res =>
if p res then Nothing else Just $ TestFailed res
2022-05-02 20:03:22 -04:00
acceptsWith : List Token -> Test
2022-05-02 11:13:13 -04:00
acceptsWith expect = acceptsSuchThat' $ \res =>
if res == expect then Nothing else Just $ WrongAnswer expect res
accepts : Test
accepts = acceptsSuchThat $ const True
rejects : Test
rejects = testThrows label (\case LexerError _ => True; _ => False) $ delay $
2022-05-06 15:58:32 -04:00
bimap LexerError (map val) $ lex input
2022-05-02 11:13:13 -04:00
parameters (input : String) {default False esc : Bool}
show' : String -> String
show' s = if esc then show s else "\"\{s}\""
2022-05-02 20:03:22 -04:00
acceptsWith' : List Token -> Test
2022-05-02 11:13:13 -04:00
acceptsWith' = acceptsWith (show' input) input
accepts' : Test
accepts' = accepts (show' input) input
rejects' : Test
rejects' = rejects "\{show' input} (reject)" input
tests = "lexer" :- [
"comments" :- [
acceptsWith' "" [],
acceptsWith' " \n \t\t " [] {esc = True},
acceptsWith' "-- a" [],
acceptsWith' "{- -}" [],
acceptsWith' "{--}" [],
acceptsWith' "{------}" [],
acceptsWith' "{- {- -} -}" [],
acceptsWith' "{- } -}" [],
rejects' "{-}",
rejects' "{- {- -}",
acceptsWith' "( -- comment \n )" [P LParen, P RParen] {esc = True}
],
"punctuation" :- [
acceptsWith' "({[:,::]})"
2022-05-03 18:49:09 -04:00
[P LParen, P LBrace, P LSquare,
P Colon, P Comma, P DblColon,
2022-05-02 11:13:13 -04:00
P RSquare, P RBrace, P RParen],
acceptsWith' " ( { [ : , :: ] } ) "
2022-05-03 18:49:09 -04:00
[P LParen, P LBrace, P LSquare,
P Colon, P Comma, P DblColon,
2022-05-02 11:13:13 -04:00
P RSquare, P RBrace, P RParen],
acceptsWith' "-> → => ⇒ ** × << ⊲ ∷"
[P Arrow, P Arrow, P DblArrow, P DblArrow,
P Times, P Times, P Triangle, P Triangle, P DblColon],
acceptsWith' "_" [P Wild]
],
"names & symbols" :- [
2022-05-02 20:03:22 -04:00
acceptsWith' "a" [Name "a"],
acceptsWith' "abc" [Name "abc"],
acceptsWith' "_a" [Name "_a"],
acceptsWith' "a_" [Name "a_"],
acceptsWith' "a_b" [Name "a_b"],
acceptsWith' "abc'" [Name "abc'"],
acceptsWith' "a'b'c''" [Name "a'b'c''"],
acceptsWith' "abc123" [Name "abc123"],
2022-05-03 18:49:09 -04:00
acceptsWith' "_1" [Name "_1"],
2022-05-02 20:03:22 -04:00
acceptsWith' "ab cd" [Name "ab", Name "cd"],
acceptsWith' "ab{--}cd" [Name "ab", Name "cd"],
acceptsWith' "'a" [Symbol "a"],
acceptsWith' "'ab" [Symbol "ab"],
acceptsWith' "'_b" [Symbol "_b"],
2022-05-06 15:35:04 -04:00
acceptsWith' "a.b.c" [Name "a", P Dot, Name "b", P Dot, Name "c"],
2022-05-03 21:11:37 -04:00
rejects' "'",
rejects' "1abc"
2022-05-03 18:49:09 -04:00
],
2022-05-04 09:30:52 -04:00
"keywords" :- [
acceptsWith' "fun" [K Fun],
acceptsWith' "λ" [K Fun],
acceptsWith' "let" [K Let],
acceptsWith' "in" [K In],
acceptsWith' "case" [K Case],
acceptsWith' "of" [K Of],
2022-05-06 15:35:04 -04:00
acceptsWith' "ω" [K Omega],
acceptsWith' "#" [K Omega],
2022-05-04 09:30:52 -04:00
acceptsWith' "funk" [Name "funk"]
],
2022-05-03 18:49:09 -04:00
"numbers" :- [
2022-05-03 21:11:37 -04:00
acceptsWith' "0" [N Zero],
acceptsWith' "1" [N One],
acceptsWith' "2" [N $ Other 2],
acceptsWith' "69" [N $ Other 69],
acceptsWith' "1_000" [N $ Other 1000],
acceptsWith' "0o0" [N Zero],
acceptsWith' "0o105" [N $ Other 69],
acceptsWith' "0O0" [N Zero],
acceptsWith' "0O105" [N $ Other 69],
acceptsWith' "0x0" [N Zero],
acceptsWith' "0x45" [N $ Other 69],
acceptsWith' "0xabc" [N $ Other 2748],
acceptsWith' "0xABC" [N $ Other 2748],
acceptsWith' "0xA_BC" [N $ Other 2748],
acceptsWith' "0X0" [N Zero],
acceptsWith' "0X45" [N $ Other 69],
acceptsWith' "0XABC" [N $ Other 2748],
rejects' "1_",
rejects' "1__000"
2022-05-02 11:13:13 -04:00
]
]