improvements after review in constraint system tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
VARIABLES T
|
VARIABLES T
|
||||||
|
|
||||||
SUBTYPE T Int
|
SUBTYPE T Int
|
||||||
SUBTYPE T String !
|
SUBTYPE T String weak
|
||||||
|
|
||||||
type parameter bounds:
|
type parameter bounds:
|
||||||
T <: jet.Int(SPECIAL), <: jet.String(TYPE_BOUND_POSITION(0))
|
T <: jet.Int(SPECIAL), <: jet.String(TYPE_BOUND_POSITION(0))
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
VARIABLES T
|
VARIABLES T
|
||||||
|
|
||||||
|
SUPERTYPE T IntegerValueType(1)
|
||||||
SUPERTYPE T IntegerValueType(10000000000)
|
SUPERTYPE T IntegerValueType(10000000000)
|
||||||
|
|
||||||
type parameter bounds:
|
type parameter bounds:
|
||||||
T >: IntegerValueType(10000000000)(SPECIAL)
|
T >: IntegerValueType(1)(SPECIAL), >: IntegerValueType(10000000000)(SPECIAL)
|
||||||
status:
|
status:
|
||||||
-hasConflictingConstraints: false
|
-hasConflictingConstraints: false
|
||||||
-hasContradiction: false
|
-hasContradiction: false
|
||||||
|
|||||||
+11
-17
@@ -52,17 +52,15 @@ import java.util.ArrayList
|
|||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
import org.junit.Assert
|
import org.junit.Assert
|
||||||
import java.util.LinkedHashMap
|
import java.util.LinkedHashMap
|
||||||
|
import kotlin.properties.Delegates
|
||||||
|
|
||||||
abstract public class AbstractConstraintSystemTest() : JetLiteFixture() {
|
abstract public class AbstractConstraintSystemTest() : JetLiteFixture() {
|
||||||
private val typePattern = """([\w|<|>|\(|\)]+)"""
|
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+(.*)")
|
val variablesPattern = Pattern.compile("VARIABLES\\s+(.*)")
|
||||||
|
|
||||||
private var builtIns: KotlinBuiltIns? = null
|
private var typeResolver: TypeResolver by Delegates.notNull()
|
||||||
private var typeResolver: TypeResolver? = null
|
private var myDeclarations: MyDeclarations by Delegates.notNull()
|
||||||
private var expressionTypingServices: ExpressionTypingServices? = null
|
|
||||||
private var functionDescriptor: FunctionDescriptor? = null
|
|
||||||
private var scopeToResolveTypeParameters: JetScope? = null
|
|
||||||
|
|
||||||
override fun createEnvironment(): JetCoreEnvironment {
|
override fun createEnvironment(): JetCoreEnvironment {
|
||||||
return createEnvironmentWithMockJdk(ConfigurationKind.ALL)
|
return createEnvironmentWithMockJdk(ConfigurationKind.ALL)
|
||||||
@@ -71,13 +69,9 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() {
|
|||||||
override fun setUp() {
|
override fun setUp() {
|
||||||
super.setUp()
|
super.setUp()
|
||||||
|
|
||||||
builtIns = KotlinBuiltIns.getInstance()
|
|
||||||
|
|
||||||
val injector = InjectorForTests(getProject(), JetTestUtils.createEmptyModule()!!)
|
val injector = InjectorForTests(getProject(), JetTestUtils.createEmptyModule()!!)
|
||||||
typeResolver = injector.getTypeResolver()
|
typeResolver = injector.getTypeResolver()!!
|
||||||
expressionTypingServices = injector.getExpressionTypingServices()
|
myDeclarations = analyzeDeclarations()
|
||||||
|
|
||||||
analyzeDeclarations()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getTestDataPath(): String {
|
override fun getTestDataPath(): String {
|
||||||
@@ -90,7 +84,7 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() {
|
|||||||
val psiFile = createPsiFile(null, fileName, loadFile(fileName))
|
val psiFile = createPsiFile(null, fileName, loadFile(fileName))
|
||||||
val analyzeExhaust = AnalyzerFacadeForJVM.analyzeOneFileWithJavaIntegration(psiFile, Collections.emptyList<AnalyzerScriptParameter>())!!
|
val analyzeExhaust = AnalyzerFacadeForJVM.analyzeOneFileWithJavaIntegration(psiFile, Collections.emptyList<AnalyzerScriptParameter>())!!
|
||||||
val bindingContext = analyzeExhaust.getBindingContext()
|
val bindingContext = analyzeExhaust.getBindingContext()
|
||||||
return MyDeclarations(bindingContext, getProject(), typeResolver!!)
|
return MyDeclarations(bindingContext, getProject(), typeResolver)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun doTest(filePath: String) {
|
public fun doTest(filePath: String) {
|
||||||
@@ -98,7 +92,6 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() {
|
|||||||
val fileText = JetTestUtils.doLoadFile(file)!!
|
val fileText = JetTestUtils.doLoadFile(file)!!
|
||||||
|
|
||||||
val constraintSystem = ConstraintSystemImpl()
|
val constraintSystem = ConstraintSystemImpl()
|
||||||
val myDeclarations = analyzeDeclarations()
|
|
||||||
|
|
||||||
val typeParameterDescriptors = LinkedHashMap<TypeParameterDescriptor, Variance>()
|
val typeParameterDescriptors = LinkedHashMap<TypeParameterDescriptor, Variance>()
|
||||||
val variables = parseVariables(fileText)
|
val variables = parseVariables(fileText)
|
||||||
@@ -124,8 +117,9 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() {
|
|||||||
val resultingSubstitutor = constraintSystem.getResultingSubstitutor()
|
val resultingSubstitutor = constraintSystem.getResultingSubstitutor()
|
||||||
val result = StringBuilder() append "result:\n"
|
val result = StringBuilder() append "result:\n"
|
||||||
for ((typeParameter, variance) in typeParameterDescriptors) {
|
for ((typeParameter, variance) in typeParameterDescriptors) {
|
||||||
val resultForTypeParameter = resultingSubstitutor.substitute(myDeclarations.getType(typeParameter.getName().asString()), variance)
|
val parameterType = myDeclarations.getType(typeParameter.getName().asString())
|
||||||
result append "${typeParameter.getName()}=${resultForTypeParameter?.let{ Renderers.RENDER_TYPE.render(it) }}\n"
|
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")
|
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 kind = MyConstraintKind.valueOf(matcher.group(1)!!)
|
||||||
val firstType = matcher.group(2)!!
|
val firstType = matcher.group(2)!!
|
||||||
val secondType = matcher.group(3)!!
|
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))
|
constraints.add(MyConstraint(kind, firstType, secondType, isWeak))
|
||||||
}
|
}
|
||||||
return constraints
|
return constraints
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ import com.intellij.openapi.project.Project
|
|||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
import org.jetbrains.jet.lang.resolve.constants.IntegerValueTypeConstructor
|
import org.jetbrains.jet.lang.resolve.constants.IntegerValueTypeConstructor
|
||||||
import org.jetbrains.jet.lang.types.JetTypeImpl
|
import org.jetbrains.jet.lang.types.JetTypeImpl
|
||||||
|
import kotlin.test.assertNotNull
|
||||||
|
|
||||||
public class MyDeclarations(
|
public class MyDeclarations(
|
||||||
private val context: BindingContext,
|
private val context: BindingContext,
|
||||||
@@ -58,12 +59,21 @@ public class MyDeclarations(
|
|||||||
|
|
||||||
{
|
{
|
||||||
val functions = context.getSliceContents(BindingContext.FUNCTION)
|
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 function = (BindingContextUtils.descriptorToDeclaration(context, functionFoo) as JetFunction)
|
||||||
val fooBody = function.getBodyExpression()
|
val fooBody = function.getBodyExpression()
|
||||||
scopeToResolveTypeParameters = context.get(BindingContext.RESOLUTION_SCOPE, fooBody)!!
|
scopeToResolveTypeParameters = context.get(BindingContext.RESOLUTION_SCOPE, fooBody)!!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun findFunctionByName(functions: Collection<FunctionDescriptor>, 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 {
|
fun getParameterDescriptor(name: String): TypeParameterDescriptor {
|
||||||
for (typeParameter in functionFoo.getTypeParameters()) {
|
for (typeParameter in functionFoo.getTypeParameters()) {
|
||||||
if (typeParameter.getName().asString() == name) {
|
if (typeParameter.getName().asString() == name) {
|
||||||
|
|||||||
Reference in New Issue
Block a user