14.1.5 Testing the MSE grammar class
Parsers can get complex quite quickly. For example, the MSE grammar developed previously (see Section 14.1.4) even if rather small, it is reasonably intricate as can be seen in the graph below:
In this situation testing becomes central for controlling the evolution of the parser. Given its importance, PetitParser offers a dedicated infrastructure similar to the one for creating parsers.
To create a test case, we first create a subclass of PPCompositeParserTest:
PPCompositeParserTest subclass: #ExampleMSEGrammarTest
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'MSEParserExample'
In this test class, we override the parserClass
method to return the target parser to be tested:
ExampleMSEGrammarTest>>parserClass
^ ExampleMSEGrammar
Afterwards, each test method should be focused on testing one example for one production. For writing the assertion, the base test class offers a utility parse:rule:
that checks that the string passed as the first argument is parse-able using the production passed as the second argument. For example, for testing the element name we can write:
ExampleMSEGrammarTest>>testElementName
self parse: 'ABC.XYZ' rule: #elementName