Problem: Validator/Generator cannot navigate through AST/Index/ResourceSet from Unit-Tests [message #1725978] |
Tue, 08 March 2016 19:11 |
Tobias L. Messages: 24 Registered: July 2010 |
Junior Member |
|
|
Hello *,
I would like to implement unit tests for mydsl. Specifically for the validator and the generators.
Both the Validator, and the generators query the index (ResourceSet) to objects in the AST - and find nothing. Why not? (Please see attached file).
In unit test I set up a ResourceSet of three models and can navigate through the AST also. However, the validator and the generators can not access the ResourceSet (or find anything).
Why is that?
The most important code snippets (for example ValidatorTest):
Unit-Test:
@RunWith(XtextRunner)
@InjectWith(MyDslInjectorProvider)
class MyDslValidatorTest {
static val DATATYPES = '''
datatypes {
type Integer
type String
type Date
type Timestamp
}
'''
// static val CONFIGURATION = '''
// configuration ConfigX {
// }
// '''
@Inject extension ParseHelper<Model>
@Inject extension ValidationTestHelper
@Inject extension Provider<XtextResourceSet>
var resourceSet = null as XtextResourceSet
@Before
def void setup() {
resourceSet = get
resourceSet.classpathURIContext = this
DATATYPES.parse(URI.createURI("datatypes.mydsl"), resourceSet)
// TESTCASE: no config should raise an error in MyDslValidator
// val config = resourceSet.createResource(URI.createURI("config.mydsl"))
// config.load(new StringInputStream(CONFIGURATION), emptyMap)
}
@Test
def testCheckConfiguration() {
val model = '''
package foo.bar
entity MyEntity
{
Id : Integer
Name : String
ValidFrom : Timestamp
}
'''.parse(URI.createURI("model.mydsl"), resourceSet)
model.assertError(MyDslPackage.Literals.MODEL, 'ERR-CODE-0815')
}
}
Validator:
@Inject extension ResourceExtension
@Check
def checkConfiguration(Model it) {
if (eResource.allConfigurations.nullOrEmpty) {
error("no config? no cookies!", MyDslPackage.Literals.MODEL__PACKAGE, 'ERR-CODE-0815')
}
}
What's wrong in my example? *headscratch*
Please - help ;-)
Tobias
|
|
|
Re: Problem: Validator/Generator cannot navigate through AST/Index/ResourceSet from Unit-Tests [message #1725981 is a reply to message #1725978] |
Tue, 08 March 2016 19:51 |
|
Hi,
the problem is that validators silently catch errors.
if you catch for exceptions you will get
Caused by: java.lang.NullPointerException
at org.eclipse.xtext.resource.impl.ResourceSetBasedResourceDescriptions.getResourceDescription(ResourceSetBasedResourceDescriptions.java:111)
at org.xtext.example.mydsl.extensions.ResourceExtension.getAllResourceDescriptions(ResourceExtension.java:41)
at org.xtext.example.mydsl.extensions.ResourceExtension.getAllObjectDescriptions(ResourceExtension.java:54)
at org.xtext.example.mydsl.extensions.ResourceExtension.getAllConfigurations(ResourceExtension.java:63)
at org.xtext.example.mydsl.validation.MyDslValidator.checkConfiguration(MyDslValidator.java:36)
so the problem is your impl of the resourceextensions
class ResourceExtension {
@Inject ResourceDescriptionsProvider indexProvider
@Inject IResourceServiceProvider.Registry registery
private def IContainer.Manager getManager(Resource it) {
registery.getResourceServiceProvider(URI).containerManager;
}
private def getAllResourceDescriptions(Resource it) {
val index = indexProvider.getResourceDescriptions(it)
val resourceDescByURI = index.getResourceDescription(URI)
manager.getVisibleContainers(resourceDescByURI, index).map[resourceDescriptions].flatten
}
(alternatively call iresourcedescriptions.setContext manually)
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
Powered by
FUDForum. Page generated in 0.01323 seconds