diff --git a/src/main/kotlin/org/jetbrains/research/testspark/display/ErrorMessageNormalizer.kt b/src/main/kotlin/org/jetbrains/research/testspark/display/ErrorMessageNormalizer.kt new file mode 100644 index 000000000..360162105 --- /dev/null +++ b/src/main/kotlin/org/jetbrains/research/testspark/display/ErrorMessageNormalizer.kt @@ -0,0 +1,46 @@ +package org.jetbrains.research.testspark.display + +/** + * The ErrorMessageNormalizer class is responsible for normalizing error messages by inserting "
" tags after every block size characters. + */ +object ErrorMessageNormalizer { + const val BLOCK_SIZE = 100 + + const val SEPARATOR = "
" + + /** + * Normalizes an error message by inserting "
" tags after every block size characters, + * except if there is already a "
" tag within the blockSize characters. + * If the string length is a multiple of blockSize and the last character is a "
" tag, + * it is removed from the result. + * + * @param error The error message to be normalized. + * @return The normalized error message. + */ + fun normalize(error: String): String { + // init variables + val builder = StringBuilder() + var lastIndex = 0 + + // string separating + while (lastIndex < error.length) { + val nextIndex = (lastIndex + BLOCK_SIZE).coerceAtMost(error.length) + val substring = error.substring(lastIndex, nextIndex) + + if (!substring.contains(SEPARATOR)) { + builder.append(substring).append(SEPARATOR) + } else { + builder.append(substring) + } + + lastIndex = nextIndex + } + + // remove the last
if the string length is a multiple of the block size, and it didn't have
+ if (builder.endsWith(SEPARATOR) && (error.length % BLOCK_SIZE == 0)) { + builder.deleteRange(builder.length - SEPARATOR.length, builder.length) + } + + return builder.toString() + } +} diff --git a/src/main/kotlin/org/jetbrains/research/testspark/display/TestCasePanelFactory.kt b/src/main/kotlin/org/jetbrains/research/testspark/display/TestCasePanelFactory.kt index 5e7620831..55f837959 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/display/TestCasePanelFactory.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/display/TestCasePanelFactory.kt @@ -327,7 +327,7 @@ class TestCasePanelFactory( errorLabel.isVisible = false } else { errorLabel.isVisible = true - errorLabel.toolTipText = error + errorLabel.toolTipText = ErrorMessageNormalizer.normalize(error) } } diff --git a/src/test/kotlin/org/jetbrains/research/testspark/helper/ErrorMessageNormalizerTest.kt b/src/test/kotlin/org/jetbrains/research/testspark/helper/ErrorMessageNormalizerTest.kt new file mode 100644 index 000000000..a608c1413 --- /dev/null +++ b/src/test/kotlin/org/jetbrains/research/testspark/helper/ErrorMessageNormalizerTest.kt @@ -0,0 +1,68 @@ +package org.jetbrains.research.testspark.display + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class ErrorMessageNormalizerTest { + + /** + * Test with a string length less than BLOCK_SIZE and no separators. + */ + @Test + fun testNormalizeWithNoSeparator() { + val input = "a".repeat(ErrorMessageNormalizer.BLOCK_SIZE - 1) + val expected = "a".repeat(ErrorMessageNormalizer.BLOCK_SIZE - 1) + ErrorMessageNormalizer.SEPARATOR + val result = ErrorMessageNormalizer.normalize(input) + assertEquals(expected, result) + } + + /** + * Test with a string length exactly equal to BLOCK_SIZE and no separators. + */ + @Test + fun testNormalizeWithExactBlockSizeNoSeparator() { + val input = "a".repeat(ErrorMessageNormalizer.BLOCK_SIZE) + val expected = "a".repeat(ErrorMessageNormalizer.BLOCK_SIZE) + val result = ErrorMessageNormalizer.normalize(input) + assertEquals(expected, result) + } + + /** + * Test with a string length greater than BLOCK_SIZE and no separators. + */ + @Test + fun testNormalizeWithMultipleBlocksNoSeparator() { + val input = "a".repeat(ErrorMessageNormalizer.BLOCK_SIZE * 2 + 50) + val expected = "a".repeat(ErrorMessageNormalizer.BLOCK_SIZE) + ErrorMessageNormalizer.SEPARATOR + + "a".repeat(ErrorMessageNormalizer.BLOCK_SIZE) + ErrorMessageNormalizer.SEPARATOR + + "a".repeat(50) + ErrorMessageNormalizer.SEPARATOR + val result = ErrorMessageNormalizer.normalize(input) + assertEquals(expected, result) + } + + /** + * Test with an empty string. + */ + @Test + fun testNormalizeEmptyString() { + val input = "" + val expected = "" + val result = ErrorMessageNormalizer.normalize(input) + assertEquals(expected, result) + } + + /** + * Test with a string containing multiple separators. + */ + @Test + fun testNormalizeWithMultipleSeparators() { + val input = "a".repeat(40) + ErrorMessageNormalizer.SEPARATOR + + "b".repeat(40) + ErrorMessageNormalizer.SEPARATOR + + "c".repeat(20) + val expected = "a".repeat(40) + ErrorMessageNormalizer.SEPARATOR + + "b".repeat(40) + ErrorMessageNormalizer.SEPARATOR + + "c".repeat(20) + ErrorMessageNormalizer.SEPARATOR + val result = ErrorMessageNormalizer.normalize(input) + assertEquals(expected, result) + } +}