Alexey Romanov
Alexey Romanov

Reputation: 170805

Testing that something fails to parse in Xtext

I have the following grammar:

grammar ru.focusmedia.fire.idl.IDL with org.eclipse.xtext.xbase.Xbase

generate idl "http://www.focusmedia.ru/fire/idl/IDL"

Model:
    'package' package=QualifiedName
    imports+=Import*
    typeDefs+=TypeDef+;

...

So the string asd should fail to parse, as should anything not starting with package. I expected ParseHelper.parse("asd") to throw an exception or to return null, but it does neither. How do I recognize that parsing failed?

Upvotes: 1

Views: 256

Answers (2)

Sebastian Zarnekow
Sebastian Zarnekow

Reputation: 6729

You can query the resource that contains the parse result for errors by means of Resource#getErrors. Something like resultFromParseHelper.eResource().getErrors() should do the trick.

Using junit:

Assert.assertEquals(result.eResource().getErrors().toString,0,result.eResource().getErrors().size)

Upvotes: 4

Alexey Romanov
Alexey Romanov

Reputation: 170805

Another option is to use ValidationTestHelper.

Upvotes: 0

Related Questions