Uni-Logo
OrganisationExercisesTools and SupportForum

Test-driven Parser Development

From exercise 2 on sheet 1 the corresponding Standard Project Templates help you do your development in a test-driven way.

What the test suite does

The template contains a JUnit test FileParsingTest, which makes sure it runs once for each file in the directories testdata/ok and testdata/error. Each ok file is expected to be accepted by your parser. Each error file is expected to be rejected by your parser. For each failing expectation, you get a JUnit failure.

Running tests

Once you have generated the parser, right-click on the project, choose "Run As/JUnit test". That will cause Eclipse to execute all JUnit tests in the project, i.e. the FileParsingTest. You will then see a JUnit view with a tree. The tree contains green nodes for passing tests, blue nodes for failing tests and red nodes for unexpected run-time errors.

Growing a grammar one piece at a time

Start with a grammar which can only recognize programs consisting of an empty main class. Run the test suite and keep debugging your grammar until the initial test suite goes green.

Then, until you're done,

  1. pick a rule from the grammar which is already reachable from the grammar you have covered (in the beginning e.g. VarDecl, rather not ParamList, which only makes sense once you can parse methods)
  2. Write one or more valid MiniJava programs which use the feature, and put them into testdata/ok.
  3. Create damaged versions of your new test cases which are in an interesting way syntactically invalid, and put them into testdata/error.
  4. Check that all of the new "testdata/ok" tests fail. If other tests fail, fix that problem first. If new tests don't fail, that's a bad sign, too.
  5. Extend your grammar
  6. Fix your grammar until all tests are green

If you have MiniJava programs which you don't expect to parse until later, you can stash them away in testdata/ok-future.