From d7f713839cd865c058d25240c66640baefe423c2 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Thu, 1 Feb 2024 01:52:15 +0100 Subject: [PATCH] Added a test: see if my parsers aren't modifying the input. --- test/Main.purs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/test/Main.purs b/test/Main.purs index 0afab71..b4d22a9 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -1,6 +1,6 @@ module Test.Main where -import Prelude (Unit, discard, show, ($), (<>)) +import Prelude (Unit, discard, show, ($), (<>), (==)) import Data.Either (Either(..)) import Data.Maybe (Maybe(..), maybe) import Data.String.CodeUnits (fromCharArray) @@ -17,6 +17,29 @@ import GenericParser.IPAddress as IP import GenericParser.EmailAddress as E import Test.TestValues as T +run :: forall e v. Parser e v -> String -> P.Result e v +run (Parser p) str = p { string: str, position: 0 } + +data COMPARISON = SAME | DIFFERENT | FAILED +compare_results :: forall e v. Parser e v -> Parser e v -> String -> COMPARISON +compare_results p1 p2 str + = let e1 = run p1 str + e2 = run p2 str + in case e1, e2 of + Right r1, Right r2 -> if r1.suffix == r2.suffix + then SAME + else DIFFERENT + _, _ -> FAILED + +compare_parsers :: forall e v. String -> Parser e v -> Parser e v -> String -> Effect Unit +compare_parsers s p1 p2 str + = log $ s <> " " + <> case compare_results p1 p2 str of + SAME -> "SAME" + DIFFERENT -> "DIFFERENT" + FAILED -> "FAILED" + <> " [" <> str <>"] " + logtest :: forall e v. String -> Parser e v -> String -> (v -> String) -> (e -> String) -> Effect Unit logtest fname (Parser p) str r e = do log $ "(" <> fname <> ") parsing '" <> str <> "': " @@ -99,3 +122,6 @@ main = do let quotedstrings = [ """" "spaces?""", """ " " """ ] test_series "E.quoted_string---------------" E.quoted_string id showerror_email quotedstrings test_series "P.read_input (E.quoted_string)" (P.read_input E.quoted_string) id showerror_email quotedstrings + + log "Does parsers behave correctly (give the exact same input)?" + foreachE T.valid_email_addresses_short (\s -> compare_parsers "E.address" (P.read_input E.address) E.address s)