Improve test exceptions fixation:

- exceptions is analyzed if it's specified explicitly only,
- compute test case number for diagnostic tests in which an exception is thrown.
This commit is contained in:
victor.petukhov
2018-12-28 15:43:37 +03:00
parent 317da3d481
commit 06b5a42d0d
45 changed files with 117 additions and 59 deletions
@@ -13,14 +13,20 @@ import java.io.PrintWriter
import java.util.regex.Matcher
import java.util.regex.Pattern
enum class TestsExceptionFilePostfix(val text: String) {
enum class TestsExceptionType(val postfix: String) {
COMPILER_ERROR("compiler"),
COMPILETIME_ERROR("compiletime"),
RUNTIME_ERROR("runtime"),
INFRASTRUCTURE_ERROR("infrastructure")
INFRASTRUCTURE_ERROR("infrastructure");
companion object {
private val map = values().associateBy(TestsExceptionType::postfix)
fun fromValue(type: String) = map[type]
}
}
sealed class TestsError(val original: Throwable, val postfix: TestsExceptionFilePostfix) : Error() {
sealed class TestsError(val original: Throwable, val type: TestsExceptionType) : Error() {
override fun toString(): String = original.toString()
override fun getStackTrace(): Array<out StackTraceElement> = original.stackTrace
override fun initCause(cause: Throwable?): Throwable = original.initCause(cause)
@@ -38,10 +44,10 @@ sealed class TestsError(val original: Throwable, val postfix: TestsExceptionFile
override fun printStackTrace(s: PrintWriter?) = original.printStackTrace(s)
}
class TestsCompilerError(original: Throwable) : TestsError(original, TestsExceptionFilePostfix.COMPILER_ERROR)
class TestsInfrastructureError(original: Throwable) : TestsError(original, TestsExceptionFilePostfix.INFRASTRUCTURE_ERROR)
class TestsCompiletimeError(original: Throwable) : TestsError(original, TestsExceptionFilePostfix.COMPILETIME_ERROR)
class TestsRuntimeError(original: Throwable) : TestsError(original, TestsExceptionFilePostfix.RUNTIME_ERROR)
class TestsCompilerError(original: Throwable) : TestsError(original, TestsExceptionType.COMPILER_ERROR)
class TestsInfrastructureError(original: Throwable) : TestsError(original, TestsExceptionType.INFRASTRUCTURE_ERROR)
class TestsCompiletimeError(original: Throwable) : TestsError(original, TestsExceptionType.COMPILETIME_ERROR)
class TestsRuntimeError(original: Throwable) : TestsError(original, TestsExceptionType.RUNTIME_ERROR)
private enum class ExceptionType {
ANALYZING_EXPRESSION,
@@ -71,8 +77,8 @@ class TestExceptionsComparator(wholeFile: File) {
return null
}
private fun getExceptionInfo(e: TestsError, cases: Set<Int>?): String {
val casesAsString = cases?.run { "CASES: " + joinToString() + ls } ?: ""
private fun getExceptionInfo(e: TestsError, exceptionByCases: Set<Int>?): String {
val casesAsString = exceptionByCases?.run { "CASES: " + joinToString() + ls } ?: ""
return when (e) {
is TestsRuntimeError ->
@@ -84,28 +90,35 @@ class TestExceptionsComparator(wholeFile: File) {
}
private fun validateExistingExceptionFiles(e: TestsError?) {
val postfixesOfFilesToCheck = TestsExceptionFilePostfix.values().toMutableSet().filter { it != e?.postfix }
val postfixesOfFilesToCheck = TestsExceptionType.values().toMutableSet().filter { it != e?.type }
postfixesOfFilesToCheck.forEach {
if (File("$filePathPrefix.${it.text}.txt").exists())
Assert.fail("No $it, but file $filePathPrefix.${it.text}.txt exists.")
if (File("$filePathPrefix.${it.postfix}.txt").exists())
Assert.fail("No $it, but file $filePathPrefix.${it.postfix}.txt exists.")
}
}
fun runAndCompareWithExpected(checkUnexpectedBehaviour: ((Matcher?) -> Pair<Boolean, Set<Int>?>)? = null, runnable: () -> Unit) {
fun run(
expectedException: TestsExceptionType?,
exceptionByCases: Map<Int, TestsExceptionType?> = mapOf(),
computeExceptionPoint: ((Matcher?) -> Set<Int>?)? = null,
runnable: () -> Unit
) {
try {
runnable()
} catch (e: TestsError) {
val exceptionInfo = analyze(e.original)
val unexpectedBehaviourCheckResult = checkUnexpectedBehaviour?.invoke(exceptionInfo)
val analyzeResult = analyze(e.original)
val casesWithExpectedException =
computeExceptionPoint?.invoke(analyzeResult)?.filter { exceptionByCases[it] == e.type }?.toSet()
if (e is TestsCompilerError && unexpectedBehaviourCheckResult?.first == false)
throw e.original
if (casesWithExpectedException == null && e.type != expectedException) {
throw e
}
val exceptionsFile = File("$filePathPrefix.${e.postfix.text}.txt")
val exceptionsFile = File("$filePathPrefix.${e.type.postfix}.txt")
try {
KotlinTestUtils.assertEqualsToFile(exceptionsFile, getExceptionInfo(e, unexpectedBehaviourCheckResult?.second))
KotlinTestUtils.assertEqualsToFile(exceptionsFile, getExceptionInfo(e, casesWithExpectedException))
} catch (t: AssertionError) {
e.original.printStackTrace()
throw t