set explicit receiver kind properly for calls

This commit is contained in:
Svetlana Isakova
2013-12-04 18:28:08 +04:00
parent cf5174d798
commit 928f77ffce
26 changed files with 612 additions and 35 deletions
@@ -229,11 +229,13 @@ public class JetTestUtils {
return AnalyzerFacadeForJVM.analyzeOneFileWithJavaIntegration(namespace, Collections.<AnalyzerScriptParameter>emptyList());
}
@NotNull
public static JetCoreEnvironment createEnvironmentWithFullJdk(Disposable disposable) {
return createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea(disposable,
ConfigurationKind.ALL, TestJdkKind.FULL_JDK);
}
@NotNull
public static JetCoreEnvironment createEnvironmentWithMockJdkAndIdeaAnnotations(Disposable disposable) {
return createEnvironmentWithMockJdkAndIdeaAnnotations(disposable, ConfigurationKind.ALL);
}
@@ -243,6 +245,7 @@ public class JetTestUtils {
return createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea(disposable, configurationKind, TestJdkKind.MOCK_JDK);
}
@NotNull
public static JetCoreEnvironment createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea(
@NotNull Disposable disposable,
@NotNull ConfigurationKind configurationKind,
@@ -445,6 +448,7 @@ public class JetTestUtils {
return testFiles;
}
@NotNull
public static Map<String, String> parseDirectives(String expectedText) {
Map<String, String> directives = Maps.newHashMap();
Matcher directiveMatcher = DIRECTIVE_PATTERN.matcher(expectedText);
@@ -25,10 +25,12 @@ public abstract class KotlinTestWithEnvironmentManagement extends UsefulTestCase
System.setProperty("java.awt.headless", "true");
}
@NotNull
protected JetCoreEnvironment createEnvironmentWithMockJdk(@NotNull ConfigurationKind configurationKind) {
return createEnvironmentWithJdk(configurationKind, TestJdkKind.MOCK_JDK);
}
@NotNull
protected JetCoreEnvironment createEnvironmentWithJdk(@NotNull ConfigurationKind configurationKind, @NotNull TestJdkKind jdkKind) {
return JetTestUtils.createEnvironmentWithJdkAndNullabilityAnnotationsFromIdea(getTestRootDisposable(), configurationKind, jdkKind);
}
@@ -0,0 +1,116 @@
/*
* 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.resolve.calls
import com.google.common.collect.ImmutableMap
import org.jetbrains.annotations.NotNull
import org.jetbrains.jet.ConfigurationKind
import org.jetbrains.jet.JetLiteFixture
import org.jetbrains.jet.JetTestUtils
import org.jetbrains.jet.analyzer.AnalyzeExhaust
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment
import org.jetbrains.jet.lang.descriptors.CallableDescriptor
import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils
import org.jetbrains.jet.lang.psi.JetElement
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter
import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall
import org.jetbrains.jet.lang.resolve.calls.tasks.ExplicitReceiverKind
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM
import org.jetbrains.jet.lang.resolve.scopes.receivers.AbstractReceiverValue
import org.jetbrains.jet.lang.resolve.scopes.receivers.ExpressionReceiver
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue
import java.io.File
import java.util.Collections
import kotlin.test.assertEquals
import kotlin.test.assertTrue
public abstract class AbstractResolvedCallsTest() : JetLiteFixture() {
override fun createEnvironment(): JetCoreEnvironment = createEnvironmentWithMockJdk(ConfigurationKind.JDK_ONLY)
public fun doTest(filePath: String) {
val file = File(filePath)
val text = JetTestUtils.doLoadFile(file)
val directives = JetTestUtils.parseDirectives(text)
val (callName, thisObject, receiverArgument) = with (directives) {
Triple(get("CALL"), get("THIS_OBJECT"), get("RECEIVER_ARGUMENT"))
}
val explicitReceiverKind = directives.getExplicitReceiverKind()
fun analyzeFileAndGetResolvedCallEntries(): Map<JetElement, ResolvedCall<out CallableDescriptor?>> {
val psiFile = JetTestUtils.loadJetFile(getProject(), file)
val analyzeExhaust = AnalyzerFacadeForJVM.analyzeOneFileWithJavaIntegration(psiFile, Collections.emptyList())
val bindingContext = analyzeExhaust!!.getBindingContext()
return bindingContext.getSliceContents(BindingContext.RESOLVED_CALL)
}
fun checkResolvedCall(resolvedCall: ResolvedCall<out CallableDescriptor?>, element: JetElement) {
val lineAndColumn = DiagnosticUtils.getLineAndColumnInPsiFile(element.getContainingFile(), element.getTextRange())
val (actualThisObject, actualReceiverArgument, actualExplicitReceiverKind) = with(resolvedCall) {
Triple(getThisObject().getText(), getReceiverArgument().getText(), getExplicitReceiverKind())
}
val actualDataMessage = "Actual data:\nThis object: $actualThisObject. Receiver argument: $actualReceiverArgument. " +
"Explicit receiver kind: $actualExplicitReceiverKind.\n"
assertEquals(thisObject, actualThisObject, "${actualDataMessage}This object mismatch: ")
assertEquals(receiverArgument, actualReceiverArgument, "${actualDataMessage}Receiver argument mismatch: ")
assertEquals(explicitReceiverKind, actualExplicitReceiverKind, "$actualDataMessage" +
"Explicit receiver kind for resolved call for '${element.getText()}'$lineAndColumn in not as expected")
}
var callFound = false
for ((element, resolvedCall) in analyzeFileAndGetResolvedCallEntries()) {
if (callName.equals(element.getText())) {
callFound = true
checkResolvedCall(resolvedCall, element)
}
}
assertTrue(callFound, "Resolved call for $callName was not found.")
}
}
private fun ReceiverValue.getText() =
if (this is ExpressionReceiver) {
this.getExpression().getText()
}
else if (this is AbstractReceiverValue) {
this.getType().toString()
}
else toString()
private val EXPLICIT_RECEIVER_KIND_DIRECTIVE: String = "EXPLICIT_RECEIVER_KIND"
private fun Map<String, String>.getExplicitReceiverKind(): ExplicitReceiverKind {
val explicitReceiverKind = get(EXPLICIT_RECEIVER_KIND_DIRECTIVE)
assert(explicitReceiverKind != null) { "$EXPLICIT_RECEIVER_KIND_DIRECTIVE should be present." }
try
{
return ExplicitReceiverKind.valueOf(explicitReceiverKind!!)
}
catch (e: IllegalArgumentException) {
val message = StringBuilder()
message.append("$EXPLICIT_RECEIVER_KIND_DIRECTIVE must be one of the following: ")
for (kind in ExplicitReceiverKind.values()) {
message.append("$kind, ")
}
message.append("\nnot $explicitReceiverKind.")
throw AssertionError(message)
}
}
@@ -0,0 +1,167 @@
/*
* 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.resolve.calls;
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.resolve.calls.AbstractResolvedCallsTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/resolvedCalls")
@InnerTestClasses({JetResolvedCallsTestGenerated.FunctionTypes.class, JetResolvedCallsTestGenerated.Invoke.class, JetResolvedCallsTestGenerated.RealExamples.class})
public class JetResolvedCallsTestGenerated extends AbstractResolvedCallsTest {
public void testAllFilesPresentInResolvedCalls() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/resolvedCalls"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("explicitReceiverIsReceiverArgument.kt")
public void testExplicitReceiverIsReceiverArgument() throws Exception {
doTest("compiler/testData/resolvedCalls/explicitReceiverIsReceiverArgument.kt");
}
@TestMetadata("explicitReceiverIsThisObject.kt")
public void testExplicitReceiverIsThisObject() throws Exception {
doTest("compiler/testData/resolvedCalls/explicitReceiverIsThisObject.kt");
}
@TestMetadata("hasBothThisObjectAndReceiverArgument.kt")
public void testHasBothThisObjectAndReceiverArgument() throws Exception {
doTest("compiler/testData/resolvedCalls/hasBothThisObjectAndReceiverArgument.kt");
}
@TestMetadata("hasBothThisObjectAndReceiverArgumentWithoutExplicitReceiver.kt")
public void testHasBothThisObjectAndReceiverArgumentWithoutExplicitReceiver() throws Exception {
doTest("compiler/testData/resolvedCalls/hasBothThisObjectAndReceiverArgumentWithoutExplicitReceiver.kt");
}
@TestMetadata("implicitReceiverIsReceiverArgument.kt")
public void testImplicitReceiverIsReceiverArgument() throws Exception {
doTest("compiler/testData/resolvedCalls/implicitReceiverIsReceiverArgument.kt");
}
@TestMetadata("implicitReceiverIsThisObject.kt")
public void testImplicitReceiverIsThisObject() throws Exception {
doTest("compiler/testData/resolvedCalls/implicitReceiverIsThisObject.kt");
}
@TestMetadata("impliedThisNoExplicitReceiver.kt")
public void testImpliedThisNoExplicitReceiver() throws Exception {
doTest("compiler/testData/resolvedCalls/impliedThisNoExplicitReceiver.kt");
}
@TestMetadata("simpleCall.kt")
public void testSimpleCall() throws Exception {
doTest("compiler/testData/resolvedCalls/simpleCall.kt");
}
@TestMetadata("compiler/testData/resolvedCalls/functionTypes")
public static class FunctionTypes extends AbstractResolvedCallsTest {
public void testAllFilesPresentInFunctionTypes() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/resolvedCalls/functionTypes"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("invokeForExtensionFunctionType.kt")
public void testInvokeForExtensionFunctionType() throws Exception {
doTest("compiler/testData/resolvedCalls/functionTypes/invokeForExtensionFunctionType.kt");
}
@TestMetadata("invokeForFunctionType.kt")
public void testInvokeForFunctionType() throws Exception {
doTest("compiler/testData/resolvedCalls/functionTypes/invokeForFunctionType.kt");
}
@TestMetadata("valOfExtensionFunctionType.kt")
public void testValOfExtensionFunctionType() throws Exception {
doTest("compiler/testData/resolvedCalls/functionTypes/valOfExtensionFunctionType.kt");
}
@TestMetadata("valOfExtensionFunctionTypeInvoke.kt")
public void testValOfExtensionFunctionTypeInvoke() throws Exception {
doTest("compiler/testData/resolvedCalls/functionTypes/valOfExtensionFunctionTypeInvoke.kt");
}
@TestMetadata("valOfFunctionType.kt")
public void testValOfFunctionType() throws Exception {
doTest("compiler/testData/resolvedCalls/functionTypes/valOfFunctionType.kt");
}
@TestMetadata("valOfFunctionTypeInvoke.kt")
public void testValOfFunctionTypeInvoke() throws Exception {
doTest("compiler/testData/resolvedCalls/functionTypes/valOfFunctionTypeInvoke.kt");
}
}
@TestMetadata("compiler/testData/resolvedCalls/invoke")
public static class Invoke extends AbstractResolvedCallsTest {
public void testAllFilesPresentInInvoke() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/resolvedCalls/invoke"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("bothReceivers.kt")
public void testBothReceivers() throws Exception {
doTest("compiler/testData/resolvedCalls/invoke/bothReceivers.kt");
}
@TestMetadata("implicitReceiverForInvoke.kt")
public void testImplicitReceiverForInvoke() throws Exception {
doTest("compiler/testData/resolvedCalls/invoke/implicitReceiverForInvoke.kt");
}
@TestMetadata("receiverArgumentAsReceiverForInvoke.kt")
public void testReceiverArgumentAsReceiverForInvoke() throws Exception {
doTest("compiler/testData/resolvedCalls/invoke/receiverArgumentAsReceiverForInvoke.kt");
}
@TestMetadata("thisObjectAsReceiverForInvoke.kt")
public void testThisObjectAsReceiverForInvoke() throws Exception {
doTest("compiler/testData/resolvedCalls/invoke/thisObjectAsReceiverForInvoke.kt");
}
}
@TestMetadata("compiler/testData/resolvedCalls/realExamples")
public static class RealExamples extends AbstractResolvedCallsTest {
public void testAllFilesPresentInRealExamples() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/resolvedCalls/realExamples"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("stringPlusInBuilders.kt")
public void testStringPlusInBuilders() throws Exception {
doTest("compiler/testData/resolvedCalls/realExamples/stringPlusInBuilders.kt");
}
}
public static Test suite() {
TestSuite suite = new TestSuite("JetResolvedCallsTestGenerated");
suite.addTestSuite(JetResolvedCallsTestGenerated.class);
suite.addTestSuite(FunctionTypes.class);
suite.addTestSuite(Invoke.class);
suite.addTestSuite(RealExamples.class);
return suite;
}
}