Convert JetCodeConformanceTest to Kotlin: invoke J2K
This commit is contained in:
@@ -14,120 +14,96 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.code;
|
package org.jetbrains.kotlin.code
|
||||||
|
|
||||||
import com.intellij.openapi.util.io.FileUtil;
|
import com.intellij.openapi.util.io.FileUtil
|
||||||
import com.intellij.openapi.util.text.StringUtil;
|
import com.intellij.openapi.util.text.StringUtil
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase
|
||||||
import kotlin.jvm.functions.Function1;
|
import org.junit.Assert
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.junit.Assert;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File
|
||||||
import java.io.IOException;
|
import java.io.IOException
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList
|
||||||
import java.util.Arrays;
|
import java.util.Arrays
|
||||||
import java.util.List;
|
import java.util.regex.Matcher
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Pattern
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
public class JetCodeConformanceTest extends TestCase {
|
public class JetCodeConformanceTest : TestCase() {
|
||||||
private static final Pattern JAVA_FILE_PATTERN = Pattern.compile(".+\\.java");
|
|
||||||
private static final Pattern SOURCES_FILE_PATTERN = Pattern.compile("(.+\\.java|.+\\.kt|.+\\.js)");
|
|
||||||
private static final List<File> EXCLUDED_FILES_AND_DIRS = Arrays.asList(
|
|
||||||
new File("android.tests.dependencies"),
|
|
||||||
new File("core/reflection.jvm/src/kotlin/reflect/jvm/internal/pcollections"),
|
|
||||||
new File("libraries/tools/kotlin-reflect/target/copied-sources"),
|
|
||||||
new File("dependencies"),
|
|
||||||
new File("js/js.translator/qunit/qunit.js"),
|
|
||||||
new File("libraries/tools/kotlin-js-tests/src/test/web/qunit.js"),
|
|
||||||
new File("out"),
|
|
||||||
new File("dist"),
|
|
||||||
new File("ideaSDK"),
|
|
||||||
new File("libraries/tools/kotlin-gradle-plugin-core/gradle_api_jar/build/tmp"),
|
|
||||||
new File("libraries/tools/kotlin-maven-plugin/target/generated-sources"),
|
|
||||||
new File("compiler/testData/psi/kdoc"),
|
|
||||||
new File("compiler/tests/org/jetbrains/kotlin/parsing/JetCodeConformanceTest.java")
|
|
||||||
);
|
|
||||||
public static final Pattern AUTHOR_JAVADOC_PATTERN = Pattern.compile("/\\*.+@author.+\\*/", Pattern.DOTALL);
|
|
||||||
|
|
||||||
public void testParserCode() throws Exception {
|
throws(Exception::class)
|
||||||
for (File sourceFile : FileUtil.findFilesByMask(JAVA_FILE_PATTERN, new File("compiler/frontend/src/org/jetbrains/kotlin/parsing"))) {
|
public fun testParserCode() {
|
||||||
String source = FileUtil.loadFile(sourceFile, true);
|
for (sourceFile in FileUtil.findFilesByMask(JAVA_FILE_PATTERN, File("compiler/frontend/src/org/jetbrains/kotlin/parsing"))) {
|
||||||
|
val source = FileUtil.loadFile(sourceFile, true)
|
||||||
|
|
||||||
Pattern atPattern = Pattern.compile("assert.*?\\b[^_]at.*?$", Pattern.MULTILINE);
|
val atPattern = Pattern.compile("assert.*?\\b[^_]at.*?$", Pattern.MULTILINE)
|
||||||
Matcher matcher = atPattern.matcher(source);
|
val matcher = atPattern.matcher(source)
|
||||||
|
|
||||||
if (matcher.find()) {
|
if (matcher.find()) {
|
||||||
fail("An at-method with side-effects is used inside assert: " + matcher.group() + "\nin file: " + sourceFile);
|
TestCase.fail("An at-method with side-effects is used inside assert: " + matcher.group() + "\nin file: " + sourceFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testForAuthorJavadoc() throws IOException {
|
throws(IOException::class)
|
||||||
List<File> filesWithAuthorJavadoc = filterSourceFiles(new Function1<String, Boolean>() {
|
public fun testForAuthorJavadoc() {
|
||||||
@Override
|
val filesWithAuthorJavadoc = filterSourceFiles(object : Function1<String, Boolean> {
|
||||||
public Boolean invoke(String source) {
|
override fun invoke(source: String): Boolean? {
|
||||||
// .contains() is invoked for optimization
|
// .contains() is invoked for optimization
|
||||||
return source.contains("@author") && AUTHOR_JAVADOC_PATTERN.matcher(source).find();
|
return source.contains("@author") && AUTHOR_JAVADOC_PATTERN.matcher(source).find()
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
if (!filesWithAuthorJavadoc.isEmpty()) {
|
if (!filesWithAuthorJavadoc.isEmpty()) {
|
||||||
fail(String.format("%d source files contain @author javadoc tag. Please remove them or exclude in this test:\n%s",
|
TestCase.fail(String.format("%d source files contain @author javadoc tag. Please remove them or exclude in this test:\n%s", filesWithAuthorJavadoc.size(), StringUtil.join(filesWithAuthorJavadoc, "\n")))
|
||||||
filesWithAuthorJavadoc.size(), StringUtil.join(filesWithAuthorJavadoc, "\n")));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNoJCommanderInternalImports() throws IOException {
|
throws(IOException::class)
|
||||||
List<File> filesWithJCommander = filterSourceFiles(new Function1<String, Boolean>() {
|
public fun testNoJCommanderInternalImports() {
|
||||||
@Override
|
val filesWithJCommander = filterSourceFiles(object : Function1<String, Boolean> {
|
||||||
public Boolean invoke(String source) {
|
override fun invoke(source: String): Boolean? {
|
||||||
return source.contains("com.beust.jcommander.internal");
|
return source.contains("com.beust.jcommander.internal")
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
Assert.assertTrue(
|
Assert.assertTrue("It seems that you've used something from com.beust.jcommander.internal package. This code won't work when there's " + "no TestNG in the classpath of our IDEA plugin, because there's only an optional dependency on testng.jar.\n" + "Most probably you meant to use Guava's Lists, Maps or Sets instead. Please change references in these files to " + "com.google.common.collect: " + filesWithJCommander, filesWithJCommander.isEmpty())
|
||||||
"It seems that you've used something from com.beust.jcommander.internal package. This code won't work when there's " +
|
|
||||||
"no TestNG in the classpath of our IDEA plugin, because there's only an optional dependency on testng.jar.\n" +
|
|
||||||
"Most probably you meant to use Guava's Lists, Maps or Sets instead. Please change references in these files to " +
|
|
||||||
"com.google.common.collect: " + filesWithJCommander,
|
|
||||||
filesWithJCommander.isEmpty()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNoOrgJetbrainsJet() throws IOException {
|
throws(IOException::class)
|
||||||
List<File> filesWithOrgJetbrainsJet = filterSourceFiles(new Function1<String, Boolean>() {
|
public fun testNoOrgJetbrainsJet() {
|
||||||
@Override
|
val filesWithOrgJetbrainsJet = filterSourceFiles(object : Function1<String, Boolean> {
|
||||||
public Boolean invoke(String source) {
|
override fun invoke(source: String): Boolean? {
|
||||||
return source.contains("org.jetbrains.jet");
|
return source.contains("org.jetbrains.jet")
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
Assert.assertTrue(
|
Assert.assertTrue("Package org.jetbrains.jet is deprecated now in favor of org.jetbrains.kotlin. " + "Please consider changing the package in these files: " + filesWithOrgJetbrainsJet, filesWithOrgJetbrainsJet.isEmpty())
|
||||||
"Package org.jetbrains.jet is deprecated now in favor of org.jetbrains.kotlin. " +
|
|
||||||
"Please consider changing the package in these files: " + filesWithOrgJetbrainsJet,
|
|
||||||
filesWithOrgJetbrainsJet.isEmpty()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
companion object {
|
||||||
private static List<File> filterSourceFiles(@NotNull Function1<String, Boolean> predicate) throws IOException {
|
private val JAVA_FILE_PATTERN = Pattern.compile(".+\\.java")
|
||||||
List<File> result = new ArrayList<File>();
|
private val SOURCES_FILE_PATTERN = Pattern.compile("(.+\\.java|.+\\.kt|.+\\.js)")
|
||||||
for (File sourceFile : FileUtil.findFilesByMask(SOURCES_FILE_PATTERN, new File("."))) {
|
private val EXCLUDED_FILES_AND_DIRS = Arrays.asList<File>(File("android.tests.dependencies"), File("core/reflection.jvm/src/kotlin/reflect/jvm/internal/pcollections"), File("libraries/tools/kotlin-reflect/target/copied-sources"), File("dependencies"), File("js/js.translator/qunit/qunit.js"), File("libraries/tools/kotlin-js-tests/src/test/web/qunit.js"), File("out"), File("dist"), File("ideaSDK"), File("libraries/tools/kotlin-gradle-plugin-core/gradle_api_jar/build/tmp"), File("libraries/tools/kotlin-maven-plugin/target/generated-sources"), File("compiler/testData/psi/kdoc"), File("compiler/tests/org/jetbrains/kotlin/parsing/JetCodeConformanceTest.java"))
|
||||||
if (!excludeFile(sourceFile) && predicate.invoke(FileUtil.loadFile(sourceFile, true))) {
|
public val AUTHOR_JAVADOC_PATTERN: Pattern = Pattern.compile("/\\*.+@author.+\\*/", Pattern.DOTALL)
|
||||||
result.add(sourceFile);
|
|
||||||
|
throws(IOException::class)
|
||||||
|
private fun filterSourceFiles(predicate: Function1<String, Boolean>): List<File> {
|
||||||
|
val result = ArrayList<File>()
|
||||||
|
for (sourceFile in FileUtil.findFilesByMask(SOURCES_FILE_PATTERN, File("."))) {
|
||||||
|
if (!excludeFile(sourceFile) && predicate.invoke(FileUtil.loadFile(sourceFile, true))) {
|
||||||
|
result.add(sourceFile)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean excludeFile(@NotNull File file) {
|
private fun excludeFile(file: File): Boolean {
|
||||||
for (File excludedFileOrDir : EXCLUDED_FILES_AND_DIRS) {
|
for (excludedFileOrDir in EXCLUDED_FILES_AND_DIRS) {
|
||||||
if (FileUtil.isAncestor(excludedFileOrDir, file, false)) {
|
if (FileUtil.isAncestor(excludedFileOrDir, file, false)) {
|
||||||
return true;
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user