|
Re: JUnit tests for content assist needing classpath context [message #1345716 is a reply to message #1343688] |
Sat, 10 May 2014 08:48 |
|
On 09/05/2014 14:09, Stephan Herrmann wrote:
> I'm happily using some infrastructure from o.e.x.junit4, delighted to
> find advanced stuff like AbstractContentAssistProcessorTest.
>
> This works nicely for simple examples, but some of my content assists
> need to retrieve information from secondary models on the class path.
>
> I couldn't find a way to configure these tests with two or more
> resources so that one could resolve references to the other.
>
> Am I looking in the wrong place? Are there other test classes better
> suitable for testing multi-model situations?
Hi
you may want to look in that base class and see how things are setup; in
particular, it uses org.eclipse.xtext.junit4.ui.util.JavaProjectSetupUtil.*
if you dig into org.eclipse.xtext.junit4 package you see that there are
many utility functions for setting up a project programmatically,
creating files, etc.; you may have to perform additional setup steps
yourself.
cheers
Lorenzo
--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book
HOME: http://www.lorenzobettini.it
TDD Book: https://leanpub.com/tdd-buildautomation-ci
Xtext Book: https://www.packtpub.com/application-development/implementing-domain-specific-languages-xtext-and-xtend-second-edition
|
|
|
Re: JUnit tests for content assist needing classpath context [message #1385171 is a reply to message #1345716] |
Wed, 04 June 2014 18:05 |
Stephan Herrmann Messages: 1853 Registered: July 2009 |
Senior Member |
|
|
Hi Lorenzo,
thanks for your encouragement (I was "detained" by https://bugs.eclipse.org/430302 , but I seem to be back in the game ...).
Unfortunately, I can't see the connection from AbstractContentAssistProcessorTest to JavaProjectSetupUtil, which you mentioned, and all attempts to combine them two let me think that those are not meant to be working together. Even single-resource tests that have been working with AbstractContentAssistProcessorTest completely break down when I try to run them with an IJavaProject.
Hence my question: are there any tests out there that successfully use AbstractContentAssistProcessorTest with an IJavaProject?
FYI, these are some of the hooks that I override so far:
- a @Before method to call createJavaProject and addNature
- override get(Class<T>) to create a XtextResourceSet for the given IJavaProject
- write my own createFile() method (for secondary files), because everything that's routed through doGetResource() doesn't really create the resource in the file system, which means JDT cannot see it.
- override doGetResource to avoid calling rs.setClasspathURIContext() (we already have the IJavaProject as the context, using getClass() is wrong in this situation).
The "best" I could achieve with all this is: no completion proposals.
In the debugger I see that org.eclipse.xtext.ui.editor.contentassist.antlr.ParserBasedContentAssistContextFactory.StatefulFactory.doCreateContexts(int) fails to create any contextBuilders. I have a valid root model element, but it doesn't seem to have and followElements. Without an IJavaProject all was well for this test.
anyone have any successful examples?
thanks,
Stephan
|
|
|
|
Re: JUnit tests for content assist needing classpath context [message #1385178 is a reply to message #1385175] |
Wed, 04 June 2014 19:07 |
|
btw the following works for me:
Model:
elements+=Element*;
Element:
'element' name=ID ('extends' extends=[Element])?;
package org.xtext.example.mydsl.tests;
import java.io.InputStream;
import javax.inject.Inject;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.xtext.ISetup;
import org.eclipse.xtext.junit4.InjectWith;
import org.eclipse.xtext.junit4.XtextRunner;
import org.eclipse.xtext.junit4.ui.AbstractContentAssistProcessorTest;
import org.eclipse.xtext.junit4.ui.util.IResourcesSetupUtil;
import org.eclipse.xtext.junit4.ui.util.JavaProjectSetupUtil;
import org.eclipse.xtext.linking.lazy.LazyLinkingResource;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.ui.XtextProjectHelper;
import org.eclipse.xtext.util.CancelIndicator;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.xtext.example.mydsl.MyDslUiInjectorProvider;
import com.google.inject.Injector;
@SuppressWarnings("restriction")
@RunWith(XtextRunner.class)
@InjectWith(MyDslUiInjectorProvider.class)
public class MyDslContentAssistProcessorTest extends AbstractContentAssistProcessorTest {
@Inject
private Injector injector;
private IJavaProject project;
@Override
public void setUp() throws Exception {
super.setUp();
IJavaProject lib = JavaProjectSetupUtil.createJavaProject("lib");
JavaProjectSetupUtil.addSourceFolder(lib, "src");
IResourcesSetupUtil.addNature(lib.getProject(), XtextProjectHelper.NATURE_ID);
IResourcesSetupUtil.addBuilder(lib.getProject(), XtextProjectHelper.BUILDER_ID);
IResourcesSetupUtil.createFile("lib/src/dummy.mydsl", "element D");
project = JavaProjectSetupUtil.createJavaProject("dummy");
JavaProjectSetupUtil.addSourceFolder(project, "src");
IResourcesSetupUtil.addNature(project.getProject(), XtextProjectHelper.NATURE_ID);
IResourcesSetupUtil.addBuilder(project.getProject(), XtextProjectHelper.BUILDER_ID);
IResourcesSetupUtil.createFile("dummy/src/dummy.mydsl", "element C");
JavaProjectSetupUtil.addProjectReference(project, lib);
IResourcesSetupUtil.waitForAutoBuild();
}
@Override
protected ISetup doGetSetup() {
return new ISetup() {
public Injector createInjectorAndDoEMFRegistration() {
return injector;
}
};
}
@Test
public void testSomething() throws Exception {
newBuilder().append("element A element B extends ").assertText("A","B","C","D");
}
protected XtextResource doGetResource(InputStream in, URI uri) throws Exception {
uri = URI.createURI("platform:/resource/dummy/test.mydsl");
XtextResourceSet rs = get(XtextResourceSet.class);
rs.setClasspathURIContext(project);
XtextResource resource = (XtextResource) getResourceFactory().createResource(uri);
rs.getResources().add(resource);
resource.load(in, null);
if (resource instanceof LazyLinkingResource) {
((LazyLinkingResource) resource).resolveLazyCrossReferences(CancelIndicator.NullImpl);
} else {
EcoreUtil.resolveAll(resource);
}
return resource;
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02901 seconds