Skip to content

Commit

Permalink
refactor(context): remove unused variables and comments
Browse files Browse the repository at this point in the history
- Remove unused import and variables in MethodContext.kt
- Remove unused variables and comments in TestCodeGenTask.kt
  • Loading branch information
phodal committed Jan 12, 2024
1 parent f2382c5 commit 1c5bad0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 64 deletions.
100 changes: 47 additions & 53 deletions docs/development/test-prompting.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,56 @@ parent: Development

## Test Prompts

Write unit test for following code.
You MUST return code only, not explain.
You MUST Use English to reply me!
You are working on a project that uses Spring MVC,Spring WebFlux,JDBC to build RESTful APIs.
You MUST use should_xx_xx style for test method name.
You MUST use given-when-then style.
Write unit test for following Language:
- 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.
- Ensure that each test focuses on a single use case to maintain clarity and readability.
- Instead of using `@BeforeEach` methods for setup, include all necessary code initialization within each individual test method, do not write parameterized tests.
| This project uses JUnit 5, you should import `org.junit.jupiter.api.Test` and use `@Test` annotation.- You MUST use MockMvc and test API only.
- Use appropriate Spring test annotations such as `@MockBean`, `@Autowired`, `@WebMvcTest`, `@DataJpaTest`, `@AutoConfigureTestDatabase`, `@AutoConfigureMockMvc`, `@SpringBootTest` etc.
- This project uses JUnit 4, you should import `org.junit.Test` and use `@Test` annotation.
Kotlin API version: 1.9


// here are related classes:
// 'filePath: /Users/phodal/IdeaProjects/untitled/src/main/java/cc/unitmesh/untitled/demo/service/BlogService.java
// class BlogService {
// blogRepository
// + public BlogPost createBlog(BlogPost blogDto)
// + public BlogPost getBlogById(Long id)
// + public BlogPost updateBlog(Long id, BlogPost blogDto)
// + public void deleteBlog(Long id)
// }
// 'filePath: /Users/phodal/IdeaProjects/untitled/src/main/java/cc/unitmesh/untitled/demo/dto/CreateBlogRequest.java
// class CreateBlogRequest {
// title
// content
// user
//
// }
// 'filePath: /Users/phodal/IdeaProjects/untitled/src/main/java/cc/unitmesh/untitled/demo/entity/BlogPost.java
// class BlogPost {
// id
// title
// content
// author
// + public BlogPost(String title, String content, String author)
// + public BlogPost()
// + public void setId(Long id)
// + public Long getId()
// + public String getTitle()
// + public void setTitle(String title)
// + public String getContent()
// + public void setContent(String content)
// + public String getAuthor()
// + public void setAuthor(String author)
// }
```java
@ApiOperation(value = "Create a new blog")
@PostMapping("/")
public BlogPost createBlog(@RequestBody CreateBlogRequest request) {
CreateBlogResponse response = new CreateBlogResponse();
BlogPost blogPost = new BlogPost();
BeanUtils.copyProperties(request, blogPost);
BlogPost createdBlog = blogService.createBlog(blogPost);
BeanUtils.copyProperties(createdBlog, response);
return createdBlog;
// here is current class information:
'package: cc.unitmesh.idea.provider.JavaTestDataBuilder
class JavaTestDataBuilder {
+ override fun baseRoute(element: PsiElement): String
+ override fun inboundData(element: PsiElement): Map<String, String>
+ private fun handleFromType(parameter: PsiParameter): Map<@NlsSafe String, String>
+ private fun processing(returnType: PsiType): Map<@NlsSafe String, String>
+ private fun processingClassType(type: PsiClassType): Map<@NlsSafe String, String>
+ override fun outboundData(element: PsiElement): Map<String, String>
}
Code:
// import cc.unitmesh.devti.provider.TestDataBuilder
// import cc.unitmesh.idea.context.JavaContextCollection
// import com.intellij.openapi.util.NlsSafe
// import com.intellij.psi.*
// import com.intellij.psi.impl.source.PsiClassReferenceType
```kotlin
/**
* Returns the base route of a given Kotlin language method.
*
* This method takes a PsiElement as input and checks if it is an instance of PsiMethod. If it is not, an empty string is returned.
* If the input element is a PsiMethod, the method checks if its containing class has the annotation "@RequestMapping" from the Spring Framework.
* If the annotation is found, the method retrieves the value attribute of the annotation and returns it as a string.
* If the value attribute is not a PsiLiteralExpression, an empty string is returned.
*
* @param element the PsiElement representing the Kotlin language method
* @return the base route of the method as a string, or an empty string if the method does not have a base route or if the input element is not a PsiMethod
*/
override fun baseRoute(element: PsiElement): String {
if (element !is PsiMethod) return ""

val containingClass = element.containingClass ?: return ""
containingClass.annotations.forEach {
if (it.qualifiedName == "org.springframework.web.bind.annotation.RequestMapping") {
val value = it.findAttributeValue("value") ?: return ""
if (value is PsiLiteralExpression) {
return value.value as String
}
}
}

return ""
}
```
Start with `import` syntax here:
```Start with `import` syntax here:
23 changes: 13 additions & 10 deletions src/main/kotlin/cc/unitmesh/devti/context/MethodContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cc.unitmesh.devti.context

import cc.unitmesh.devti.context.base.NamedElementContext
import cc.unitmesh.devti.isInProject
import com.intellij.lang.LanguageCommenters
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiReference
Expand All @@ -18,11 +17,9 @@ class MethodContext(
val paramNames: List<String> = emptyList(),
val includeClassContext: Boolean = false,
val usages: List<PsiReference> = emptyList(),
val fanInOut: List<PsiElement> = emptyList(),
private val fanInOut: List<PsiElement> = emptyList(),
) : NamedElementContext(root, text, name) {
private val classContext: ClassContext?
private val commenter = LanguageCommenters.INSTANCE.forLanguage(root.language) ?: null
private val commentPrefix = commenter?.lineCommentPrefix ?: ""
private val project: Project = root.project


Expand All @@ -41,10 +38,12 @@ class MethodContext(
"${classFile.name} -> $useText"
}

var query = """language: ${language ?: "_"}
fun name: ${name ?: "_"}
fun signature: ${signature ?: "_"}
"""
var query = """
language: ${language ?: "_"}
fun name: ${name ?: "_"}
fun signature: ${signature ?: "_"}
""".trimIndent()

if (usageString.isNotEmpty()) {
query += "usages: \n$usageString"
}
Expand All @@ -69,14 +68,18 @@ fun signature: ${signature ?: "_"}
}

context.let { classContext ->
result += classContext.format() + "\n"
result += "${classContext.format()}\n"
}
}

if (result.isEmpty()) {
return ""
}

return "```uml\n$result\n```\n"
return """
```uml
$result
```
""".trimIndent()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class TestCodeGenTask(val request: TestCodeGenRequest) :
return
}

var prompter = "Write unit test for following ${request.element.language} code."
var prompter = "Write unit test for following $lang code."

indicator.text = AutoDevBundle.message("intentions.chat.code.test.step.collect-context")
indicator.fraction = 0.3
Expand Down

0 comments on commit 1c5bad0

Please sign in to comment.