Skip to content

Commit

Permalink
feat: add add check naming style function name
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Dec 24, 2023
1 parent 42181c7 commit c52bc1c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ClassTestCodeBuilder(private val context: JobContext) : TestCodeBuilder {
relevantClasses: List<CodeDataStruct>,
): List<ClassTestIns> {
val generatedCode = dataStruct.toSourceCode()

return listOf(
ClassTestIns(
lang = context.project.language,
Expand All @@ -30,8 +31,6 @@ class ClassTestCodeBuilder(private val context: JobContext) : TestCodeBuilder {
testFrameworks = context.project.testFrameworks,
testType = TestCodeBuilderType.CLASS_UNIT,
specs = listOf(
"You MUST use should_xx_xx style for test method name.",
"You MUST use given-when-then style.",
"Test file should be complete and compilable, without need for further actions.",
"Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parameterized tests."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,37 @@ fun CodeContainer.buildSourceCode(codeLines: List<String>) {
}
}

enum class NamingStyle(val value: String) {
CAMEL_CASE("CamelCase"),
SNAKE_CASE("snake_case"),
KEBAB_CASE("kebab-case"),
;
}

fun CodeDataStruct.checkNamingStyle(): String {
val countByNaming: MutableMap<NamingStyle, Int> = mutableMapOf(
NamingStyle.CAMEL_CASE to 0,
NamingStyle.SNAKE_CASE to 0,
NamingStyle.KEBAB_CASE to 0,
)
this.Functions.map {
// check by [CodeFunction.Name]
val name = it.Name
val nameStyle = when {
name.contains("_") -> NamingStyle.SNAKE_CASE
name.contains("-") -> NamingStyle.KEBAB_CASE
else -> NamingStyle.CAMEL_CASE
}

countByNaming[nameStyle] = countByNaming[nameStyle]!! + 1
}

val maxCount = countByNaming.values.maxOrNull()!!
val maxNamingStyle = countByNaming.filter { it.value == maxCount }.keys.first()

return maxNamingStyle.value
}

fun CodeDataStruct.toSourceCode(): String {
val result = StringBuilder()
result.append("package ${this.Package};\n\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,56 @@ class CodeDataStructUtilTest {

assertEquals(originCode, codeContainer.DataStructures[0].toSourceCode())
}

@Test
fun should_return_camel_case_when_all_function_names_are_in_camel_case() {
// given
val sourceCode = """
package com.example.springboot;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class HelloController {
@GetMapping("/blog/get")
public String index() {
return "Greetings from Spring Boot!";
}
}
""".trimIndent()

val codeDataStruct = JavaAnalyser().analysis(sourceCode, "").DataStructures[0]
// when
val result = codeDataStruct.checkNamingStyle()

// then
assertEquals("CamelCase", result)
}

@Test
fun should_return_snake_case_when_all_function_names_are_in_snake_case() {
// given
val sourceCode = """
package com.example.springboot;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class HelloController {
@GetMapping("/blog/get")
public String index_name() {
return "Greetings from Spring Boot!";
}
}
""".trimIndent()

val codeDataStruct = JavaAnalyser().analysis(sourceCode, "").DataStructures[0]
// when
val result = codeDataStruct.checkNamingStyle()

// then
assertEquals("snake_case", result)
}
}

0 comments on commit c52bc1c

Please sign in to comment.