Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to test global scoping / xtext index
How to test global scoping / xtext index [message #1064736] Thu, 20 June 2013 17:32 Go to next message
Jens von Pilgrim is currently offline Jens von PilgrimFriend
Messages: 295
Registered: July 2009
Senior Member
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 Go to previous message
Jens von Pilgrim is currently offline Jens von PilgrimFriend
Messages: 295
Registered: July 2009
Senior Member
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
Previous Topic:Xtext Grammar - Exclusion of a specific Rule
Next Topic:Expression Grammar [fatal] Error
Goto Forum:
  


Current Time: Sun Nov 23 02:23:19 GMT 2014

Powered by FUDForum. Page generated in 0.02031 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software