Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Unit test MyDslProposalProvider(Unit test proposal provider without UI project (Eclipse plugin))
Unit test MyDslProposalProvider [message #1800235] Fri, 21 December 2018 15:50 Go to next message
Mykola Makhin is currently offline Mykola MakhinFriend
Messages: 50
Registered: July 2018
Member
Hi

I have a DSL I use with web integration and thus I don't have Eclipse plugin made for it.

I have my custom content assist (proposal provider) in IDE project (do not let the name fool you - it does not have Eclipse plugin or anything desktop related, this is for generic content assist that works with LanguageServerProtocol and/or XtextServlet web integration).

Question is - how can I implement unit tests for it?
I know about AbstractContentAssistTest for Eclipse plugin project, but again, that's not what I'm looking for.
Re: Unit test MyDslProposalProvider [message #1800236 is a reply to message #1800235] Fri, 21 December 2018 15:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
there is no testing api yet.
you may let you inspire e.g. org.eclipse.xtext.ide.server.contentassist.ContentAssistService.createProposals(String, TextRegion, int, XtextResource, IIdeContentProposalAcceptor)


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Unit test MyDslProposalProvider [message #1800400 is a reply to message #1800236] Wed, 26 December 2018 20:11 Go to previous message
Mykola Makhin is currently offline Mykola MakhinFriend
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.
Previous Topic:multiple alternatives error during the generation of the artifacts
Next Topic:Error in building shadowJar from project with multiple xtext files (Cross-reference)
Goto Forum:
  


Current Time: Thu Apr 25 01:42:06 GMT 2024

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

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

Back to the top