generate constraint system tests

more tests added
This commit is contained in:
Svetlana Isakova
2014-01-24 21:09:51 +04:00
parent 791fa22abb
commit 9832676701
27 changed files with 593 additions and 27 deletions
@@ -46,15 +46,16 @@ import java.util.Collections
import com.intellij.openapi.project.Project
import java.util.HashMap
import java.util.regex.Pattern
import org.jetbrains.jet.resolve.constraintSystem.ConstraintSystemTest.MyConstraintKind
import org.jetbrains.jet.resolve.constraintSystem.ConstraintSystemTest.MyConstraint
import org.jetbrains.jet.resolve.constraintSystem.AbstractConstraintSystemTest.MyConstraintKind
import org.jetbrains.jet.resolve.constraintSystem.AbstractConstraintSystemTest.MyConstraint
import java.util.ArrayList
import kotlin.test.assertEquals
import org.junit.Assert
import java.util.LinkedHashMap
public class ConstraintSystemTest() : JetLiteFixture() {
val constraintPattern = Pattern.compile("(SUBTYPE|SUPERTYPE|EQUALITY)\\s+(\\w+)\\s+(\\w+)", Pattern.MULTILINE)
abstract public class AbstractConstraintSystemTest() : JetLiteFixture() {
private val typePattern = """([\w|<|>|\(|\)]+)"""
val constraintPattern = Pattern.compile("""(SUBTYPE|SUPERTYPE|EQUALITY)\s+${typePattern}\s+${typePattern}\s+(!)?""", Pattern.MULTILINE)
val variablesPattern = Pattern.compile("VARIABLES\\s+(.*)")
private var builtIns: KotlinBuiltIns? = null
@@ -92,10 +93,6 @@ public class ConstraintSystemTest() : JetLiteFixture() {
return MyDeclarations(bindingContext, getProject(), typeResolver!!)
}
public fun testSimpleSuccessful() {
doTest("compiler/testData/constraintSystem/simpleSuccessful.bounds")
}
public fun doTest(filePath: String) {
val file = File(filePath)
val fileText = JetTestUtils.doLoadFile(file)!!
@@ -103,7 +100,7 @@ public class ConstraintSystemTest() : JetLiteFixture() {
val constraintSystem = ConstraintSystemImpl()
val myDeclarations = analyzeDeclarations()
val typeParameterDescriptors = HashMap<TypeParameterDescriptor, Variance>()
val typeParameterDescriptors = LinkedHashMap<TypeParameterDescriptor, Variance>()
val variables = parseVariables(fileText)
for (variable in variables) {
typeParameterDescriptors.put(myDeclarations.getParameterDescriptor(variable), Variance.INVARIANT)
@@ -120,20 +117,18 @@ public class ConstraintSystemTest() : JetLiteFixture() {
MyConstraintKind.SUPERTYPE -> constraintSystem.addSupertypeConstraint(firstType, secondType, position)
}
}
constraintSystem.processDeclaredBoundConstraints()
val expectedStatus = parseStatus(fileText)
val resultingStatus = Renderers.RENDER_CONSTRAINT_SYSTEM.render(constraintSystem)
Assert.assertEquals("Constraint system status mismatch", expectedStatus, resultingStatus)
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}"
result append "${typeParameter.getName()}=${resultForTypeParameter?.let{ Renderers.RENDER_TYPE.render(it) }}\n"
}
val expectedResult = parseResult(fileText)
Assert.assertEquals("Constraint system substitution mismatch", expectedResult, result.toString())
JetTestUtils.assertEqualsToFile(file, "${getConstraintsText(fileText)}${resultingStatus}\n\n${result}\n")
}
class MyConstraint(val kind: MyConstraintKind, val firstType: String, val secondType: String, val isWeak: Boolean)
@@ -164,9 +159,7 @@ public class ConstraintSystemTest() : JetLiteFixture() {
return constraints
}
private fun parseStatus(text: String) = text.substring(text.indexOf("type parameter bounds"), text.indexOf("result")).trim()
private fun parseResult(text: String) = text.substring(text.indexOf("result"), text.length())
private fun getConstraintsText(text: String) = text.substring(0, text.indexOf("type parameter bounds"))
}
@@ -0,0 +1,195 @@
/*
* Copyright 2010-2014 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.constraintSystem;
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.constraintSystem.AbstractConstraintSystemTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/constraintSystem")
@InnerTestClasses({ConstraintSystemTestGenerated.CheckStatus.class, ConstraintSystemTestGenerated.ComputeValues.class, ConstraintSystemTestGenerated.IntegerValueTypes.class, ConstraintSystemTestGenerated.SeveralVariables.class, ConstraintSystemTestGenerated.Variance.class})
public class ConstraintSystemTestGenerated extends AbstractConstraintSystemTest {
public void testAllFilesPresentInConstraintSystem() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/constraintSystem"), Pattern.compile("^(.+)\\.bounds$"), true);
}
@TestMetadata("compiler/testData/constraintSystem/checkStatus")
public static class CheckStatus extends AbstractConstraintSystemTest {
public void testAllFilesPresentInCheckStatus() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/constraintSystem/checkStatus"), Pattern.compile("^(.+)\\.bounds$"), true);
}
@TestMetadata("conflictingConstraints.bounds")
public void testConflictingConstraints() throws Exception {
doTest("compiler/testData/constraintSystem/checkStatus/conflictingConstraints.bounds");
}
@TestMetadata("successful.bounds")
public void testSuccessful() throws Exception {
doTest("compiler/testData/constraintSystem/checkStatus/successful.bounds");
}
@TestMetadata("typeConstructorMismatch.bounds")
public void testTypeConstructorMismatch() throws Exception {
doTest("compiler/testData/constraintSystem/checkStatus/typeConstructorMismatch.bounds");
}
@TestMetadata("unknownParameters.bounds")
public void testUnknownParameters() throws Exception {
doTest("compiler/testData/constraintSystem/checkStatus/unknownParameters.bounds");
}
@TestMetadata("violatedUpperBound.bounds")
public void testViolatedUpperBound() throws Exception {
doTest("compiler/testData/constraintSystem/checkStatus/violatedUpperBound.bounds");
}
}
@TestMetadata("compiler/testData/constraintSystem/computeValues")
public static class ComputeValues extends AbstractConstraintSystemTest {
public void testAllFilesPresentInComputeValues() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/constraintSystem/computeValues"), Pattern.compile("^(.+)\\.bounds$"), true);
}
@TestMetadata("contradiction.bounds")
public void testContradiction() throws Exception {
doTest("compiler/testData/constraintSystem/computeValues/contradiction.bounds");
}
@TestMetadata("subTypeOfUpperBounds.bounds")
public void testSubTypeOfUpperBounds() throws Exception {
doTest("compiler/testData/constraintSystem/computeValues/subTypeOfUpperBounds.bounds");
}
@TestMetadata("superTypeOfLowerBounds1.bounds")
public void testSuperTypeOfLowerBounds1() throws Exception {
doTest("compiler/testData/constraintSystem/computeValues/superTypeOfLowerBounds1.bounds");
}
@TestMetadata("superTypeOfLowerBounds2.bounds")
public void testSuperTypeOfLowerBounds2() throws Exception {
doTest("compiler/testData/constraintSystem/computeValues/superTypeOfLowerBounds2.bounds");
}
}
@TestMetadata("compiler/testData/constraintSystem/integerValueTypes")
public static class IntegerValueTypes extends AbstractConstraintSystemTest {
public void testAllFilesPresentInIntegerValueTypes() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/constraintSystem/integerValueTypes"), Pattern.compile("^(.+)\\.bounds$"), true);
}
@TestMetadata("byteOverflow.bounds")
public void testByteOverflow() throws Exception {
doTest("compiler/testData/constraintSystem/integerValueTypes/byteOverflow.bounds");
}
@TestMetadata("defaultLong.bounds")
public void testDefaultLong() throws Exception {
doTest("compiler/testData/constraintSystem/integerValueTypes/defaultLong.bounds");
}
@TestMetadata("numberAndAny.bounds")
public void testNumberAndAny() throws Exception {
doTest("compiler/testData/constraintSystem/integerValueTypes/numberAndAny.bounds");
}
@TestMetadata("numberAndString.bounds")
public void testNumberAndString() throws Exception {
doTest("compiler/testData/constraintSystem/integerValueTypes/numberAndString.bounds");
}
@TestMetadata("severalNumbers.bounds")
public void testSeveralNumbers() throws Exception {
doTest("compiler/testData/constraintSystem/integerValueTypes/severalNumbers.bounds");
}
@TestMetadata("simpleByte.bounds")
public void testSimpleByte() throws Exception {
doTest("compiler/testData/constraintSystem/integerValueTypes/simpleByte.bounds");
}
@TestMetadata("simpleInt.bounds")
public void testSimpleInt() throws Exception {
doTest("compiler/testData/constraintSystem/integerValueTypes/simpleInt.bounds");
}
@TestMetadata("simpleShort.bounds")
public void testSimpleShort() throws Exception {
doTest("compiler/testData/constraintSystem/integerValueTypes/simpleShort.bounds");
}
}
@TestMetadata("compiler/testData/constraintSystem/severalVariables")
public static class SeveralVariables extends AbstractConstraintSystemTest {
public void testAllFilesPresentInSeveralVariables() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/constraintSystem/severalVariables"), Pattern.compile("^(.+)\\.bounds$"), true);
}
@TestMetadata("simpleDependency.bounds")
public void testSimpleDependency() throws Exception {
doTest("compiler/testData/constraintSystem/severalVariables/simpleDependency.bounds");
}
}
@TestMetadata("compiler/testData/constraintSystem/variance")
public static class Variance extends AbstractConstraintSystemTest {
public void testAllFilesPresentInVariance() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/constraintSystem/variance"), Pattern.compile("^(.+)\\.bounds$"), true);
}
@TestMetadata("consumer.bounds")
public void testConsumer() throws Exception {
doTest("compiler/testData/constraintSystem/variance/consumer.bounds");
}
@TestMetadata("invariant.bounds")
public void testInvariant() throws Exception {
doTest("compiler/testData/constraintSystem/variance/invariant.bounds");
}
@TestMetadata("producer.bounds")
public void testProducer() throws Exception {
doTest("compiler/testData/constraintSystem/variance/producer.bounds");
}
}
public static Test suite() {
TestSuite suite = new TestSuite("ConstraintSystemTestGenerated");
suite.addTestSuite(ConstraintSystemTestGenerated.class);
suite.addTestSuite(CheckStatus.class);
suite.addTestSuite(ComputeValues.class);
suite.addTestSuite(IntegerValueTypes.class);
suite.addTestSuite(SeveralVariables.class);
suite.addTestSuite(Variance.class);
return suite;
}
}
@@ -44,6 +44,9 @@ import java.io.File
import java.io.IOException
import java.util.Collections
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
public class MyDeclarations(
private val context: BindingContext,
@@ -70,7 +73,16 @@ public class MyDeclarations(
throw AssertionError("Unsupported type parameter name: " + name + ".")
}
fun getType(name: String) = typeResolver.resolveType(
fun getType(name: String): JetType {
val matcher = INTEGER_VALUE_TYPE_PATTERN.matcher(name)
if (matcher.find()) {
val number = matcher.group(1)!!
return JetTypeImpl(IntegerValueTypeConstructor(number.toLong()), JetScope.EMPTY)
}
return typeResolver.resolveType(
scopeToResolveTypeParameters, JetPsiFactory.createType(project, name),
JetTestUtils.DUMMY_TRACE, true)
}
}
private val INTEGER_VALUE_TYPE_PATTERN = Pattern.compile("""IntegerValueType\((\d*)\)""")