Search code examples
eclipseunit-testingvalidationxtextrule

Xtext: combine tests and validation


I'm newbie in the world of Xtend and Unit Test. I'm testing my grammar, and I want to write a test which test the validation of it.

Here a simple example:

MyGrammar:

Domainmodel: entities+=Entity* ;

Entity:
'ENTITY' name=ID 'END_ENTITY';

Now suppose I've also inserted this validation rule:

@Check
public void checkNoJack(Entity e){
   if (e.getName().equals("Jack")){
      error("This name is not valid.",null);
   }
}

So, if I try to write:

ENTITY Jack END_ENTITY

I will receive an error.

But, if I try this Xtend test:

@Test
def void example() throws Exception{
   parserHelper.parse("ENTITY Jack END_ENTITY")
}

all will go right.

How can I consider validation rules in my tests?

UPDATE: name parsed correctly:

@Test
    def void example() throws Exception{
        val model = parserHelper.parse("ENTITY John END_ENTITY");
        val entity=model.entities.get(0);
        assertEquals(entity.name, "John");
    }

    @Test
    def void example2() throws Exception{
        val model = parserHelper.parse("ENTITY Jack END_ENTITY");
        val entity=model.entities.get(0);
        assertEquals(entity.name, "Jack");
    }

Solution

  • Solved with use of ParserHelper and method:

    parse.assertNoError