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
get() = _typeResolver!!
private var _myDeclarations: MyDeclarations? = null
private val myDeclarations: MyDeclarations
get() = _myDeclarations!!
private var _testDeclarations: ConstraintSystemTestData? = null
private val testDeclarations: ConstraintSystemTestData
get() = _testDeclarations!!
override fun createEnvironment(): JetCoreEnvironment {
return createEnvironmentWithMockJdk(ConfigurationKind.ALL)
@@ -57,12 +57,12 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() {
val injector = InjectorForTests(getProject(), JetTestUtils.createEmptyModule())
_typeResolver = injector.getTypeResolver()!!
_myDeclarations = analyzeDeclarations()
_testDeclarations = analyzeDeclarations()
}
override fun tearDown() {
_typeResolver = null
_myDeclarations = null
_testDeclarations = null
super<JetLiteFixture>.tearDown()
}
@@ -70,12 +70,12 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() {
return super.getTestDataPath() + "/constraintSystem/"
}
private fun analyzeDeclarations(): MyDeclarations {
private fun analyzeDeclarations(): ConstraintSystemTestData {
val fileName = "declarations/declarations.kt"
val psiFile = createPsiFile(null, fileName, loadFile(fileName))!!
val bindingContext = JvmResolveUtil.analyzeOneFileWithJavaIntegrationAndCheckForErrors(psiFile).bindingContext
return MyDeclarations(bindingContext, getProject(), typeResolver)
return ConstraintSystemTestData(bindingContext, getProject(), typeResolver)
}
public fun doTest(filePath: String) {
@@ -87,14 +87,14 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() {
val typeParameterDescriptors = LinkedHashMap<TypeParameterDescriptor, Variance>()
val variables = parseVariables(fileText)
for (variable in variables) {
typeParameterDescriptors.put(myDeclarations.getParameterDescriptor(variable), Variance.INVARIANT)
typeParameterDescriptors.put(testDeclarations.getParameterDescriptor(variable), Variance.INVARIANT)
}
constraintSystem.registerTypeVariables(typeParameterDescriptors)
val constraints = parseConstraints(fileText)
for (constraint in constraints) {
val firstType = myDeclarations.getType(constraint.firstType)
val secondType = myDeclarations.getType(constraint.secondType)
val firstType = testDeclarations.getType(constraint.firstType)
val secondType = testDeclarations.getType(constraint.secondType)
val position = if (constraint.isWeak) TYPE_BOUND_POSITION.position(0) else SPECIAL.position()
when (constraint.kind) {
MyConstraintKind.SUBTYPE -> constraintSystem.addSubtypeConstraint(firstType, secondType, position)
@@ -108,7 +108,7 @@ abstract public class AbstractConstraintSystemTest() : JetLiteFixture() {
val resultingSubstitutor = constraintSystem.getResultingSubstitutor()
val result = StringBuilder() append "result:\n"
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)
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.descriptors.annotations.Annotations
public class MyDeclarations(
public class ConstraintSystemTestData(
context: BindingContext,
private val project: Project,
private val typeResolver: TypeResolver
@@ -46,21 +46,13 @@ public class MyDeclarations(
}
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")
return functions.firstOrNull { it.getName().asString() == name } ?:
throw AssertionError("Function ${name} is not declared")
}
fun getParameterDescriptor(name: String): TypeParameterDescriptor {
for (typeParameter in functionFoo.getTypeParameters()) {
if (typeParameter.getName().asString() == name) {
return typeParameter;
}
}
throw AssertionError("Unsupported type parameter name: " + name + ".")
return functionFoo.getTypeParameters().firstOrNull { it.getName().asString() == name } ?:
throw AssertionError("Unsupported type parameter name: " + name + ".")
}
fun getType(name: String): JetType {