Basic utility for rendering DataFlowValues

This commit is contained in:
Andrey Breslav
2013-11-12 23:21:24 +04:00
parent 9aee57bfb5
commit c4ecb9663a
24 changed files with 264 additions and 0 deletions
@@ -523,6 +523,13 @@ public class GenerateTests {
AbstractOutOfBlockModificationTest.class,
testModel("idea/testData/codeInsight/outOfBlock")
);
generateTest(
"idea/tests/",
"DataFlowValueRenderingTestGenerated",
AbstractDataFlowValueRenderingTest.class,
testModel("idea/testData/dataFlowValueRendering")
);
}
private static SimpleTestClassModel testModel(@NotNull String rootPath) {
@@ -0,0 +1,25 @@
package org.jetbrains.jet.plugin.completion
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowValue
import org.jetbrains.jet.lang.resolve.scopes.receivers.ThisReceiver
import org.jetbrains.jet.lang.psi.JetExpression
import org.jetbrains.jet.lang.descriptors.VariableDescriptor
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor
import org.jetbrains.jet.lang.resolve.DescriptorUtils
fun renderDataFlowValue(value: DataFlowValue): String? {
// If it is not a stable identifier, there's no point in rendering it
if (!value.isStableIdentifier()) return null
fun renderId(id: Any?): String? {
return when (id) {
is JetExpression -> id.getText()
is ThisReceiver -> "this@${id.getDeclarationDescriptor().getName()}"
is VariableDescriptor -> id.getName().asString()
is NamespaceDescriptor -> DescriptorUtils.getFQName(id).asString()
is com.intellij.openapi.util.Pair<*, *> -> renderId(id.first) + "." + renderId(id.second)
else -> null
}
}
return renderId(value.getId())
}
@@ -0,0 +1,9 @@
class C {
val a: Any? = null
fun test() {
if (a is String) {
<caret>null
}
}
}
@@ -0,0 +1 @@
this@C.a
@@ -0,0 +1,9 @@
fun outer(c: C) {
if (c.x is String) {
<caret>null
}
}
class C {
val x: Any? = null
}
@@ -0,0 +1 @@
c.x
@@ -0,0 +1,9 @@
fun C.outer(a: Any?) {
if (x is String) {
<caret>null
}
}
class C {
val x: Any? = null
}
@@ -0,0 +1 @@
this@outer.x
@@ -0,0 +1,9 @@
fun outer(c: C?) {
if (c != null && c.x is String) {
<caret>null
}
}
class C {
val x: Any? = null
}
@@ -0,0 +1,9 @@
fun C.outer(a: Any?) {
if (this.x is String) {
<caret>null
}
}
class C {
val x: Any? = null
}
@@ -0,0 +1 @@
this@outer.x
@@ -0,0 +1,7 @@
fun outer(a: Any?) {
fun inner(b: Any?) {
if (a is String && b is String) {
<caret>null
}
}
}
@@ -0,0 +1,2 @@
a
b
@@ -0,0 +1,9 @@
package foo
val a: Any? = null
fun outer() {
if (a is String) {
<caret>null
}
}
@@ -0,0 +1 @@
a
@@ -0,0 +1,7 @@
fun Any?.outer() {
fun Any?.inner() {
if (this is String && this@outer is String) {
<caret>null
}
}
}
@@ -0,0 +1,2 @@
this@inner
this@outer
@@ -0,0 +1,5 @@
fun outer(a: Any?) {
if (a is String) {
<caret>null
}
}
@@ -0,0 +1 @@
a
@@ -0,0 +1,5 @@
fun outer(a: Any?) {
if (a != null) {
<caret>null
}
}
@@ -0,0 +1 @@
a
@@ -0,0 +1,52 @@
/*
* Copyright 2010-2013 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.jet.completion
import org.jetbrains.jet.plugin.PluginTestCaseBase
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.lang.psi.JetExpression
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.jet.plugin.completion.renderDataFlowValue
import org.jetbrains.jet.JetTestUtils
import com.intellij.openapi.util.io.FileUtil
import java.io.File
abstract class AbstractDataFlowValueRenderingTest: LightCodeInsightFixtureTestCase() {
override fun getTestDataPath() : String {
return PluginTestCaseBase.getTestDataPathBase() + "/dataFlowValueRendering/"
}
fun doTest(fileName: String) {
val fixture = myFixture!!
fixture.configureByFile(fileName)
val jetFile = fixture.getFile() as JetFile
val bindingContext = AnalyzerFacadeWithCache.analyzeFileWithCache(jetFile).getBindingContext()
val element = jetFile.findElementAt(fixture.getCaretOffset())
val expression = PsiTreeUtil.getParentOfType(element, javaClass<JetExpression>())
val info = bindingContext.get(BindingContext.EXPRESSION_DATA_FLOW_INFO, expression)!!
val allValues = (info.getCompleteTypeInfo().keySet() + info.getCompleteNullabilityInfo().keySet()).toSet()
val actual = allValues.map { renderDataFlowValue(it) }.filterNotNull().sort().makeString("\n")
JetTestUtils.assertEqualsToFile(File(FileUtil.getNameWithoutExtension(fileName) + ".txt"), actual)
}
}
@@ -0,0 +1,89 @@
/*
* Copyright 2010-2013 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.jet.completion;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.io.File;
import java.util.regex.Pattern;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.test.InnerTestClasses;
import org.jetbrains.jet.test.TestMetadata;
import org.jetbrains.jet.completion.AbstractDataFlowValueRenderingTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("idea/testData/dataFlowValueRendering")
public class DataFlowValueRenderingTestGenerated extends AbstractDataFlowValueRenderingTest {
public void testAllFilesPresentInDataFlowValueRendering() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/dataFlowValueRendering"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("classProperty.kt")
public void testClassProperty() throws Exception {
doTest("idea/testData/dataFlowValueRendering/classProperty.kt");
}
@TestMetadata("complexIdentifier.kt")
public void testComplexIdentifier() throws Exception {
doTest("idea/testData/dataFlowValueRendering/complexIdentifier.kt");
}
@TestMetadata("complexIdentifierWithImplicitReceiver.kt")
public void testComplexIdentifierWithImplicitReceiver() throws Exception {
doTest("idea/testData/dataFlowValueRendering/complexIdentifierWithImplicitReceiver.kt");
}
@TestMetadata("complexIdentifierWithInitiallyNullableReceiver.kt")
public void testComplexIdentifierWithInitiallyNullableReceiver() throws Exception {
doTest("idea/testData/dataFlowValueRendering/complexIdentifierWithInitiallyNullableReceiver.kt");
}
@TestMetadata("complexIdentifierWithReceiver.kt")
public void testComplexIdentifierWithReceiver() throws Exception {
doTest("idea/testData/dataFlowValueRendering/complexIdentifierWithReceiver.kt");
}
@TestMetadata("multipleVariables.kt")
public void testMultipleVariables() throws Exception {
doTest("idea/testData/dataFlowValueRendering/multipleVariables.kt");
}
@TestMetadata("packageProperty.kt")
public void testPackageProperty() throws Exception {
doTest("idea/testData/dataFlowValueRendering/packageProperty.kt");
}
@TestMetadata("receivers.kt")
public void testReceivers() throws Exception {
doTest("idea/testData/dataFlowValueRendering/receivers.kt");
}
@TestMetadata("smartCast.kt")
public void testSmartCast() throws Exception {
doTest("idea/testData/dataFlowValueRendering/smartCast.kt");
}
@TestMetadata("smartNotNull.kt")
public void testSmartNotNull() throws Exception {
doTest("idea/testData/dataFlowValueRendering/smartNotNull.kt");
}
}