[antlr-interest] JUnit testing of ASTs

Benny & Wendy Sadeh sadeh at mindsmiths.com
Tue Dec 21 11:24:46 PST 2004


myself, I like to keep the tests in the code. it may be harder to write them 
but I think it is easier to understand them that way. YMMV.

also, I divide my tests into two categories:
1. the parsing tests (for the language itself), and
2. the usage tests (for the code which uses the language)
(I keep them in tow separate packages)

for example:

1.
public class CreateTypeTest extends ParserBasedTest {

 public void testCreateTypeWithPrimaryKey() {
  statement =
   "CREATE TYPE typeA(fieldA1 INTEGER, fieldA2 STRING(100))" +
   "PRIMARY KEY (fieldA1)";
  assertEquals("( TYPE typeA ( FIELD_DEFINITION_LIST ( FIELD_DEFINITION 
fieldA1 INTEGER ) ( FIELD_DEFINITION fieldA2 STRING 100 ) ) ( KEY_LIST 
fieldA1 ) )", parse(statement));

  statement =
   "CREATE TYPE typeB(fieldB1 string(30), fieldB2 string(15))" +
   "PRIMARY KEY (fieldB1, fieldB2)";
  assertEquals("( TYPE typeB ( FIELD_DEFINITION_LIST ( FIELD_DEFINITION 
fieldB1 string 30 ) ( FIELD_DEFINITION fieldB2 string 15 ) ) ( KEY_LIST 
fieldB1 fieldB2 ) )", parse(statement));
 }

... other tests ...
}

2.
public class CreateTypeTest extends EvaluatorBasedTest {
 private DataType type;

 public void testCreateTypeWithPrimaryKey() {
  assertNull(engine.typeFor("typeA"));
  type = (DataType) parse(
   "CREATE TYPE typeA(fieldA1 INTEGER, fieldA2 STRING(100)) \n" +
   "PRIMARY KEY (fieldA1)");
  assertNotNull(type);
  assertEquals(engine.typeFor("typeA"), type);
  assertEquals("typeA$(typeA)[fieldA1]", type.primaryView().toString());

  assertNull(engine.typeFor("typeB"));
  type = (DataType) parse(
   "CREATE TYPE typeB(fieldB1 string(30), fieldB2 string(15)) \n" +
   "PRIMARY KEY (fieldB1, fieldB2)");
  assertNotNull(type);
  assertEquals(engine.typeFor("typeB"), type);
  assertEquals("typeB$(typeB)[fieldB1, fieldB2]", 
type.primaryView().toString());
 }


... other tests ...
}


Benny


----- Original Message ----- 
From: "Rodrigo B. de Oliveira" <rodrigobamboo at gmail.com>
To: "Paul J. Lucas" <pauljlucas at mac.com>
Cc: "ANTLR Interest" <antlr-interest at antlr.org>
Sent: Tuesday, December 21, 2004 10:57 AM
Subject: Re: [antlr-interest] JUnit testing of ASTs


> That's the exact approach I used in boo and I really like the way it 
> works.
>
> The only significant difference is that the expected output is
> embedded in the test file (the one to be parsed) as a docstring:
>
> <this is a test file>
> """
> a = 'Hello!'
> print a
> """
> a = "Hello"
> print a
> </this is a test file>
>
> The build generates the actual nunit test fixture class by looking in
> a specific directory for test files. That way, adding a new test case
> is as simple as "Save As...".
>
> Best wishes,
> Rodrigo
>
> On Tue, 21 Dec 2004 10:20:37 -0800 (PST), Paul J. Lucas
> <pauljlucas at mac.com> wrote:
>>         Does anybody do JUnit regression testing on their parsers?  If
>>         so, how, exactly?
>>
>>         For lack of anything better, *a* way of doing it is to have a
>>         set of files to parse and the pretty-printed output of the
>>         expected ASTs.  Then, when you run the tests, simply diff their
>>         output with the expected output.
>>
>>         Anything better?
>>
>>         - Paul
>>
> _______________________________________________
> antlr-interest mailing list
> antlr-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/antlr-interest
> 



More information about the antlr-interest mailing list