Testing parsers is not entirely trivial. (Depending on the complexity of the thing, as with all testing.)
One thing you can do is write an Arbitrary
instance which constructs all valid expressions (or whatever it is you're trying to parse), and then check that if you pretty-print the thing and then parse the resulting string, you get back exactly what you started with.
There are a couple of problems with that:
If the answer is wrong, what's broken? The parser or the printer?
If the thing you're parsing is complicated enough to have optional brackets and things, you need to check it works both with and without the optional brackets. Your pretty printer will only do it one way or the other, usually.
This doesn't check that garbage input really does get rejected (and not parsed as something strange). E.g., I've written plenty of Parsec parsers which will silently ignore a syntax error if it occurs at the end of the input.
In general, the only really thorough way I know of testing a parser is to just write lots and lots of manual tests by hand. (And every time you find something that parses wrong, add another test case for it.) This is essentially what GHC does with their test suit, for example.
Of course, it depends how complex your parser is, and how much assurance you want... If you're just parsing JSON, you can probably test it fairly easily. If you're parsing something like Markdown... my God have mercy on your soul!
id = parse . pretty
be good enough? (Also,oneOf "_" == char '_'
) – Ingraspaces
or other skippable characters in parser, then, the result produced would be different. And additionalparse
would help. – Wicket