Skip to content

Commit

Permalink
test: add tet for code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 7, 2024
1 parent 01e3e30 commit c2ec233
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ data class CodeComment(
* @param content the comment content to be re-indented
* @return the re-indented comment content
*/
private fun reIndentComment(content: String): String {
fun reIndentComment(content: String): String {
val lines = content.split("\n")
val indent = lines[1].takeWhile { it == ' ' }
val linesWithoutIndent = lines.map { it.removePrefix(indent) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package cc.unitmesh.quality.documentation

import chapi.domain.core.CodePosition
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class CodeCommentTest {

@Test
fun should_reIndentComment() {
// Given
val commentContent = """
| This is a comment.
| It has multiple lines.
| And some indented lines.
""".trimMargin()

// When
val reIndentedComment = CodeComment.reIndentComment(commentContent)

// Then
val expectedComment = """
|This is a comment.
| It has multiple lines.
| And some indented lines.
""".trimMargin()

assertEquals(expectedComment, reIndentedComment)
}

@Test
fun should_extractKotlinComment() {
// Given
val code = """
|fun main() {
| /**
| * This is a comment.
| * It has multiple lines.
| */
| println("Hello, World!")
|}
""".trimMargin()

// When
val extractedComments = CodeComment.extractKotlinComment(code)

// Then
val expectedComment = CodeComment(
"""
|/**
| * This is a comment.
| * It has multiple lines.
| */
""".trimMargin(),
CodePosition(2, 4, 5, 6)
)

assertEquals(listOf(expectedComment), extractedComments)
}
}

0 comments on commit c2ec233

Please sign in to comment.