From 2ebe8fd6a2971731113c7e63e8a8cc0b3ec8db3a Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Fri, 31 Jan 2014 18:52:34 +0400 Subject: [PATCH] improvements after review in constraint system tests --- .../checkStatus/violatedUpperBound.bounds | 2 +- .../integerValueTypes/defaultLong.bounds | 3 +- .../AbstractConstraintSystemTest.kt | 28 ++++++++----------- .../constraintSystem/MyDeclarations.kt | 12 +++++++- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/compiler/testData/constraintSystem/checkStatus/violatedUpperBound.bounds b/compiler/testData/constraintSystem/checkStatus/violatedUpperBound.bounds index 244e16764cc..928cfd4aa1f 100644 --- a/compiler/testData/constraintSystem/checkStatus/violatedUpperBound.bounds +++ b/compiler/testData/constraintSystem/checkStatus/violatedUpperBound.bounds @@ -1,7 +1,7 @@ VARIABLES T SUBTYPE T Int -SUBTYPE T String ! +SUBTYPE T String weak type parameter bounds: T <: jet.Int(SPECIAL), <: jet.String(TYPE_BOUND_POSITION(0)) diff --git a/compiler/testData/constraintSystem/integerValueTypes/defaultLong.bounds b/compiler/testData/constraintSystem/integerValueTypes/defaultLong.bounds index b24a930adce..09ede966f72 100644 --- a/compiler/testData/constraintSystem/integerValueTypes/defaultLong.bounds +++ b/compiler/testData/constraintSystem/integerValueTypes/defaultLong.bounds @@ -1,9 +1,10 @@ VARIABLES T +SUPERTYPE T IntegerValueType(1) SUPERTYPE T IntegerValueType(10000000000) type parameter bounds: -T >: IntegerValueType(10000000000)(SPECIAL) +T >: IntegerValueType(1)(SPECIAL), >: IntegerValueType(10000000000)(SPECIAL) status: -hasConflictingConstraints: false -hasContradiction: false diff --git a/compiler/tests/org/jetbrains/jet/resolve/constraintSystem/AbstractConstraintSystemTest.kt b/compiler/tests/org/jetbrains/jet/resolve/constraintSystem/AbstractConstraintSystemTest.kt index 98aa9bb44c6..c9b1a0f2117 100644 --- a/compiler/tests/org/jetbrains/jet/resolve/constraintSystem/AbstractConstraintSystemTest.kt +++ b/compiler/tests/org/jetbrains/jet/resolve/constraintSystem/AbstractConstraintSystemTest.kt @@ -52,17 +52,15 @@ import java.util.ArrayList import kotlin.test.assertEquals import org.junit.Assert import java.util.LinkedHashMap +import kotlin.properties.Delegates abstract public class AbstractConstraintSystemTest() : JetLiteFixture() { private val typePattern = """([\w|<|>|\(|\)]+)""" - val constraintPattern = Pattern.compile("""(SUBTYPE|SUPERTYPE|EQUALITY)\s+${typePattern}\s+${typePattern}\s+(!)?""", Pattern.MULTILINE) + val constraintPattern = Pattern.compile("""(SUBTYPE|SUPERTYPE)\s+${typePattern}\s+${typePattern}\s+(weak)?""", Pattern.MULTILINE) val variablesPattern = Pattern.compile("VARIABLES\\s+(.*)") - private var builtIns: KotlinBuiltIns? = null - private var typeResolver: TypeResolver? = null - private var expressionTypingServices: ExpressionTypingServices? = null - private var functionDescriptor: FunctionDescriptor? = null - private var scopeToResolveTypeParameters: JetScope? = null + private var typeResolver: TypeResolver by Delegates.notNull() + private var myDeclarations: MyDeclarations by Delegates.notNull() override fun createEnvironment(): JetCoreEnvironment { return createEnvironmentWithMockJdk(ConfigurationKind.ALL) @@ -71,13 +69,9 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() { override fun setUp() { super.setUp() - builtIns = KotlinBuiltIns.getInstance() - val injector = InjectorForTests(getProject(), JetTestUtils.createEmptyModule()!!) - typeResolver = injector.getTypeResolver() - expressionTypingServices = injector.getExpressionTypingServices() - - analyzeDeclarations() + typeResolver = injector.getTypeResolver()!! + myDeclarations = analyzeDeclarations() } override fun getTestDataPath(): String { @@ -90,7 +84,7 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() { val psiFile = createPsiFile(null, fileName, loadFile(fileName)) val analyzeExhaust = AnalyzerFacadeForJVM.analyzeOneFileWithJavaIntegration(psiFile, Collections.emptyList())!! val bindingContext = analyzeExhaust.getBindingContext() - return MyDeclarations(bindingContext, getProject(), typeResolver!!) + return MyDeclarations(bindingContext, getProject(), typeResolver) } public fun doTest(filePath: String) { @@ -98,7 +92,6 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() { val fileText = JetTestUtils.doLoadFile(file)!! val constraintSystem = ConstraintSystemImpl() - val myDeclarations = analyzeDeclarations() val typeParameterDescriptors = LinkedHashMap() val variables = parseVariables(fileText) @@ -124,8 +117,9 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() { val resultingSubstitutor = constraintSystem.getResultingSubstitutor() val result = StringBuilder() append "result:\n" for ((typeParameter, variance) in typeParameterDescriptors) { - val resultForTypeParameter = resultingSubstitutor.substitute(myDeclarations.getType(typeParameter.getName().asString()), variance) - result append "${typeParameter.getName()}=${resultForTypeParameter?.let{ Renderers.RENDER_TYPE.render(it) }}\n" + val parameterType = myDeclarations.getType(typeParameter.getName().asString()) + val resultType = resultingSubstitutor.substitute(parameterType, variance) + result append "${typeParameter.getName()}=${resultType?.let{ Renderers.RENDER_TYPE.render(it) }}\n" } JetTestUtils.assertEqualsToFile(file, "${getConstraintsText(fileText)}${resultingStatus}\n\n${result}\n") @@ -153,7 +147,7 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() { val kind = MyConstraintKind.valueOf(matcher.group(1)!!) val firstType = matcher.group(2)!! val secondType = matcher.group(3)!! - val isWeak = matcher.groupCount() == 4 && matcher.group(4) == "!" + val isWeak = matcher.groupCount() == 4 && matcher.group(4) == "weak" constraints.add(MyConstraint(kind, firstType, secondType, isWeak)) } return constraints diff --git a/compiler/tests/org/jetbrains/jet/resolve/constraintSystem/MyDeclarations.kt b/compiler/tests/org/jetbrains/jet/resolve/constraintSystem/MyDeclarations.kt index 19c5a21fb38..019d862a72d 100644 --- a/compiler/tests/org/jetbrains/jet/resolve/constraintSystem/MyDeclarations.kt +++ b/compiler/tests/org/jetbrains/jet/resolve/constraintSystem/MyDeclarations.kt @@ -47,6 +47,7 @@ import com.intellij.openapi.project.Project import java.util.regex.Pattern import org.jetbrains.jet.lang.resolve.constants.IntegerValueTypeConstructor import org.jetbrains.jet.lang.types.JetTypeImpl +import kotlin.test.assertNotNull public class MyDeclarations( private val context: BindingContext, @@ -58,12 +59,21 @@ public class MyDeclarations( { val functions = context.getSliceContents(BindingContext.FUNCTION) - functionFoo = functions.values().iterator().next() + functionFoo = findFunctionByName(functions.values(), "foo") val function = (BindingContextUtils.descriptorToDeclaration(context, functionFoo) as JetFunction) val fooBody = function.getBodyExpression() scopeToResolveTypeParameters = context.get(BindingContext.RESOLUTION_SCOPE, fooBody)!! } + private fun findFunctionByName(functions: Collection, name: String): FunctionDescriptor { + for (function in functions) { + if (function.getName().asString() == name) { + return function + } + } + throw AssertionError("Function ${name} is not declared") + } + fun getParameterDescriptor(name: String): TypeParameterDescriptor { for (typeParameter in functionFoo.getTypeParameters()) { if (typeParameter.getName().asString() == name) {