I wrote a parser for a non-trivial language this weekend. Some of the output can be complex, even for seemingly simple input. Let’s say the input to the parser is a mathematical expression, and the output is a list of tuples that describe the input.
So the output could be 20 lines long.
How would you write the junit test? Would you run the parser, hand-check the result, and if it seems correct, drop the result into the unit test as the Right Answer?
Or is this just insane, and I need to do something differently?
Ideally the idea of a “unit” test is that it tests a small unit of functionality. If the output is so complex that it’s difficult to test, that implies that you’re testing too large a unit of functionality.
Remember that in addition to verifying that your code works, your unit tests can also act as an example of how your code should be used. A single test that just matches a result against a large predefined result probably won’t do that.
Try to break the inner workings into smaller methods and test each one. Try to test building up a result from smaller results (e.g. if input A results in output Y, and input B results in output Z, then write a test for whether input AB results in output YZ, or whatever the appropriate result would be).