From ed5390fd93b521abe3d6037d11c6ee721dfeafa9 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Wed, 5 Apr 2017 14:54:25 +0200 Subject: [PATCH] Add Java UAST tests --- .../uast/test/env/TestCoreEnvironment.java | 3 + .../test/java/AbstractJavaRenderLogTest.kt | 24 +++ .../uast/test/java/AbstractJavaUastTest.kt | 55 +++++++ .../uast/test/java/AbstractJavaValuesTest.kt | 41 +++++ .../uast/test/java/JavaUastApiTest.kt | 40 +++++ .../uast/test/java/JavaValuesTest.kt | 149 ++++++++++++++++++ .../uast/test/java/SimpleJavaRenderLogTest.kt | 38 +++++ uast/uast-tests/uast-tests.iml | 1 + 8 files changed, 351 insertions(+) create mode 100644 uast/uast-tests/test/org/jetbrains/uast/test/java/AbstractJavaRenderLogTest.kt create mode 100644 uast/uast-tests/test/org/jetbrains/uast/test/java/AbstractJavaUastTest.kt create mode 100644 uast/uast-tests/test/org/jetbrains/uast/test/java/AbstractJavaValuesTest.kt create mode 100644 uast/uast-tests/test/org/jetbrains/uast/test/java/JavaUastApiTest.kt create mode 100644 uast/uast-tests/test/org/jetbrains/uast/test/java/JavaValuesTest.kt create mode 100644 uast/uast-tests/test/org/jetbrains/uast/test/java/SimpleJavaRenderLogTest.kt diff --git a/uast/uast-tests/src/org/jetbrains/uast/test/env/TestCoreEnvironment.java b/uast/uast-tests/src/org/jetbrains/uast/test/env/TestCoreEnvironment.java index e1e8de71f26e..c4f0632f2e4b 100644 --- a/uast/uast-tests/src/org/jetbrains/uast/test/env/TestCoreEnvironment.java +++ b/uast/uast-tests/src/org/jetbrains/uast/test/env/TestCoreEnvironment.java @@ -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 { diff --git a/uast/uast-tests/test/org/jetbrains/uast/test/java/AbstractJavaRenderLogTest.kt b/uast/uast-tests/test/org/jetbrains/uast/test/java/AbstractJavaRenderLogTest.kt new file mode 100644 index 000000000000..7e05e0b19ae3 --- /dev/null +++ b/uast/uast-tests/test/org/jetbrains/uast/test/java/AbstractJavaRenderLogTest.kt @@ -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) +} \ No newline at end of file diff --git a/uast/uast-tests/test/org/jetbrains/uast/test/java/AbstractJavaUastTest.kt b/uast/uast-tests/test/org/jetbrains/uast/test/java/AbstractJavaUastTest.kt new file mode 100644 index 000000000000..87e27b32f59d --- /dev/null +++ b/uast/uast-tests/test/org/jetbrains/uast/test/java/AbstractJavaUastTest.kt @@ -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()) + } +} \ No newline at end of file diff --git a/uast/uast-tests/test/org/jetbrains/uast/test/java/AbstractJavaValuesTest.kt b/uast/uast-tests/test/org/jetbrains/uast/test/java/AbstractJavaValuesTest.kt new file mode 100644 index 000000000000..03a830a2256f --- /dev/null +++ b/uast/uast-tests/test/org/jetbrains/uast/test/java/AbstractJavaValuesTest.kt @@ -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 + } + } +} diff --git a/uast/uast-tests/test/org/jetbrains/uast/test/java/JavaUastApiTest.kt b/uast/uast-tests/test/org/jetbrains/uast/test/java/JavaUastApiTest.kt new file mode 100644 index 000000000000..0a04fff01dd0 --- /dev/null +++ b/uast/uast-tests/test/org/jetbrains/uast/test/java/JavaUastApiTest.kt @@ -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("String s;") + val typeRef = localVar.typeReference + assertNotNull(typeRef) + } + } + + @Test fun testFields() { + doTest("Simple/Field.java") { name, file -> + assertEquals(1, file.classes[0].fields.size) + } + } +} diff --git a/uast/uast-tests/test/org/jetbrains/uast/test/java/JavaValuesTest.kt b/uast/uast-tests/test/org/jetbrains/uast/test/java/JavaValuesTest.kt new file mode 100644 index 000000000000..8305d3b301f7 --- /dev/null +++ b/uast/uast-tests/test/org/jetbrains/uast/test/java/JavaValuesTest.kt @@ -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): 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) + } + }) +} \ No newline at end of file diff --git a/uast/uast-tests/test/org/jetbrains/uast/test/java/SimpleJavaRenderLogTest.kt b/uast/uast-tests/test/org/jetbrains/uast/test/java/SimpleJavaRenderLogTest.kt new file mode 100644 index 000000000000..c7c08b98e6dd --- /dev/null +++ b/uast/uast-tests/test/org/jetbrains/uast/test/java/SimpleJavaRenderLogTest.kt @@ -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") +} diff --git a/uast/uast-tests/uast-tests.iml b/uast/uast-tests/uast-tests.iml index 190539c29448..cff062e79e4b 100644 --- a/uast/uast-tests/uast-tests.iml +++ b/uast/uast-tests/uast-tests.iml @@ -15,5 +15,6 @@ + \ No newline at end of file