Make completion test framework be configurable for writing multi-platform tests
This commit is contained in:
@@ -25,6 +25,7 @@ import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public final class InTextDirectivesUtils {
|
||||
@@ -43,15 +44,15 @@ public final class InTextDirectivesUtils {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String[] findArrayWithPrefix(String fileText, String prefix) {
|
||||
return ArrayUtil.toStringArray(findListWithPrefix(fileText, prefix));
|
||||
public static String[] findArrayWithPrefix(String fileText, String... prefixes) {
|
||||
return ArrayUtil.toStringArray(findListWithPrefix(fileText, prefixes));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static List<String> findListWithPrefix(String fileText, String prefix) {
|
||||
public static List<String> findListWithPrefix(String fileText, String... prefixes) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
|
||||
for (String line : findLinesWithPrefixRemoved(fileText, prefix)) {
|
||||
for (String line : findLinesWithPrefixRemoved(fileText, prefixes)) {
|
||||
String[] variants = line.split(",");
|
||||
|
||||
for (String variant : variants) {
|
||||
@@ -63,29 +64,36 @@ public final class InTextDirectivesUtils {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String findStringWithPrefix(String fileText, String prefix) {
|
||||
List<String> strings = findListWithPrefix(fileText, prefix);
|
||||
public static String findStringWithPrefix(String fileText, String... prefixes) {
|
||||
List<String> strings = findListWithPrefix(fileText, prefixes);
|
||||
if (strings.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
assert strings.size() == 1 : "There is more than one string with given prefix " + prefix + ". Use findListWithPrefix() instead.";
|
||||
assert strings.size() == 1 : "There is more than one string with given prefixes " +
|
||||
Arrays.toString(prefixes) + ". Use findListWithPrefix() instead.";
|
||||
return strings.get(0);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static List<String> findLinesWithPrefixRemoved(String fileText, String prefix) {
|
||||
public static List<String> findLinesWithPrefixRemoved(String fileText, String... prefixes) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
|
||||
for (String line : fileNonEmptyLines(fileText)) {
|
||||
if (line.startsWith(prefix)) {
|
||||
result.add(line.substring(prefix.length()).trim());
|
||||
for (String prefix : prefixes) {
|
||||
if (line.startsWith(prefix)) {
|
||||
String noPrefixLine = line.substring(prefix.length());
|
||||
|
||||
if (noPrefixLine.isEmpty() || Character.isWhitespace(noPrefixLine.charAt(0))) {
|
||||
result.add(noPrefixLine.trim());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
private static List<String> fileNonEmptyLines(String fileText) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
|
||||
@@ -38,6 +38,14 @@ import java.util.regex.Pattern;
|
||||
* should be asserted during test execution.
|
||||
*/
|
||||
public class ExpectedCompletionUtils {
|
||||
private ExpectedCompletionUtils() {
|
||||
}
|
||||
|
||||
enum Platform {
|
||||
JAVA,
|
||||
JS,
|
||||
ALL
|
||||
}
|
||||
|
||||
public static class CompletionProposal {
|
||||
public static final Pattern PATTERN = Pattern.compile("([^~@]*)(@([^~]*))?(~(.*))?");
|
||||
@@ -90,43 +98,61 @@ public class ExpectedCompletionUtils {
|
||||
}
|
||||
|
||||
public static final String EXIST_LINE_PREFIX = "// EXIST:";
|
||||
public static final String EXIST_JS_LINE_PREFIX = "// EXIST_JS:";
|
||||
public static final String EXIST_JAVA_LINE_PREFIX = "// EXIST_JAVA:";
|
||||
|
||||
public static final String ABSENT_LINE_PREFIX = "// ABSENT:";
|
||||
public static final String ABSENT_JS_LINE_PREFIX = "// ABSENT_JS:";
|
||||
public static final String ABSENT_JAVA_LINE_PREFIX = "// ABSENT_JAVA:";
|
||||
|
||||
public static final String NUMBER_LINE_PREFIX = "// NUMBER:";
|
||||
public static final String NUMBER_JS_LINE_PREFIX = "// NUMBER_JS:";
|
||||
public static final String NUMBER_JAVA_LINE_PREFIX = "// NUMBER_JAVA:";
|
||||
|
||||
public static final String EXECUTION_TIME_PREFIX = "// TIME:";
|
||||
public static final String WITH_ORDER_PREFIX = "// WITH_ORDER:";
|
||||
|
||||
private final String existLinePrefix;
|
||||
private final String absentLinePrefix;
|
||||
private final String numberLinePrefix;
|
||||
private final String executionTimePrefix;
|
||||
private final String withOrderPrefix;
|
||||
@NotNull
|
||||
public static CompletionProposal[] itemsShouldExist(String fileText, Platform platform) {
|
||||
switch (platform) {
|
||||
case ALL:
|
||||
return processProposalAssertions(fileText, EXIST_LINE_PREFIX, EXIST_JAVA_LINE_PREFIX, EXIST_JS_LINE_PREFIX);
|
||||
case JAVA:
|
||||
return processProposalAssertions(fileText, EXIST_LINE_PREFIX, EXIST_JAVA_LINE_PREFIX);
|
||||
case JS:
|
||||
return processProposalAssertions(fileText, EXIST_LINE_PREFIX, EXIST_JS_LINE_PREFIX);
|
||||
}
|
||||
|
||||
public ExpectedCompletionUtils() {
|
||||
this(EXIST_LINE_PREFIX, ABSENT_LINE_PREFIX, NUMBER_LINE_PREFIX, EXECUTION_TIME_PREFIX, WITH_ORDER_PREFIX);
|
||||
}
|
||||
|
||||
public ExpectedCompletionUtils(String existLinePrefix, String absentLinePrefix,
|
||||
String numberLinePrefix, String executionTimePrefix, String withOrderPrefix) {
|
||||
this.existLinePrefix = existLinePrefix;
|
||||
this.absentLinePrefix = absentLinePrefix;
|
||||
this.numberLinePrefix = numberLinePrefix;
|
||||
this.executionTimePrefix = executionTimePrefix;
|
||||
this.withOrderPrefix = withOrderPrefix;
|
||||
throw new IllegalArgumentException("platform");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CompletionProposal[] itemsShouldExist(String fileText) {
|
||||
return processProposalAssertions(existLinePrefix, fileText);
|
||||
public static CompletionProposal[] itemsShouldAbsent(String fileText, Platform platform) {
|
||||
switch (platform) {
|
||||
case ALL:
|
||||
return processProposalAssertions(fileText, ABSENT_LINE_PREFIX, EXIST_JAVA_LINE_PREFIX, EXIST_JS_LINE_PREFIX);
|
||||
case JAVA:
|
||||
return processProposalAssertions(fileText, ABSENT_LINE_PREFIX, ABSENT_JAVA_LINE_PREFIX);
|
||||
case JS:
|
||||
return processProposalAssertions(fileText, ABSENT_LINE_PREFIX, ABSENT_JS_LINE_PREFIX);
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("platform");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CompletionProposal[] itemsShouldAbsent(String fileText) {
|
||||
return processProposalAssertions(absentLinePrefix, fileText);
|
||||
public static CompletionProposal[] itemsShouldExist(String fileText) {
|
||||
return itemsShouldExist(fileText, Platform.ALL);
|
||||
}
|
||||
|
||||
public static CompletionProposal[] processProposalAssertions(String prefix, String fileText) {
|
||||
@NotNull
|
||||
public static CompletionProposal[] itemsShouldAbsent(String fileText) {
|
||||
return itemsShouldAbsent(fileText, Platform.ALL);
|
||||
}
|
||||
|
||||
public static CompletionProposal[] processProposalAssertions(String fileText, String... prefixes) {
|
||||
Collection<CompletionProposal> proposals = new ArrayList<CompletionProposal>();
|
||||
for (String proposalStr : InTextDirectivesUtils.findListWithPrefix(fileText, prefix)) {
|
||||
for (String proposalStr : InTextDirectivesUtils.findListWithPrefix(fileText, prefixes)) {
|
||||
Matcher matcher = CompletionProposal.PATTERN.matcher(proposalStr);
|
||||
matcher.find();
|
||||
proposals.add(new CompletionProposal(matcher.group(CompletionProposal.LOOKUP_STRING_GROUP_INDEX),
|
||||
@@ -138,17 +164,31 @@ public class ExpectedCompletionUtils {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Integer getExpectedNumber(String fileText) {
|
||||
return InTextDirectivesUtils.getPrefixedInt(fileText, numberLinePrefix);
|
||||
public static Integer getExpectedNumber(String fileText) {
|
||||
return getExpectedNumber(fileText, Platform.ALL);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Integer getExecutionTime(String fileText) {
|
||||
return InTextDirectivesUtils.getPrefixedInt(fileText, executionTimePrefix);
|
||||
public static Integer getExpectedNumber(String fileText, Platform platform) {
|
||||
switch (platform) {
|
||||
case ALL:
|
||||
return InTextDirectivesUtils.getPrefixedInt(fileText, NUMBER_LINE_PREFIX);
|
||||
case JAVA:
|
||||
return getPlatformExpectedNumber(fileText, NUMBER_JAVA_LINE_PREFIX);
|
||||
case JS:
|
||||
return getPlatformExpectedNumber(fileText, NUMBER_JS_LINE_PREFIX);
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("platform");
|
||||
}
|
||||
|
||||
public boolean isWithOrder(String fileText) {
|
||||
return InTextDirectivesUtils.getPrefixedInt(fileText, withOrderPrefix) != null;
|
||||
@Nullable
|
||||
public static Integer getExecutionTime(String fileText) {
|
||||
return InTextDirectivesUtils.getPrefixedInt(fileText, EXECUTION_TIME_PREFIX);
|
||||
}
|
||||
|
||||
public static boolean isWithOrder(String fileText) {
|
||||
return InTextDirectivesUtils.getPrefixedInt(fileText, WITH_ORDER_PREFIX) != null;
|
||||
}
|
||||
|
||||
protected static void assertContainsRenderedItems(CompletionProposal[] expected, LookupElement[] items, boolean checkOrder) {
|
||||
@@ -178,6 +218,18 @@ public class ExpectedCompletionUtils {
|
||||
}
|
||||
}
|
||||
|
||||
private static Integer getPlatformExpectedNumber(String fileText, String platformNumberPrefix) {
|
||||
Integer prefixedInt = InTextDirectivesUtils.getPrefixedInt(fileText, platformNumberPrefix);
|
||||
if (prefixedInt != null) {
|
||||
Assert.assertNull(String.format("There shouldn't be %s and %s prefixes set in same time", NUMBER_LINE_PREFIX,
|
||||
platformNumberPrefix),
|
||||
InTextDirectivesUtils.getPrefixedInt(fileText, NUMBER_LINE_PREFIX));
|
||||
return prefixedInt;
|
||||
}
|
||||
|
||||
return InTextDirectivesUtils.getPrefixedInt(fileText, NUMBER_LINE_PREFIX);
|
||||
}
|
||||
|
||||
protected static void assertNotContainsRenderedItems(CompletionProposal[] unexpected,LookupElement[] items) {
|
||||
List<CompletionProposal> itemsInformation = getItemsInformation(items);
|
||||
String allItemsString = listToString(itemsInformation);
|
||||
|
||||
@@ -33,14 +33,13 @@ public abstract class JetCompletionMultiTestBase extends CompletionTestCase {
|
||||
complete(completionLevel);
|
||||
|
||||
String fileText = getFile().getText();
|
||||
ExpectedCompletionUtils completionUtils = new ExpectedCompletionUtils();
|
||||
|
||||
ExpectedCompletionUtils.assertContainsRenderedItems(
|
||||
completionUtils.itemsShouldExist(fileText), myItems, completionUtils.isWithOrder(fileText));
|
||||
ExpectedCompletionUtils.itemsShouldExist(fileText), myItems, ExpectedCompletionUtils.isWithOrder(fileText));
|
||||
|
||||
ExpectedCompletionUtils.assertNotContainsRenderedItems(completionUtils.itemsShouldAbsent(fileText), myItems);
|
||||
ExpectedCompletionUtils.assertNotContainsRenderedItems(ExpectedCompletionUtils.itemsShouldAbsent(fileText), myItems);
|
||||
|
||||
Integer itemsNumber = completionUtils.getExpectedNumber(fileText);
|
||||
Integer itemsNumber = ExpectedCompletionUtils.getExpectedNumber(fileText);
|
||||
if (itemsNumber != null) {
|
||||
assertEquals(itemsNumber.intValue(), myItems.length);
|
||||
}
|
||||
|
||||
@@ -25,15 +25,12 @@ import com.intellij.codeInsight.lookup.LookupManager;
|
||||
import com.intellij.openapi.projectRoots.JavaSdk;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import org.apache.commons.lang.SystemUtils;
|
||||
import org.jetbrains.jet.InTextDirectivesUtils;
|
||||
import org.jetbrains.jet.plugin.PluginTestCaseBase;
|
||||
import org.jetbrains.jet.testing.ConfigLibraryUtil;
|
||||
import org.jetbrains.jet.InTextDirectivesUtils;
|
||||
import org.jetbrains.jet.utils.ExceptionUtils;
|
||||
|
||||
public abstract class JetCompletionTestBase extends LightCompletionTestCase {
|
||||
|
||||
private final ExpectedCompletionUtils completionUtils = new ExpectedCompletionUtils();
|
||||
|
||||
private CompletionType type;
|
||||
|
||||
@Override
|
||||
@@ -54,13 +51,13 @@ public abstract class JetCompletionTestBase extends LightCompletionTestCase {
|
||||
ConfigLibraryUtil.configureKotlinRuntime(getModule(), getFullJavaJDK());
|
||||
}
|
||||
|
||||
Integer completionTime = completionUtils.getExecutionTime(fileText);
|
||||
Integer completionTime = ExpectedCompletionUtils.getExecutionTime(fileText);
|
||||
|
||||
complete(completionTime == null ? 1 : completionTime);
|
||||
|
||||
ExpectedCompletionUtils.CompletionProposal[] expected = completionUtils.itemsShouldExist(fileText);
|
||||
ExpectedCompletionUtils.CompletionProposal[] unexpected = completionUtils.itemsShouldAbsent(fileText);
|
||||
Integer itemsNumber = completionUtils.getExpectedNumber(fileText);
|
||||
ExpectedCompletionUtils.CompletionProposal[] expected = ExpectedCompletionUtils.itemsShouldExist(fileText);
|
||||
ExpectedCompletionUtils.CompletionProposal[] unexpected = ExpectedCompletionUtils.itemsShouldAbsent(fileText);
|
||||
Integer itemsNumber = ExpectedCompletionUtils.getExpectedNumber(fileText);
|
||||
|
||||
assertTrue("Should be some assertions about completion",
|
||||
expected.length != 0 || unexpected.length != 0 || itemsNumber != null);
|
||||
@@ -69,7 +66,7 @@ public abstract class JetCompletionTestBase extends LightCompletionTestCase {
|
||||
myItems = new LookupElement[0];
|
||||
}
|
||||
|
||||
ExpectedCompletionUtils.assertContainsRenderedItems(expected, myItems, completionUtils.isWithOrder(fileText));
|
||||
ExpectedCompletionUtils.assertContainsRenderedItems(expected, myItems, ExpectedCompletionUtils.isWithOrder(fileText));
|
||||
ExpectedCompletionUtils.assertNotContainsRenderedItems(unexpected, myItems);
|
||||
|
||||
if (itemsNumber != null) {
|
||||
|
||||
Reference in New Issue
Block a user