From 1cf6f9baebf9059b056a3012746f6ea1f0ae0728 Mon Sep 17 00:00:00 2001 From: Arkadii Sapozhnikov Date: Tue, 28 May 2024 00:56:09 +0200 Subject: [PATCH 1/4] feat: add ErrorMessageNormalizer --- .../display/ErrorMessageNormalizer.kt | 43 +++++++++++++++++++ .../testspark/display/TestCasePanelFactory.kt | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/org/jetbrains/research/testspark/display/ErrorMessageNormalizer.kt 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..06e5716fd --- /dev/null +++ b/src/main/kotlin/org/jetbrains/research/testspark/display/ErrorMessageNormalizer.kt @@ -0,0 +1,43 @@ +package org.jetbrains.research.testspark.display + +object ErrorMessageNormalizer { + /** + * Normalizes an error message by inserting "
" tags after every 300 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 { + // set default parameters + val blockSize = 100 + val separator = "
" + + // init variables + val builder = StringBuilder() + var lastIndex = 0 + + // string separating + while (lastIndex < error.length) { + val nextIndex = (lastIndex + blockSize).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 300 and it didn't have
+ if (builder.endsWith(separator) && (error.length % blockSize == 0)) { + builder.setLength(builder.length - separator.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 341dc95fd..281badf41 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/display/TestCasePanelFactory.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/display/TestCasePanelFactory.kt @@ -319,7 +319,7 @@ class TestCasePanelFactory( errorLabel.isVisible = false } else { errorLabel.isVisible = true - errorLabel.toolTipText = error + errorLabel.toolTipText = ErrorMessageNormalizer.normalize(error) } } From 21dc4c400790d767881e7c6332f715b6e3bc3796 Mon Sep 17 00:00:00 2001 From: Arkadii Sapozhnikov Date: Wed, 29 May 2024 22:16:11 +0200 Subject: [PATCH 2/4] fix: improve ErrorMessageNormalizer --- .../research/testspark/display/ErrorMessageNormalizer.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/org/jetbrains/research/testspark/display/ErrorMessageNormalizer.kt b/src/main/kotlin/org/jetbrains/research/testspark/display/ErrorMessageNormalizer.kt index 06e5716fd..c51f01d80 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/display/ErrorMessageNormalizer.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/display/ErrorMessageNormalizer.kt @@ -2,7 +2,7 @@ package org.jetbrains.research.testspark.display object ErrorMessageNormalizer { /** - * Normalizes an error message by inserting "
" tags after every 300 characters, + * 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. @@ -33,9 +33,9 @@ object ErrorMessageNormalizer { lastIndex = nextIndex } - // remove the last
if the string length is a multiple of 300 and it didn't have
+ // 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 % blockSize == 0)) { - builder.setLength(builder.length - separator.length) + builder.deleteRange(builder.length - separator.length, builder.length) } return builder.toString() From 3e94877eb914e5264dcf36cb84278dadab6617a8 Mon Sep 17 00:00:00 2001 From: Arkadii Sapozhnikov Date: Wed, 29 May 2024 22:29:05 +0200 Subject: [PATCH 3/4] improvements: create ErrorMessageNormalizerTest --- .../display/ErrorMessageNormalizer.kt | 21 +++--- .../helper/ErrorMessageNormalizerTest.kt | 68 +++++++++++++++++++ 2 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 src/test/kotlin/org/jetbrains/research/testspark/helper/ErrorMessageNormalizerTest.kt diff --git a/src/main/kotlin/org/jetbrains/research/testspark/display/ErrorMessageNormalizer.kt b/src/main/kotlin/org/jetbrains/research/testspark/display/ErrorMessageNormalizer.kt index c51f01d80..360162105 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/display/ErrorMessageNormalizer.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/display/ErrorMessageNormalizer.kt @@ -1,6 +1,13 @@ 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. @@ -11,21 +18,17 @@ object ErrorMessageNormalizer { * @return The normalized error message. */ fun normalize(error: String): String { - // set default parameters - val blockSize = 100 - val separator = "
" - // init variables val builder = StringBuilder() var lastIndex = 0 // string separating while (lastIndex < error.length) { - val nextIndex = (lastIndex + blockSize).coerceAtMost(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) + if (!substring.contains(SEPARATOR)) { + builder.append(substring).append(SEPARATOR) } else { builder.append(substring) } @@ -34,8 +37,8 @@ object ErrorMessageNormalizer { } // 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 % blockSize == 0)) { - builder.deleteRange(builder.length - separator.length, builder.length) + if (builder.endsWith(SEPARATOR) && (error.length % BLOCK_SIZE == 0)) { + builder.deleteRange(builder.length - SEPARATOR.length, builder.length) } return builder.toString() 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..f521fbd32 --- /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) + } +} From 0f35627e0e373274444e1241097c14f3391740b6 Mon Sep 17 00:00:00 2001 From: Arkadii Sapozhnikov Date: Wed, 29 May 2024 22:29:59 +0200 Subject: [PATCH 4/4] fix: ktlint issues --- .../testspark/helper/ErrorMessageNormalizerTest.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/kotlin/org/jetbrains/research/testspark/helper/ErrorMessageNormalizerTest.kt b/src/test/kotlin/org/jetbrains/research/testspark/helper/ErrorMessageNormalizerTest.kt index f521fbd32..a608c1413 100644 --- a/src/test/kotlin/org/jetbrains/research/testspark/helper/ErrorMessageNormalizerTest.kt +++ b/src/test/kotlin/org/jetbrains/research/testspark/helper/ErrorMessageNormalizerTest.kt @@ -34,8 +34,8 @@ class ErrorMessageNormalizerTest { 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 + "a".repeat(ErrorMessageNormalizer.BLOCK_SIZE) + ErrorMessageNormalizer.SEPARATOR + + "a".repeat(50) + ErrorMessageNormalizer.SEPARATOR val result = ErrorMessageNormalizer.normalize(input) assertEquals(expected, result) } @@ -57,11 +57,11 @@ class ErrorMessageNormalizerTest { @Test fun testNormalizeWithMultipleSeparators() { val input = "a".repeat(40) + ErrorMessageNormalizer.SEPARATOR + - "b".repeat(40) + ErrorMessageNormalizer.SEPARATOR + - "c".repeat(20) + "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 + "b".repeat(40) + ErrorMessageNormalizer.SEPARATOR + + "c".repeat(20) + ErrorMessageNormalizer.SEPARATOR val result = ErrorMessageNormalizer.normalize(input) assertEquals(expected, result) }