java - How to test an interpreter using JUnit? -


i writing tests interpreter programming language in java using junit framework. end i've created large number of test cases of them containing code snippets in language under testing. since these snippets small convenient embed them in java code. however, java doesn't support multiline string literals makes code snippets bit obscure due escape sequences , necessity split longer string literals, example:

string output = run("let := 21;\n" +                     "let b := 21;\n" +                     "print + b;"); assertequals(output, "42"); 

ideally like:

string output = run("""     let := 21;     let b := 21;     print + b; """); assertequals(output, "42"); 

one possible solution move code snippets external files , refer each file corresponding test case. adds significant maintenance burden.

another solution use different jvm language, such scala or jython support multiline string literals, write tests. add new dependency project , require port existing tests.

is there other way keep clarity of test code snippets while not adding maintenance?

moving test cases file worked me in past, interpreter well:

  1. created xml file containg snippets interpreted expected result. simple xml definition, list of test elements containing testid, value, expected result, type, , description.
  2. implemented 1 junit test read file , looped through contents, in case of failure used testid , description log failing tests.

it worked because had 1 generic well-defined interface interpreter run method, refactoring still possible. in our case did not increase maintenance effort, in fact create new tests adding more elements xml file.

maybe not optimal way in unit tests should used, worked us.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -