Add ability to test for unexpected directives in tested file
This commit is contained in:
@@ -16,17 +16,18 @@
|
||||
|
||||
package org.jetbrains.jet;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import junit.framework.Assert;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
public final class InTextDirectivesUtils {
|
||||
|
||||
@@ -50,7 +51,7 @@ public final class InTextDirectivesUtils {
|
||||
|
||||
@NotNull
|
||||
public static List<String> findListWithPrefix(String fileText, String... prefixes) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
List<String> result = new ArrayList<String>();
|
||||
|
||||
for (String line : findLinesWithPrefixRemoved(fileText, prefixes)) {
|
||||
String[] variants = line.split(",");
|
||||
@@ -76,14 +77,17 @@ public final class InTextDirectivesUtils {
|
||||
|
||||
@NotNull
|
||||
public static List<String> findLinesWithPrefixRemoved(String fileText, String... prefixes) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
List<String> result = new ArrayList<String>();
|
||||
List<String> cleanedPrefixes = cleanDirectivesFromComments(Arrays.asList(prefixes));
|
||||
|
||||
for (String line : fileNonEmptyLines(fileText)) {
|
||||
for (String prefix : prefixes) {
|
||||
for (String line : fileNonEmptyCommentedLines(fileText)) {
|
||||
for (String prefix : cleanedPrefixes) {
|
||||
if (line.startsWith(prefix)) {
|
||||
String noPrefixLine = line.substring(prefix.length());
|
||||
|
||||
if (noPrefixLine.isEmpty() || Character.isWhitespace(noPrefixLine.charAt(0))) {
|
||||
if (noPrefixLine.isEmpty() ||
|
||||
Character.isWhitespace(noPrefixLine.charAt(0)) ||
|
||||
Character.isWhitespace(prefix.charAt(prefix.length() - 1))) {
|
||||
result.add(noPrefixLine.trim());
|
||||
break;
|
||||
}
|
||||
@@ -94,9 +98,51 @@ public final class InTextDirectivesUtils {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void assertHasUnknownPrefixes(String fileText, Collection<String> knownPrefixes) {
|
||||
Set<String> prefixes = Sets.newHashSet();
|
||||
|
||||
for (String line : fileNonEmptyCommentedLines(fileText)) {
|
||||
String prefix = probableDirective(line);
|
||||
if (prefix != null) {
|
||||
prefixes.add(prefix);
|
||||
}
|
||||
}
|
||||
|
||||
prefixes.removeAll(cleanDirectivesFromComments(knownPrefixes));
|
||||
|
||||
Assert.assertTrue("File contains some unexpected directives" + prefixes, prefixes.isEmpty());
|
||||
}
|
||||
|
||||
private static String probableDirective(String line) {
|
||||
String[] arr = line.split(" ", 2);
|
||||
String firstWord = arr[0];
|
||||
|
||||
if (firstWord.length() > 1 && StringUtil.toUpperCase(firstWord).equals(firstWord)) {
|
||||
return firstWord;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static List<String> cleanDirectivesFromComments(Collection<String> prefixes) {
|
||||
List<String> resultPrefixes = Lists.newArrayList();
|
||||
|
||||
for (String prefix : prefixes) {
|
||||
if (prefix.startsWith("//")) {
|
||||
resultPrefixes.add(StringUtil.trimLeading(prefix.substring(2)));
|
||||
}
|
||||
else {
|
||||
resultPrefixes.add(prefix);
|
||||
}
|
||||
}
|
||||
|
||||
return resultPrefixes;
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
private static List<String> fileNonEmptyLines(String fileText) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
private static List<String> fileNonEmptyCommentedLines(String fileText) {
|
||||
List<String> result = new ArrayList<String>();
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new StringReader(fileText));
|
||||
@@ -104,8 +150,11 @@ public final class InTextDirectivesUtils {
|
||||
String line;
|
||||
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (!line.isEmpty()) {
|
||||
result.add(line.trim());
|
||||
if (line.startsWith("//")) {
|
||||
String uncommentedLine = line.substring(2).trim();
|
||||
if (!uncommentedLine.isEmpty()) {
|
||||
result.add(uncommentedLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user