quox/tests/Tests/FromPTerm.idr

100 lines
2.8 KiB
Idris
Raw Normal View History

2023-03-10 15:52:29 -05:00
module Tests.FromPTerm
2023-03-31 17:43:25 -04:00
import Quox.Parser.FromParser
2023-03-10 15:52:29 -05:00
import Quox.Parser
import TermImpls
2023-03-13 14:33:09 -04:00
import TypingImpls
import Tests.Parser as TParser
2023-03-10 15:52:29 -05:00
import TAP
2023-03-31 17:43:25 -04:00
import Quox.EffExtra
2023-03-10 15:52:29 -05:00
2023-03-13 14:33:09 -04:00
import System.File
2023-03-10 15:52:29 -05:00
import Derive.Prelude
%language ElabReflection
%hide TParser.Failure
%hide TParser.ExpectedFail
2023-03-10 15:52:29 -05:00
public export
data Failure =
2023-03-31 17:43:25 -04:00
ParseError Parser.Error
| FromParser FromParser.Error
2023-03-10 15:52:29 -05:00
| WrongResult String
| ExpectedFail String
2023-03-13 14:33:09 -04:00
%runElab derive "FileError" [Show]
2023-03-10 15:52:29 -05:00
export
ToInfo Failure where
toInfo (ParseError err) = toInfo err
2023-03-13 14:33:09 -04:00
toInfo (FromParser err) =
[("type", "FromParserError"),
2023-03-31 17:43:25 -04:00
("got", show $ prettyError True True err)]
2023-03-10 15:52:29 -05:00
toInfo (WrongResult got) =
[("type", "WrongResult"), ("got", got)]
toInfo (ExpectedFail got) =
[("type", "ExpectedFail"), ("got", got)]
parameters {c : Bool} {auto _ : Show b}
2023-03-31 17:43:25 -04:00
(grm : Grammar c a) (fromP : a -> Either FromParser.Error b)
2023-03-10 15:52:29 -05:00
(inp : String)
parameters {default (ltrim inp) label : String}
parsesWith : (b -> Bool) -> Test
parsesWith p = test label $ do
pres <- mapFst ParseError $ lexParseWith grm inp
2023-03-13 14:33:09 -04:00
res <- mapFst FromParser $ fromP pres
2023-03-10 15:52:29 -05:00
unless (p res) $ Left $ WrongResult $ show res
parses : Test
parses = parsesWith $ const True
parsesAs : Eq b => b -> Test
parsesAs exp = parsesWith (== exp)
parameters {default "\{ltrim inp} # fails" label : String}
parseFails : Test
parseFails = test label $ do
pres <- mapFst ParseError $ lexParseWith grm inp
2023-03-10 15:52:29 -05:00
either (const $ Right ()) (Left . ExpectedFail . show) $ fromP pres
FromString BName where fromString = Just . fromString
2023-03-31 17:43:25 -04:00
runFromParser : Eff [Except FromParser.Error] a -> Either FromParser.Error a
runFromParser = extract . runExcept
2023-03-10 15:52:29 -05:00
export
tests : Test
tests = "PTerm → Term" :- [
"dimensions" :-
2023-03-31 17:43:25 -04:00
let fromPDim = runFromParser . fromPDimWith [< "𝑖", "𝑗"]
2023-03-10 15:52:29 -05:00
in [
note "dim ctx: [𝑖, 𝑗]",
parsesAs dim fromPDim "𝑖" (BV 1),
parsesAs dim fromPDim "𝑗" (BV 0),
parseFails dim fromPDim "𝑘",
parsesAs dim fromPDim "0" (K Zero),
parsesAs dim fromPDim "1" (K One)
],
"terms" :-
2023-03-31 17:43:25 -04:00
let fromPTerm = runFromParser .
fromPTermWith [< "𝑖", "𝑗"] [< "A", "x", "y", "z"]
2023-03-10 15:52:29 -05:00
in [
note "dim ctx: [𝑖, 𝑗]; term ctx: [A, x, y, z]",
2023-03-31 17:43:25 -04:00
parsesAs term fromPTerm "x" $ BVT 2,
parseFails term fromPTerm "𝑖",
parsesAs term fromPTerm "f" $ FT "f",
parsesAs term fromPTerm "λ w ⇒ w" $ [< "w"] :\\ BVT 0,
parsesAs term fromPTerm "λ w ⇒ x" $ [< "w"] :\\ BVT 3,
parsesAs term fromPTerm "λ x ⇒ x" $ [< "x"] :\\ BVT 0,
parsesAs term fromPTerm "λ a b ⇒ f a b" $
[< "a", "b"] :\\ E (F "f" :@@ [BVT 1, BVT 0]),
2023-03-31 17:43:25 -04:00
parsesAs term fromPTerm "f @𝑖" $
2023-03-10 15:52:29 -05:00
E $ F "f" :% BV 1
],
todo "everything else"
2023-03-10 15:52:29 -05:00
]