Skip to content

Commit

Permalink
[tests] Simplify the content assist test API.
Browse files Browse the repository at this point in the history
see #580

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Feb 14, 2017
1 parent 56715aa commit 438e124
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 47 deletions.
12 changes: 12 additions & 0 deletions eclipse-sarl/plugins/io.sarl.core/src/io/sarl/core/bic.sarl
Expand Up @@ -321,6 +321,10 @@ capacity Schedules {
*
* <p>The given procedure takes one parameter: the agent associated to the task. It is name <code>it</code> by default.
*
* <p>If this function is invoked from a {@code Behavior} and there is no provided task,
* the created task will be associated to the behavior instance. It means that the task will
* be automatically cancelled when the behavior instance is unregistered from the the owning agent.
*
* @param task the task that will run the given closure. If <code>null</code>, a new task is created.
* @param delay time in milliseconds to delay the procedure execution.
* @param procedure the closure to execute.
Expand All @@ -332,6 +336,10 @@ capacity Schedules {
* Create a named task that can be retrieved and schedule later.
* If a task with the specified name already exists, this task is returned.
*
* <p>If this function is invoked from a {@code Behavior}, the created task will be associated
* to the behavior instance. It means that the task will be automatically cancelled when the behavior
* instance is unregistered from the the owning agent.
*
* @param name the name of the tash.
* @return the task instance.
*/
Expand Down Expand Up @@ -390,6 +398,10 @@ capacity Schedules {
*
* <p>The given procedure takes one parameter: the agent associated to the task. It is name <code>it</code> by default.
*
* <p>If this function is invoked from a {@code Behavior} and there is no provided task,
* the created task will be associated to the behavior instance. It means that the task will
* be automatically cancelled when the behavior instance is unregistered from the the owning agent.
*
* @param task the task to associate to the procedure. If <code>null</code> a new task is created.
* @param period the number of milliseconds between two launches of the given procedure.
* @param procedure the procedure to launch. The parameter of the procedure is the agent.
Expand Down
Expand Up @@ -93,29 +93,27 @@ public void getPath() {
public void getBundleDependencies_withEclipse() {
Iterable<String> iterable = this.container.getBundleDependencies();
assertNotNull(iterable);
assertContains(iterable,
"io.sarl.lang.core",
"javax.inject",
"org.eclipse.xtext.xbase.lib",
"com.google.guava",
"org.eclipse.osgi",
"org.eclipse.osgi.compatibility.state",
//
"io.sarl.util",
//
"io.sarl.core",
//
"aopalliance",
"com.google.inject",
"com.google.inject.multibindings");
assertPartlyContains(iterable,
"aopalliance",
"com.google.guava",
"com.google.inject",
"com.google.inject.multibindings",
"io.sarl.core",
"io.sarl.lang.core",
"io.sarl.util",
"javax.inject",
"org.eclipse.osgi",
"org.eclipse.osgi.compatibility.state",
"org.eclipse.xtext.xbase.lib"
);
}

@Test
@TestScope(eclipse = false, tycho = true)
public void getBundleDependencies_withTycho() {
Iterable<String> iterable = this.container.getBundleDependencies();
assertNotNull(iterable);
assertContains(iterable,
assertPartlyContains(iterable,
"io.sarl.lang.core",
"javax.inject",
"org.eclipse.xtext.xbase.lib",
Expand Down
Expand Up @@ -143,6 +143,7 @@ public void setUp() throws Exception {
this.jarFile = FileLocator.toFileURL(this.jarFile);
URI uri = this.jarFile.toURI();
this.path = URIUtil.toPath(uri);
assert this.path != null;
this.sre.setJarFile(this.path);
}

Expand Down Expand Up @@ -232,8 +233,8 @@ public void getAsXML() throws Exception {
trans.transform(source, xmlStream);
String content = new String(baos.toByteArray());
String[] expected = new String[] { "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>",
"<root libraryPath=\"" + this.path.toOSString() + "\" standalone=\"true\">",
"<libraryLocation packageRootPath=\"\" sourcePath=\"\" systemLibraryPath=\"" + this.path.toOSString()
"<root libraryPath=\"" + this.path.toPortableString() + "\" standalone=\"true\">",
"<libraryLocation packageRootPath=\"\" sourcePath=\"\" systemLibraryPath=\"" + this.path.toPortableString()
+ "\"/>",
"<libraryLocation packageRootPath=\"\" sourcePath=\"\" systemLibraryPath=\"a.jar\"/>",
"<libraryLocation packageRootPath=\"\" sourcePath=\"\" systemLibraryPath=\"b.jar\"/>",
Expand All @@ -250,9 +251,9 @@ public void getAsXML() throws Exception {
@Test
public void setFromXML() throws Exception {
String[] expected = new String[] { "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>",
"<SRE name=\"Hello\" mainClass=\"io.sarl.Boot\" libraryPath=\"" + this.path.toOSString()
"<SRE name=\"Hello\" mainClass=\"io.sarl.Boot\" libraryPath=\"" + this.path.toPortableString()
+ "\" standalone=\"true\">",
"<libraryLocation packageRootPath=\"\" sourcePath=\"\" systemLibraryPath=\"" + this.path.toOSString()
"<libraryLocation packageRootPath=\"\" sourcePath=\"\" systemLibraryPath=\"" + this.path.toPortableString()
+ "\"/>",
"<libraryLocation packageRootPath=\"\" sourcePath=\"\" systemLibraryPath=\"x.jar\"/>",
"<libraryLocation packageRootPath=\"\" sourcePath=\"\" systemLibraryPath=\"y.jar\"/>",
Expand Down Expand Up @@ -460,7 +461,7 @@ public void getAsXML() throws Exception {
trans.transform(source, xmlStream);
String content = new String(baos.toByteArray());
String[] expected = new String[] { "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>",
"<root libraryPath=\"" + this.path.toOSString() + "\" standalone=\"false\"/>", };
"<root libraryPath=\"" + this.path.toPortableString() + "\" standalone=\"false\"/>", };
StringBuilder b = new StringBuilder();
for (String s : expected) {
b.append(s);
Expand All @@ -473,9 +474,9 @@ public void getAsXML() throws Exception {
@Test
public void setFromXML() throws Exception {
String[] expected = new String[] { "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>",
"<SRE name=\"Hello\" mainClass=\"io.sarl.Boot\" libraryPath=\"" + this.path.toOSString()
"<SRE name=\"Hello\" mainClass=\"io.sarl.Boot\" libraryPath=\"" + this.path.toPortableString()
+ "\" standalone=\"true\">",
"<libraryLocation packageRootPath=\"\" sourcePath=\"\" systemLibraryPath=\"" + this.path.toOSString()
"<libraryLocation packageRootPath=\"\" sourcePath=\"\" systemLibraryPath=\"" + this.path.toPortableString()
+ "\"/>",
"<libraryLocation packageRootPath=\"\" sourcePath=\"\" systemLibraryPath=\"x.jar\"/>",
"<libraryLocation packageRootPath=\"\" sourcePath=\"\" systemLibraryPath=\"y.jar\"/>",
Expand Down
Expand Up @@ -139,26 +139,27 @@ public void resolveBundleDependenciesBundleBundleURLMappingsStringArray_noRootDe
assertNotNull(dependencies);
assertEquals("io.sarl.lang.core", dependencies.getBundleSymbolicName());
assertOsgiVersionEquals(Version.parseVersion(SARLVersion.SARL_RELEASE_VERSION_OSGI), dependencies.getBundleVersion());
assertContains(dependencies.getDirectSymbolicNames(),
assertPartlyContains(dependencies.getDirectSymbolicNames(),
"io.sarl.lang.core",
"javax.inject",
"org.eclipse.xtext.xbase.lib");
assertContains(dependencies.getTransitiveSymbolicNames(false),
assertPartlyContains(dependencies.getTransitiveSymbolicNames(false),
"io.sarl.lang.core",
"javax.inject",
"org.eclipse.xtext.xbase.lib",
"com.google.guava",
"org.eclipse.osgi");
if (isEclipseRuntimeEnvironment()) {
assertContains(dependencies.getTransitiveSymbolicNames(true),
assertPartlyContains(dependencies.getTransitiveSymbolicNames(true),
"io.sarl.lang.core",
"javax.inject",
"org.eclipse.xtext.xbase.lib",
"javax.inject",
"com.google.guava",
"org.eclipse.osgi",
"org.eclipse.osgi.compatibility.state");
"org.eclipse.osgi.compatibility.state"
);
} else {
assertContains(dependencies.getTransitiveSymbolicNames(true),
assertPartlyContains(dependencies.getTransitiveSymbolicNames(true),
"io.sarl.lang.core",
"javax.inject",
"org.eclipse.xtext.xbase.lib",
Expand All @@ -177,23 +178,24 @@ public void resolveBundleDependenciesBundleBundleURLMappingsStringArray_rootDepe
assertNotNull(dependencies);
assertEquals("io.sarl.lang.core", dependencies.getBundleSymbolicName());
assertOsgiVersionEquals(Version.parseVersion(SARLVersion.SARL_RELEASE_VERSION_OSGI), dependencies.getBundleVersion());
assertContains(dependencies.getDirectSymbolicNames(),
assertPartlyContains(dependencies.getDirectSymbolicNames(),
"io.sarl.lang.core",
"org.eclipse.xtext.xbase.lib");
assertContains(dependencies.getTransitiveSymbolicNames(false),
assertPartlyContains(dependencies.getTransitiveSymbolicNames(false),
"io.sarl.lang.core",
"org.eclipse.xtext.xbase.lib",
"com.google.guava",
"org.eclipse.osgi");
if (isEclipseRuntimeEnvironment()) {
assertContains(dependencies.getTransitiveSymbolicNames(true),
assertPartlyContains(dependencies.getTransitiveSymbolicNames(true),
"io.sarl.lang.core",
"org.eclipse.xtext.xbase.lib",
"com.google.guava",
"org.eclipse.osgi",
"org.eclipse.osgi.compatibility.state");
"org.eclipse.osgi.compatibility.state"
);
} else {
assertContains(dependencies.getTransitiveSymbolicNames(true),
assertPartlyContains(dependencies.getTransitiveSymbolicNames(true),
"io.sarl.lang.core",
"org.eclipse.xtext.xbase.lib",
"com.google.guava",
Expand Down
Expand Up @@ -112,7 +112,13 @@ public void newAgentSerialization() throws Exception {
content = baos.toString();
}

assertEquals("package io.sarl.eclipse.tests.wizards\n/* Foo Agent\n */\nagent FooAgent {\n}\n", content);
assertEquals(multilineString(
"package io.sarl.eclipse.tests.wizards",
"/* Foo Agent",
" */",
"agent FooAgent {",
"}",
""), content);
}

}
Expand Up @@ -95,7 +95,7 @@ public final XtextResource getResourceFor(InputStream stream) {
String line = breader.readLine();
while (line != null) {
content.append(line);
content.append("\n");
content.append(getLineSeparator());
line = breader.readLine();
}
}
Expand Down Expand Up @@ -135,9 +135,7 @@ protected String getSuffix() {
* @throws Exception if the builder cannot be created.
*/
protected final ContentAssistProcessorTestBuilder newBuilder() throws Exception {
final ContentAssistProcessorTestBuilder.Factory factory = getInjector().getInstance(
ContentAssistProcessorTestBuilder.Factory.class);
ContentAssistProcessorTestBuilder builder = factory.create(this);
ContentAssistProcessorTestBuilder builder = new ContentAssistProcessorTestBuilder(getInjectedInjector(), this);
String prefix = getPrefix();
if (prefix.length() > 0) {
builder = builder.appendNl(prefix);
Expand Down Expand Up @@ -245,7 +243,7 @@ private static String toString(Object[] array) {
if (obj != null) {
buf.append(obj);
}
buf.append("\n");
buf.append(getLineSeparator());
}
}
return buf.toString();
Expand Down
Expand Up @@ -115,7 +115,8 @@ protected QuickFixAsserts getQuickFixAsserts(
while (issueIterator.hasNext()) {
Issue nextIssue = issueIterator.next();
if (issueLabels.length() > 0) {
issueLabels.append(",\n"); //$NON-NLS-1$
issueLabels.append(","); //$NON-NLS-1$
issueLabels.append(getLineSeparator());
}
issueLabels.append(nextIssue.getCode());
issueLabels.append(" - \""); //$NON-NLS-1$
Expand All @@ -127,7 +128,8 @@ protected QuickFixAsserts getQuickFixAsserts(
}
}
if (issueLabels.length() > 0) {
issueLabels.append(".\n"); //$NON-NLS-1$
issueLabels.append("."); //$NON-NLS-1$
issueLabels.append(getLineSeparator());
}
if (issue == null) {
fail("The issue '" + issueCode //$NON-NLS-1$
Expand Down Expand Up @@ -283,6 +285,13 @@ public int getResolutionCount() {
return this.resolutions.size();
}

private static String unifiesNewLineCharacters(String content) {
String result = Strings.emptyIfNull(content);
result = result.replaceAll("\r\n", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
result = result.replaceAll("\r", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
return result.trim();
}

/** Test the existence of a valid quick fix.
*
* @param expectedLabel - the expected label for the quick fix.
Expand All @@ -294,7 +303,7 @@ public String assertQuickFix(
String... expectedResolutions) {
try {
assert (expectedResolutions.length > 0);

IssueResolution resolution = findResolution(expectedLabel, true, true);

assertEquals(expectedLabel, resolution.getLabel());
Expand All @@ -307,7 +316,7 @@ public String assertQuickFix(
String oldContent = Objects.toString(document.toString());

final IModification modification = resolution.getModification();

modification.apply(modificationContext);

String newContent = document.toString();
Expand All @@ -322,12 +331,14 @@ public String assertQuickFix(
baos.flush();
newContent = baos.toString();
}
newContent = Objects.toString(newContent).trim();
newContent = Objects.toString(newContent);
}

newContent = unifiesNewLineCharacters(newContent);

final Set<String> expected = new TreeSet<>();
for (final String expectedResolution : expectedResolutions) {
final String ex = Strings.notNull(expectedResolution).trim();
final String ex = unifiesNewLineCharacters(expectedResolution);
expected.add(ex);
}

Expand Down Expand Up @@ -481,7 +492,7 @@ public String toString() {
private static class TestLineTracker implements ILineTracker {

/** The predefined delimiters of this tracker */
private final static String[] STR_DELIMITERS = { "\r", "\n", "\r\n" }; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
private final static String[] STR_DELIMITERS = { System.getProperty("line.separator"), "\r", "\n" }; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$

private final StringBuilder content = new StringBuilder();
private String[] lines = null;
Expand Down

0 comments on commit 438e124

Please sign in to comment.