Skip to content

Commit

Permalink
document that this is the "old" way
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed May 16, 2024
1 parent d10ede9 commit f6a299a
Showing 1 changed file with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.eclipse.xtext.scoping.IScopeProvider;
import org.eclipse.xtext.serializer.ISerializer;
import org.eclipse.xtext.testing.GlobalRegistries.GlobalStateMemento;
import org.eclipse.xtext.testing.extensions.InjectionExtension;
import org.eclipse.xtext.testing.serializer.SerializerTestHelper;
import org.eclipse.xtext.util.CancelIndicator;
import org.eclipse.xtext.util.LazyStringInputStream;
Expand All @@ -50,6 +51,8 @@
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;

import com.google.common.annotations.Beta;
import com.google.common.base.Joiner;
Expand All @@ -63,6 +66,13 @@
import com.google.inject.name.Names;

/**
* Note that this represents the "old" way of implementing unit tests.
*
* New tests should be implemented using the {@link InjectWith} annotation, and
* the generated {@link IInjectorProvider} and {@link RunWith} with
* {@link XtextRunner}, for JUnit 4, and {@link ExtendWith} with
* {@link InjectionExtension}, for JUnit 5.
*
* @author Sven Efftinge - Initial contribution and API
* @since 2.35
*/
Expand All @@ -73,7 +83,7 @@ public abstract class AbstractXtextTests extends Assert {
private boolean canCreateInjector;
private boolean isSerializerTestDisabled = false;
private GlobalStateMemento globalStateMemento;

static {
GlobalRegistries.initializeDefaults();
}
Expand Down Expand Up @@ -114,11 +124,11 @@ public void with(ISetup setup) throws Exception {
assertTrue("super.setUp() has to be called before any injector is instantiated", canCreateInjector);
setInjector(setup.createInjectorAndDoEMFRegistration());
}

protected void setInjector(Injector injector) {
this.injector = injector;
}

final public Injector getInjector() {
if (injector==null)
throw new IllegalStateException("No injector set. Did you forget to call something like 'with(new YourStadaloneSetup())'?");
Expand All @@ -136,7 +146,7 @@ public<T> T get(Key<T> key) {
injector = Guice.createInjector();
return injector.getInstance(key);
}

public void injectMembers(Object object) {
if (injector == null)
injector = Guice.createInjector();
Expand Down Expand Up @@ -170,15 +180,15 @@ protected IValueConverterService getValueConverterService() {
protected ISerializer getSerializer() {
return getInjector().getInstance(ISerializer.class);
}

protected INodeModelFormatter getNodeModelFormatter() {
return getInjector().getInstance(INodeModelFormatter.class);
}

protected IScopeProvider getScopeProvider() {
return getInjector().getInstance(IScopeProvider.class);
}

protected InvariantChecker getInvariantChecker(){
return getInjector().getInstance(InvariantChecker.class);
}
Expand All @@ -189,33 +199,32 @@ protected InvariantChecker getInvariantChecker(){
protected InputStream getAsStream(String model) {
return getAsStream(model, Charset.defaultCharset());
}

/**
* Gets the string as input stream with specified encoding.
* @since 2.16
*/
protected InputStream getAsStream(String model, Charset encoding) {
return new LazyStringInputStream(model, encoding.name());
}

// parse methods

public EObject getModel(String model) throws Exception {
return getModel(getAsStream(model));
}

public final EObject getModel(InputStream model) throws Exception {
XtextResource resource = getResource(model);
return getModel(resource);
}

public static final int EXPECT_ERRORS = -2;
public static final int UNKNOWN_EXPECTATION = Integer.MIN_VALUE;

public final EObject getModelAndExpect(String model, int errors) throws Exception {
return getModelAndExpect(getAsStream(model), errors);
}

public final EObject getModelAndExpect(InputStream model, int errors) throws Exception {
XtextResource resource = getResourceAndExpect(model, errors);
return getModel(resource);
Expand All @@ -228,22 +237,22 @@ protected EObject getModel(XtextResource resource) {
protected final XtextResource getResourceFromString(String model) throws Exception {
return getResource(getAsStream(model));
}

protected final XtextResource getResourceFromStringAndExpect(String model, int errors) throws Exception {
return getResourceAndExpect(getAsStream(model), errors);
}

public final XtextResource getResource(InputStream in) throws Exception {
return getResource(in, URI.createURI("mytestmodel."+getCurrentFileExtension()));
}

protected String getCurrentFileExtension() {
String instance = getInjector().getInstance(Key.get(String.class,Names.named(Constants.FILE_EXTENSIONS)));
if (instance.indexOf(',')==-1)
return instance;
return instance.split(",")[0];
}

public final XtextResource getResourceFor(InputStream stream) {
try {
return getResourceAndExpect(stream, AbstractXtextTests.UNKNOWN_EXPECTATION);
Expand All @@ -253,26 +262,23 @@ public final XtextResource getResourceFor(InputStream stream) {
throw new RuntimeException(e);
}
}

public final XtextResource getResourceAndExpect(InputStream in, int errors) throws Exception {
return getResourceAndExpect(in, getTestModelURI(), errors);
}

/**
* @since 2.8
*/
protected URI getTestModelURI() {
return URI.createURI("mytestmodel."+getCurrentFileExtension());
}

public final XtextResource getResource(InputStream in, URI uri) throws Exception {
return getResourceAndExpect(in, uri, 0);
}

public final XtextResource getResource(String contents, String uri) throws Exception {
return getResource(getAsStream(contents), URI.createURI(uri));
}

public final XtextResource getResourceAndExpect(InputStream in, URI uri, int expectedErrors) throws Exception {
XtextResource resource = doGetResource(in, uri);
checkNodeModel(resource);
Expand Down Expand Up @@ -307,11 +313,11 @@ protected void checkNodeModel(XtextResource resource) {
protected boolean shouldTestSerializer(XtextResource resource) {
return !isSerializerTestDisabled && !"org.eclipse.xtext.Xtext".equals(resource.getLanguageName());
}

protected void disableSerializerTest() {
isSerializerTestDisabled = true;
}

protected Object getClasspathURIContext() {
return getClass();
}
Expand All @@ -334,7 +340,7 @@ protected final ICompositeNode getRootNode(InputStream model) throws Exception {
XtextResource resource = getResource(model);
return getRootNode(resource);
}

protected final ICompositeNode getRootNodeAndExpect(InputStream model, int errors) throws Exception {
XtextResource resource = getResourceAndExpect(model, errors);
return getRootNode(resource);
Expand All @@ -347,19 +353,19 @@ protected final ICompositeNode getRootNode(XtextResource resource) {
protected final IParseResult getParseResult(String model) throws Exception {
return getResourceFromString(model).getParseResult();
}

protected final IParseResult getParseResultAndExpect(String model, int errors) throws Exception {
return getResourceFromStringAndExpect(model, errors).getParseResult();
}

protected final ICompositeNode getRootNode(String model) throws Exception {
return getRootNode(getAsStream(model));
}

protected final ICompositeNode getRootNodeAndExpect(String model, int errors) throws Exception {
return getRootNodeAndExpect(getAsStream(model), errors);
}

protected String readFileIntoString(String filePath) throws IOException {
ClassLoader classLoader = getClass().getClassLoader();
URL url = classLoader.getResource(filePath);
Expand Down

0 comments on commit f6a299a

Please sign in to comment.