Skip to content

Commit

Permalink
fix(PatternFuncProcessor): enhance path resolution and refactor tests #…
Browse files Browse the repository at this point in the history
…83

Enhanced the path resolution logic in PatternFuncProcessor to handle different types of patterMatchPaths including List, Array, and String. Added a warning log for unsupported types. Also, refactored the ShireCompileTest by reordering the code and adding a new test case for controller code with head.
  • Loading branch information
phodal committed Sep 13, 2024
1 parent 56a1961 commit a81c603
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,23 @@ open class PatternFuncProcessor(open val myProject: Project, open val hole: Hobb
val baseDir = myProject.guessProjectDir()!!
var paths = userPaths
if (userPaths.isEmpty()) {
paths = patterMatchPaths as Array<String>
when (patterMatchPaths) {
is List<*> -> {
paths = (patterMatchPaths as List<String>).toTypedArray()
}

is Array<*> -> {
paths = patterMatchPaths as Array<String>
}

is String -> {
paths = arrayOf(patterMatchPaths)
}

else -> {
logger<PatternFuncProcessor>().warn("resolvePaths error: $patterMatchPaths")
}
}
}

val absolutePaths: List<VirtualFile> = paths.mapNotNull {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import kotlinx.coroutines.runBlocking
import org.intellij.lang.annotations.Language

class ShireCompileTest : BasePlatformTestCase() {
fun testShouldReturnControllerCodeWithFindCat() {
val javaHelloController = """
val javaHelloController = """
package com.phodal.shirelang.controller;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -32,9 +31,7 @@ class ShireCompileTest : BasePlatformTestCase() {
}
""".trimIndent()

myFixture.addFileToProject("src/main/java/com/phodal/shirelang/controller/HelloController.java", javaHelloController)

val javaHelloEntity = """
val javaHelloEntity = """
package com.phodal.shirelang.entity;
public class HelloEntity {
Expand All @@ -50,6 +47,11 @@ class ShireCompileTest : BasePlatformTestCase() {
}
""".trimIndent()

fun testShouldReturnControllerCodeWithFindCat() {
myFixture.addFileToProject(
"src/main/java/com/phodal/shirelang/controller/HelloController.java",
javaHelloController
)
myFixture.addFileToProject("src/main/java/com/phodal/shirelang/entity/HelloEntity.java", javaHelloEntity)

@Language("Shire")
Expand Down Expand Up @@ -96,6 +98,65 @@ public class HelloController {
public String hello() {
return "Hello, World!";
}
}""", context.compiledVariables["controllers"])
}""", context.compiledVariables["controllers"]
)
}

fun testShouldReturnControllerCodeWithFindCatWithHead() {
myFixture.addFileToProject(
"src/main/java/com/phodal/shirelang/controller/HelloController.java",
javaHelloController
)
myFixture.addFileToProject("src/main/java/com/phodal/shirelang/entity/HelloEntity.java", javaHelloEntity)

@Language("Shire")
val code = """
---
name: "类图分析"
variables:
"output": "name.adl"
"con": /.*.java/ { print | head(1)}
"controllers": /.*.java/ { find("Controller") | grep("src/main/java/.*") | head(1) | cat }
"outputFile": /any/ { print("name.adl") }
onStreamingEnd: { parseCode | saveFile(${'$'}outputFile) }
---
下面是你要执行转换的数据:
${'$'}controllers
""".trimIndent()

val file = myFixture.configureByText("test.shire", code)
val compile = ShireSyntaxAnalyzer(project, file as ShireFile, myFixture.editor).parse()
val hole = compile.config!!

val context = PostProcessorContext(
genText = "User prompt:\n\n",
)

runBlocking {
val templateCompiler = ShireTemplateCompiler(project, hole, compile.variableTable, code)
val compiledVariables =
templateCompiler.compileVariable(myFixture.editor)

context.compiledVariables = compiledVariables
}

assertEquals(
"""[/src/src/main/java/com/phodal/shirelang/entity/HelloEntity.java]""",
context.compiledVariables["con"]
)
assertEquals("package com.phodal.shirelang.controller;\n" +
"\n" +
"import org.springframework.web.bind.annotation.GetMapping;\n" +
"import org.springframework.web.bind.annotation.RestController;\n" +
"\n" +
"@RestController\n" +
"public class HelloController {\n" +
" @GetMapping(\"/hello\")\n" +
" public String hello() {\n" +
" return \"Hello, World!\";\n" +
" }\n" +
"}", context.compiledVariables["controllers"])
}
}

0 comments on commit a81c603

Please sign in to comment.