Skip to content

Instantly share code, notes, and snippets.

@szarnekow
Last active August 29, 2015 14:07
Show Gist options
  • Save szarnekow/ad2369160bee7b42e619 to your computer and use it in GitHub Desktop.
Save szarnekow/ad2369160bee7b42e619 to your computer and use it in GitHub Desktop.
Small example to show how to test multiple DSLs with Xtext
class MultiLangInjectorProvider extends MyDslInjectorProvider {
override protected internalCreateInjector() {
// trigger injector creation of other language
new SecondDslInjectorProvider().getInjector
return super.internalCreateInjector()
}
}
@Test def void successfulLanguageTest() {
val resourceSet = resourceSetProvider.get
// create a resource for language 'MyDsl'
val firstLang = resourceSet.createResource(URI.createURI("sample.mydsl"))
// parse some contents
firstLang.load(new StringInputStream(".."), emptyMap)
// and read the root instance
val root = firstLang.contents.head as MyDslRoot
// ensure it's not null
assertNotNull(root)
// and valid
validationTester.assertNoIssues(root)
// use the parse helper to read the model under test
// into the same resource set
val testMe = parseHelper.parse("..", resourceSet)
// it's not null, too
assertNotNull(testMe)
// and also valid
validationTester.assertNoIssues(testMe)
// more assertions below
..
}
@RunWith(XtextRunner)
@InjectWith(MyDSLInjectorProvider)
class MyTest {
// Helper to read models
@Inject ParseHelper<MyModel> parseHelper
// Helper to test all validation rules and ensure resolved links
@Inject ValidationTestHelper validationTester
@Test def void myTest() {
val model = parseHelper.parse('Hello World!')
validationTester.assertNoIssues(model)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment