-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #224 from JetBrains-Research/arksap2002/bugs/fix-e…
…rror-window-size Fix error message window size
- Loading branch information
Showing
3 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/org/jetbrains/research/testspark/display/ErrorMessageNormalizer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.jetbrains.research.testspark.display | ||
|
||
/** | ||
* The ErrorMessageNormalizer class is responsible for normalizing error messages by inserting "<br/>" tags after every block size characters. | ||
*/ | ||
object ErrorMessageNormalizer { | ||
const val BLOCK_SIZE = 100 | ||
|
||
const val SEPARATOR = "<br/>" | ||
|
||
/** | ||
* Normalizes an error message by inserting "<br/>" tags after every block size characters, | ||
* except if there is already a "<br/>" tag within the blockSize characters. | ||
* If the string length is a multiple of blockSize and the last character is a "<br/>" 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 <br/> if the string length is a multiple of the block size, and it didn't have <br/> | ||
if (builder.endsWith(SEPARATOR) && (error.length % BLOCK_SIZE == 0)) { | ||
builder.deleteRange(builder.length - SEPARATOR.length, builder.length) | ||
} | ||
|
||
return builder.toString() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/test/kotlin/org/jetbrains/research/testspark/helper/ErrorMessageNormalizerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |