How to test global scoping / xtext index [message #1064736] |
Thu, 20 June 2013 17:32 |
|
Hi,
I have implemented an Xtext based language with custom global scoping,
e.g., with a custom implementation of IResourceDescription.Manager etc.
While I can easily test local scoping using @RunWith(XtextRunner) and
things like the ParseHelper, I'm not sure how to best test global
scoping related issues, e.g., which things are exported. Since this
seems a common task, I was wondering if there is something like a
pattern for that or/and some helper classes available in Xtext already?
Regards,
Jens
|
|
|
Re: How to test global scoping / xtext index [message #1064832 is a reply to message #1064736] |
Fri, 21 June 2013 09:47 |
|
Hi,
well, I can answer that myself:
First, let's test if everything is correctly exported:
@InjectWith(MyLangInjectorProvider)
@RunWith(XtextRunner)
class MyLangScopingTest {
@Inject
extension ParseHelper<Program>
@javax.inject.Inject
ResourceDescriptionsProvider resourceDescriptionsProvider;
@Test
def void testExport() {
val program =
'''
my program with some exported elements
'''.parse
// syntax ok?
assertTrue(program.eResource.errors.empty)
val IResourceDescriptions resDescr =
resourceDescriptionsProvider
.getResourceDescriptions(program.eResource());
// do something, e.g.
println(resDescr.allResourceDescriptions.toList);
println(
resDescr.allResourceDescriptions.head
.exportedObjects.toList);
}
}
Note that the resource's URI is a synthetic one similar to
uri=file:/path/to/tes/project/__synthetic0.mylang
Also see https://bugs.eclipse.org/bugs/show_bug.cgi?id=381335
However, it is possible to explicitly set the URI in the parse method,
as shown in the next setup.
This first example only checked the exported elements. Now, let's test a
cross link between different files (in a new test method, test class
is the one of the first example):
@Test
def void testImportExport() {
// use one resource set
val rs = new XtextResourceSet();
val supplier =
'''
program exporting something
'''.parse(URI.createURI("Supplier.mylang"), rs)
val someExportedElement =
supplier.eAllContents.filter(ExportElementType).head;
val client =
'''
// program importing the Supplier, e.g
import Supplier;
// and accessing the exported element, e.g.
Supplier.element;
'''.parse(URI.createURI("Client.mylang"), rs)
// trigger linking
EcoreUtil.resolveAll(rs)
val reference = client.eAllContents.filter(ReferenceType).head;
assertEquals(someExportedElement, reference.element);
}
Of course, you have to ensure that your global scoping (including naming
and URI resolving) can work with the test setup (e.g., neither a
classpath nor an Eclipse project is available in the sample setup).
Regards,
Jens
|
|
|
Powered by
FUDForum. Page generated in 0.03378 seconds