Add Java UAST tests

This commit is contained in:
Dmitry Jemerov
2017-04-06 15:19:42 +02:00
parent 72613dc7c8
commit ed5390fd93
8 changed files with 351 additions and 0 deletions
@@ -22,6 +22,7 @@ import com.intellij.core.CoreApplicationEnvironment;
import com.intellij.core.CoreJavaFileManager;
import com.intellij.core.JavaCoreApplicationEnvironment;
import com.intellij.core.JavaCoreProjectEnvironment;
import com.intellij.lang.MetaLanguage;
import com.intellij.mock.MockProject;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.components.ServiceManager;
@@ -168,6 +169,8 @@ public class TestCoreEnvironment extends AbstractCoreEnvironment {
Extensions.getRootArea(), ClsCustomNavigationPolicy.EP_NAME, ClsCustomNavigationPolicy.class);
CoreApplicationEnvironment.registerExtensionPoint(
Extensions.getRootArea(), ClassFileDecompilers.EP_NAME, ClassFileDecompilers.Decompiler.class);
CoreApplicationEnvironment.registerExtensionPoint(
Extensions.getRootArea(), MetaLanguage.EP_NAME, MetaLanguage.class);
}
private class TestJavaCoreProjectEnvironment extends JavaCoreProjectEnvironment {
@@ -0,0 +1,24 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.uast.test.java
import org.jetbrains.uast.test.common.RenderLogTestBase
import java.io.File
abstract class AbstractJavaRenderLogTest : AbstractJavaUastTest(), RenderLogTestBase {
override fun getTestFile(testName: String, ext: String) =
File(File(TEST_JAVA_MODEL_DIR, testName).canonicalPath.substringBeforeLast('.') + '.' + ext)
}
@@ -0,0 +1,55 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.uast.test.java
import com.intellij.openapi.application.ex.PathManagerEx
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.vfs.StandardFileSystems
import com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.uast.test.env.AbstractCoreEnvironment
import org.jetbrains.uast.test.env.AbstractUastTest
import org.jetbrains.uast.test.env.TestCoreEnvironment
import java.io.File
abstract class AbstractJavaUastTest : AbstractUastTest() {
protected companion object {
val TEST_JAVA_MODEL_DIR = File(PathManagerEx.getCommunityHomePath(), "uast/uast-tests/java")
}
override fun getVirtualFile(testName: String): VirtualFile {
val testSourcesDir = File(TEST_JAVA_MODEL_DIR, testName.substringBefore('/'))
super.initializeEnvironment(testSourcesDir)
val vfs = StandardFileSystems.local()
environment.addJavaSourceRoot(testSourcesDir)
val javaHome = System.getProperty("java.home")
var rtJar = File(javaHome, "jre/lib/rt.jar")
if (!rtJar.exists()) {
rtJar = File(javaHome, "lib/rt.jar")
}
if (rtJar.exists()) {
environment.addJar(rtJar)
}
return vfs.findFileByPath(File(TEST_JAVA_MODEL_DIR, testName).canonicalPath)!!
}
override fun createEnvironment(source: File): AbstractCoreEnvironment {
return TestCoreEnvironment(Disposer.newDisposable())
}
}
@@ -0,0 +1,41 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.uast.test.java
import org.jetbrains.uast.evaluation.UEvaluatorExtension
import org.jetbrains.uast.test.common.ValuesTestBase
import java.io.File
abstract class AbstractJavaValuesTest : AbstractJavaUastTest(), ValuesTestBase {
protected var _evaluatorExtension: UEvaluatorExtension? = null
override fun getEvaluatorExtension(): UEvaluatorExtension? = _evaluatorExtension
private fun getTestFile(testName: String, ext: String) =
File(File(TEST_JAVA_MODEL_DIR, testName).canonicalPath.substringBeforeLast('.') + '.' + ext)
override fun getValuesFile(testName: String) = getTestFile(testName, "values.txt")
fun doTest(name: String, extension: UEvaluatorExtension) {
_evaluatorExtension = extension
try {
doTest(name)
}
finally {
_evaluatorExtension = null
}
}
}
@@ -0,0 +1,40 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.uast.test.java
import org.jetbrains.uast.UFile
import org.jetbrains.uast.ULocalVariable
import org.jetbrains.uast.test.env.findElementByText
import org.junit.Test
class JavaUastApiTest : AbstractJavaUastTest() {
override fun check(testName: String, file: UFile) {
}
@Test fun testTypeReference() {
doTest("Simple/TypeReference.java") { name, file ->
val localVar = file.findElementByText<ULocalVariable>("String s;")
val typeRef = localVar.typeReference
assertNotNull(typeRef)
}
}
@Test fun testFields() {
doTest("Simple/Field.java") { name, file ->
assertEquals(1, file.classes[0].fields.size)
}
}
}
@@ -0,0 +1,149 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.uast.test.java
import com.intellij.psi.PsiMethod
import org.jetbrains.uast.UParameter
import org.jetbrains.uast.UVariable
import org.jetbrains.uast.evaluation.SimpleEvaluatorExtension
import org.jetbrains.uast.values.UBooleanConstant
import org.jetbrains.uast.values.UValue
import org.junit.Test
class JavaValuesTest : AbstractJavaValuesTest() {
@Test fun testAliveThenElse() = doTest("Simple/AliveThenElse.java")
@Test fun testAnonymous() = doTest("Simple/Anonymous.java")
@Test fun testBitwise() = doTest("Simple/Bitwise.java")
@Test fun testByteShort() = doTest("Simple/ByteShort.java")
@Test fun testCascadeIf() = doTest("Simple/CascadeIf.java")
@Test fun testCharacters() = doTest("Simple/Characters.java")
@Test fun testClassLiteral() = doTest("Simple/ClassLiteral.java")
@Test fun testDeadElse() = doTest("Simple/DeadElse.java")
@Test fun testDeadFor() = doTest("Simple/DeadFor.java")
@Test fun testDeadIfComparison() = doTest("Simple/DeadIfComparison.java")
@Test fun testDeadSwitchEntries() = doTest("Simple/DeadSwitchEntries.java")
@Test fun testDeadSwitchEntriesWithoutBreaks() = doTest("Simple/DeadSwitchEntriesWithoutBreaks.java")
@Test fun testDeadThen() = doTest("Simple/DeadThen.java")
@Test fun testDependents() = doTest("Simple/Dependents.java")
@Test fun testDoWhile() = doTest("Simple/DoWhile.java")
@Test fun testDoWhileInfinite() = doTest("Simple/DoWhileInfinite.java")
@Test fun testDoWhileWithReturn() = doTest("Simple/DoWhileWithReturn.java")
@Test fun testEnumChoice() = doTest("Simple/EnumChoice.java")
@Test fun testEnumSwitch() = doTest("Simple/EnumSwitch.java")
@Test fun testEnumSwitchConditionalBreak() = doTest("Simple/EnumSwitchConditionalBreak.java")
@Test fun testEnumSwitchWithoutBreaks() = doTest("Simple/EnumSwitchWithoutBreaks.java")
@Test fun testExternal() = doTest("Simple/External.java")
@Test fun testFieldRef() = doTest("Simple/FieldRef.java")
@Test fun testFloatDouble() = doTest("Simple/FloatDouble.java")
@Test fun testFor() = doTest("Simple/For.java")
@Test fun testForEach() = doTest("Simple/ForEach.java")
@Test fun testForEachMutableIterable() = doTest("Simple/ForEachMutableIterable.java")
@Test fun testIdentityEquals() = doTest("Simple/IdentityEquals.java")
@Test fun testImmutableField() = doTest("Simple/ImmutableField.java")
@Test fun testIncDec() = doTest("Simple/IncDec.java")
@Test fun testIntLong() = doTest("Simple/IntLong.java")
@Test fun testLabeled() = doTest("Simple/Labeled.java")
@Test fun testLabeledOuter() = doTest("Simple/LabeledOuter.java")
@Test fun testLambda() = doTest("Simple/Lambda.java")
@Test fun testLogicals() = doTest("Simple/Logicals.java")
@Test fun testMethodReference() = doTest("Simple/MethodReference.java")
@Test fun testModification() = doTest("Simple/Modification.java")
@Test fun testMutableField() = doTest("Simple/MutableField.java")
@Test fun testNotANumber() = doTest("Simple/NotANumber.java")
@Test fun testReturnMinusX() = doTest("Simple/ReturnMinusX.java")
@Test fun testReturnSum() = doTest("Simple/ReturnSum.java")
@Test fun testReturnX() = doTest("Simple/ReturnX.java")
@Test fun testShift() = doTest("Simple/Shift.java")
@Test fun testStrings() = doTest("Simple/Strings.java")
@Test fun testTernary() = doTest("Simple/Ternary.java")
@Test fun testTryCatch() = doTest("Simple/TryCatch.java")
@Test fun testWhile() = doTest("Simple/While.java")
@Test fun testWhileWithContinue() = doTest("Simple/WhileWithContinue.java")
@Test fun testWhileWithIncrement() = doTest("Simple/WhileWithIncrement.java")
@Test fun testWhileWithMutableCondition() = doTest("Simple/WhileWithMutableCondition.java")
@Test fun testWhileWithReturn() = doTest("Simple/WhileWithReturn.java")
@Test fun testEvaluatorExtension() = doTest("Simple/EvaluatorExtension.java", object : SimpleEvaluatorExtension() {
override fun evaluateMethodCall(target: PsiMethod, argumentValues: List<UValue>): Any? {
if (target.name == "getTestName") {
(argumentValues.singleOrNull() as? UBooleanConstant)?.let { arg ->
return if (arg.value) "UPPER" else "lower"
}
}
return super.evaluateMethodCall(target, argumentValues)
}
})
@Test fun testParamViaEvaluatorExtension() = doTest("Simple/ParamViaEvaluatorExtension.java", object : SimpleEvaluatorExtension() {
override fun evaluateVariable(variable: UVariable): Any? {
if (variable is UParameter && variable.name == "x") {
return "0"
}
return super.evaluateVariable(variable)
}
})
}
@@ -0,0 +1,38 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.uast.test.java
import org.junit.Test
class SimpleJavaRenderLogTest : AbstractJavaRenderLogTest() {
@Test fun testDataClass() = doTest("DataClass/DataClass.java")
@Test fun testEnumSwitch() = doTest("Simple/EnumSwitch.java")
@Test fun testLocalClass() = doTest("Simple/LocalClass.java")
@Test fun testReturnX() = doTest("Simple/ReturnX.java")
@Test fun testJava() = doTest("Simple/Simple.java")
@Test fun testClass() = doTest("Simple/SuperTypes.java")
@Test fun testTryWithResources() = doTest("Simple/TryWithResources.java")
@Test fun testEnumValueMembers() = doTest("Simple/EnumValueMembers.java")
@Test fun testQualifiedConstructorCall() = doTest("Simple/QualifiedConstructorCall.java")
}
+1
View File
@@ -15,5 +15,6 @@
<orderEntry type="module" module-name="uast-java" />
<orderEntry type="module" module-name="java-runtime" />
<orderEntry type="module" module-name="java-psi-impl" />
<orderEntry type="module" module-name="testFramework" scope="TEST" />
</component>
</module>