Rename: MyDeclarations -> ConstraintSystemTestData

Simplified code (used 'firstOrNull')
This commit is contained in:
Svetlana Isakova
2014-12-01 21:37:14 +03:00
parent 4df8c250ab
commit e798b7c6d1
2 changed files with 16 additions and 24 deletions
@@ -44,9 +44,9 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() {
private val typeResolver: TypeResolver private val typeResolver: TypeResolver
get() = _typeResolver!! get() = _typeResolver!!
private var _myDeclarations: MyDeclarations? = null private var _testDeclarations: ConstraintSystemTestData? = null
private val myDeclarations: MyDeclarations private val testDeclarations: ConstraintSystemTestData
get() = _myDeclarations!! get() = _testDeclarations!!
override fun createEnvironment(): JetCoreEnvironment { override fun createEnvironment(): JetCoreEnvironment {
return createEnvironmentWithMockJdk(ConfigurationKind.ALL) return createEnvironmentWithMockJdk(ConfigurationKind.ALL)
@@ -57,12 +57,12 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() {
val injector = InjectorForTests(getProject(), JetTestUtils.createEmptyModule()) val injector = InjectorForTests(getProject(), JetTestUtils.createEmptyModule())
_typeResolver = injector.getTypeResolver()!! _typeResolver = injector.getTypeResolver()!!
_myDeclarations = analyzeDeclarations() _testDeclarations = analyzeDeclarations()
} }
override fun tearDown() { override fun tearDown() {
_typeResolver = null _typeResolver = null
_myDeclarations = null _testDeclarations = null
super<JetLiteFixture>.tearDown() super<JetLiteFixture>.tearDown()
} }
@@ -70,12 +70,12 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() {
return super.getTestDataPath() + "/constraintSystem/" return super.getTestDataPath() + "/constraintSystem/"
} }
private fun analyzeDeclarations(): MyDeclarations { private fun analyzeDeclarations(): ConstraintSystemTestData {
val fileName = "declarations/declarations.kt" val fileName = "declarations/declarations.kt"
val psiFile = createPsiFile(null, fileName, loadFile(fileName))!! val psiFile = createPsiFile(null, fileName, loadFile(fileName))!!
val bindingContext = JvmResolveUtil.analyzeOneFileWithJavaIntegrationAndCheckForErrors(psiFile).bindingContext val bindingContext = JvmResolveUtil.analyzeOneFileWithJavaIntegrationAndCheckForErrors(psiFile).bindingContext
return MyDeclarations(bindingContext, getProject(), typeResolver) return ConstraintSystemTestData(bindingContext, getProject(), typeResolver)
} }
public fun doTest(filePath: String) { public fun doTest(filePath: String) {
@@ -87,14 +87,14 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() {
val typeParameterDescriptors = LinkedHashMap<TypeParameterDescriptor, Variance>() val typeParameterDescriptors = LinkedHashMap<TypeParameterDescriptor, Variance>()
val variables = parseVariables(fileText) val variables = parseVariables(fileText)
for (variable in variables) { for (variable in variables) {
typeParameterDescriptors.put(myDeclarations.getParameterDescriptor(variable), Variance.INVARIANT) typeParameterDescriptors.put(testDeclarations.getParameterDescriptor(variable), Variance.INVARIANT)
} }
constraintSystem.registerTypeVariables(typeParameterDescriptors) constraintSystem.registerTypeVariables(typeParameterDescriptors)
val constraints = parseConstraints(fileText) val constraints = parseConstraints(fileText)
for (constraint in constraints) { for (constraint in constraints) {
val firstType = myDeclarations.getType(constraint.firstType) val firstType = testDeclarations.getType(constraint.firstType)
val secondType = myDeclarations.getType(constraint.secondType) val secondType = testDeclarations.getType(constraint.secondType)
val position = if (constraint.isWeak) TYPE_BOUND_POSITION.position(0) else SPECIAL.position() val position = if (constraint.isWeak) TYPE_BOUND_POSITION.position(0) else SPECIAL.position()
when (constraint.kind) { when (constraint.kind) {
MyConstraintKind.SUBTYPE -> constraintSystem.addSubtypeConstraint(firstType, secondType, position) MyConstraintKind.SUBTYPE -> constraintSystem.addSubtypeConstraint(firstType, secondType, position)
@@ -108,7 +108,7 @@ 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 parameterType = myDeclarations.getType(typeParameter.getName().asString()) val parameterType = testDeclarations.getType(typeParameter.getName().asString())
val resultType = resultingSubstitutor.substitute(parameterType, variance) val resultType = resultingSubstitutor.substitute(parameterType, variance)
result append "${typeParameter.getName()}=${resultType?.let{ Renderers.RENDER_TYPE.render(it) }}\n" result append "${typeParameter.getName()}=${resultType?.let{ Renderers.RENDER_TYPE.render(it) }}\n"
} }
@@ -29,7 +29,7 @@ import org.jetbrains.jet.lang.resolve.constants.IntegerValueTypeConstructor
import org.jetbrains.jet.lang.types.JetTypeImpl import org.jetbrains.jet.lang.types.JetTypeImpl
import org.jetbrains.jet.lang.descriptors.annotations.Annotations import org.jetbrains.jet.lang.descriptors.annotations.Annotations
public class MyDeclarations( public class ConstraintSystemTestData(
context: BindingContext, context: BindingContext,
private val project: Project, private val project: Project,
private val typeResolver: TypeResolver private val typeResolver: TypeResolver
@@ -46,21 +46,13 @@ public class MyDeclarations(
} }
private fun findFunctionByName(functions: Collection<FunctionDescriptor>, name: String): FunctionDescriptor { private fun findFunctionByName(functions: Collection<FunctionDescriptor>, name: String): FunctionDescriptor {
for (function in functions) { return functions.firstOrNull { it.getName().asString() == name } ?:
if (function.getName().asString() == name) { throw AssertionError("Function ${name} is not declared")
return function
}
}
throw AssertionError("Function ${name} is not declared")
} }
fun getParameterDescriptor(name: String): TypeParameterDescriptor { fun getParameterDescriptor(name: String): TypeParameterDescriptor {
for (typeParameter in functionFoo.getTypeParameters()) { return functionFoo.getTypeParameters().firstOrNull { it.getName().asString() == name } ?:
if (typeParameter.getName().asString() == name) { throw AssertionError("Unsupported type parameter name: " + name + ".")
return typeParameter;
}
}
throw AssertionError("Unsupported type parameter name: " + name + ".")
} }
fun getType(name: String): JetType { fun getType(name: String): JetType {