Skip to content

Commit dc5289d

Browse files
committed
feat: add for junit rules
1 parent e0084de commit dc5289d

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

java/src/main/kotlin/cc/unitmesh/idea/provider/JavaTestContextProvider.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ open class JavaTestContextProvider : ChatContextProvider {
2828

2929
val isSpringRelated = creationContext.element?.let { isSpringRelated(it) } ?: false
3030

31-
val baseTestPrompt = """
31+
var baseTestPrompt = """
3232
|You MUST use should_xx_xx style for test method name.
3333
|You MUST use given-when-then style.
3434
|- Test file should be complete and compilable, without need for further actions.
3535
|- Ensure that each test focuses on a single use case to maintain clarity and readability.
3636
|- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parameterized tests.
3737
|""".trimMargin()
3838

39-
// todo: check is spring project
39+
baseTestPrompt += junitRule(project)
4040

4141
items += when {
4242
isController && isSpringRelated -> {
@@ -64,6 +64,19 @@ open class JavaTestContextProvider : ChatContextProvider {
6464
return items
6565
}
6666

67+
private fun junitRule(project: Project): String {
68+
SpringContextProvider.prepareLibraryData(project)?.forEach {
69+
if (it.groupId?.contains("org.junit.jupiter") == true) {
70+
return "| This project uses JUnit 5, you should import `org.junit.jupiter.api.Test` and use `@Test` annotation."
71+
}
72+
if (it.groupId?.contains("org.junit") == true) {
73+
return "| This project uses JUnit 4, you should import `org.junit.Test` and use `@Test` annotation."
74+
}
75+
}
76+
77+
return ""
78+
}
79+
6780
private fun isSpringRelated(method: PsiElement): Boolean {
6881
when (method) {
6982
is PsiMethod -> {

java/src/main/kotlin/cc/unitmesh/idea/provider/SpringContextProvider.kt

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ open class SpringContextProvider : ChatContextProvider {
1717
}
1818

1919
private fun hasProjectLibraries(project: Project): Boolean {
20-
prepareLibraryData(project)?.forEach {
20+
Companion.prepareLibraryData(project)?.forEach {
2121
if (it.groupId?.contains("org.springframework") == true) {
2222
return true
2323
}
@@ -68,23 +68,8 @@ open class SpringContextProvider : ChatContextProvider {
6868
)
6969
}
7070

71-
private fun prepareLibraryData(project: Project): List<LibraryData>? {
72-
val basePath = project.basePath ?: return null
73-
val projectData = ProjectDataManager.getInstance().getExternalProjectData(
74-
project, GradleConstants.SYSTEM_ID, basePath
75-
)
76-
77-
val libraryDataList = projectData?.externalProjectStructure?.children?.filter {
78-
it.data is LibraryData
79-
}?.map {
80-
it.data as LibraryData
81-
}
82-
83-
return libraryDataList
84-
}
85-
8671
private fun prepareLibrary(project: Project): TestStack {
87-
val libraryDataList = prepareLibraryData(project)
72+
val libraryDataList = Companion.prepareLibraryData(project)
8873

8974
val testStack = TestStack()
9075
var hasMatchSpringMvc = false
@@ -139,4 +124,21 @@ open class SpringContextProvider : ChatContextProvider {
139124

140125
return testStack
141126
}
127+
128+
companion object {
129+
fun prepareLibraryData(project: Project): List<LibraryData>? {
130+
val basePath = project.basePath ?: return null
131+
val projectData = ProjectDataManager.getInstance().getExternalProjectData(
132+
project, GradleConstants.SYSTEM_ID, basePath
133+
)
134+
135+
val libraryDataList = projectData?.externalProjectStructure?.children?.filter {
136+
it.data is LibraryData
137+
}?.map {
138+
it.data as LibraryData
139+
}
140+
141+
return libraryDataList
142+
}
143+
}
142144
}

0 commit comments

Comments
 (0)