Skip to content

Commit

Permalink
Merge branch 'development' into ebraun/bugs/psi-related-bugs/top-leve…
Browse files Browse the repository at this point in the history
…l-function
  • Loading branch information
arksap2002 committed Aug 19, 2024
2 parents b910d9d + a986088 commit 9b995da
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class TestCasePanelFactory(
errorLabel.isVisible = false
} else {
errorLabel.isVisible = true
errorLabel.toolTipText = error
errorLabel.toolTipText = ErrorMessageNormalizer.normalize(error)
}
}

Expand Down
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)
}
}

0 comments on commit 9b995da

Please sign in to comment.