|
|
Re: Unit test MyDslProposalProvider [message #1800400 is a reply to message #1800236] |
Wed, 26 December 2018 20:11 |
Mykola Makhin Messages: 50 Registered: July 2018 |
Member |
|
|
Ok, here's rough code I've ended up with ATM - maybe it'll be useful to others.
Some of this code can be extracted into an abstract test class in case you'll have more than one test suite class.
class MyDslCompletionTest {
static Injector injector
static ContentAssistService contentAssistService
static XtextResourceFactory resourceFactory
static Provider<XtextResourceSet> xtextResourceSetProvider
@BeforeAll
def static void init() {
injector = new MyDslIdeSetup() {
override createInjector() {
Guice.createInjector(Modules2.mixin(new MyDslRuntimeModule, new MyDslIdeTestModule))
}
}.createInjectorAndDoEMFRegistration
contentAssistService = injector.getInstance(ContentAssistService)
resourceFactory = injector.getInstance(XtextResourceFactory)
xtextResourceSetProvider = injector.getInstance(new Key<Provider<XtextResourceSet>>() {
})
}
// example test method
@Test
def void testListElementSubPropertyCompletion() {
val completions = getCompletions(
"my dsl code here function( object.property. )",
-2);
assertMatches(completions.items.map[label + " # " + detail], "property1 # description",
"property2 # description 2")
}
def assertMatches(List<String> actualItems, String... expectedItems) {
Assertions.assertEquals(if(expectedItems === null) 0 else expectedItems.length, actualItems.size)
for (var i = 0; i < expectedItems.length; i++) {
Assertions.assertEquals(expectedItems.get(i), actualItems.get(i))
}
}
def getCompletions(String text, int position) {
var positionVal = position
if (positionVal < 0) {
positionVal = text.length + positionVal
}
val result = parse(text)
Assertions.assertNotNull(result)
return contentAssistService.createCompletionList(new Document(1, text), result,
new TextDocumentPositionParams(new TextDocumentIdentifier(result.URI.toString),
new Position(0, positionVal)), null)
}
def parse(String text) {
val resourceSet = xtextResourceSetProvider.get()
val resource = resourceFactory.createResource(URI.createURI(UUID.randomUUID() + ".mydsl")) as XtextResource;
resourceSet.getResources().add(resource)
try {
resource.load(new StringInputStream(text), null)
} catch (IOException ioException) {
// Should never happen for StringInputStream
throw new RuntimeException(ioException)
}
return resource;
}
}
I have MyDslIdeTestModule class that extends Xtext generated MyDslIdeModule and customizes some dependencies which I wanted to be different in tests than in runtime.
Without this customization the code could've been even simpler.
|
|
|
Powered by
FUDForum. Page generated in 0.02947 seconds