From c2ec233ca79ceee793ee45ea8fbd3f0f891f60a3 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Sun, 7 Jan 2024 15:09:48 +0800 Subject: [PATCH] test: add tet for code comments --- .../quality/documentation/CodeComment.kt | 2 +- .../quality/documentation/CodeCommentTest.kt | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 code-quality/src/test/kotlin/cc/unitmesh/quality/documentation/CodeCommentTest.kt diff --git a/code-quality/src/main/kotlin/cc/unitmesh/quality/documentation/CodeComment.kt b/code-quality/src/main/kotlin/cc/unitmesh/quality/documentation/CodeComment.kt index 9375f3ad..eacf12de 100644 --- a/code-quality/src/main/kotlin/cc/unitmesh/quality/documentation/CodeComment.kt +++ b/code-quality/src/main/kotlin/cc/unitmesh/quality/documentation/CodeComment.kt @@ -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) } diff --git a/code-quality/src/test/kotlin/cc/unitmesh/quality/documentation/CodeCommentTest.kt b/code-quality/src/test/kotlin/cc/unitmesh/quality/documentation/CodeCommentTest.kt new file mode 100644 index 00000000..4ac488d0 --- /dev/null +++ b/code-quality/src/test/kotlin/cc/unitmesh/quality/documentation/CodeCommentTest.kt @@ -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) + } +}