Skip to content

Commit

Permalink
test(shirelang): update foreign function processing and test case #116
Browse files Browse the repository at this point in the history
Update the Shirelang foreign function processor to use the 'findFile' method and refine the related test case. This change ensures a more accurate error message is provided when a run service for a JavaScript file is not found, replacing the outdated 'lookupFile' method and removing the redundant Python file handling from the test.
  • Loading branch information
phodal committed Oct 7, 2024
1 parent c4e51fc commit 2f71596
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.phodal.shirelang.compiler.hobbit.execute

import com.intellij.openapi.project.Project
import com.phodal.shirecore.lookupFile
import com.phodal.shirecore.findFile
import com.phodal.shirecore.provider.shire.FileRunService
import com.phodal.shirelang.compiler.SHIRE_ERROR
import com.phodal.shirelang.compiler.hobbit.function.ForeignFunction
Expand All @@ -13,11 +13,7 @@ class ForeignFunctionProcessor {
): Any {
val filename = func.funcPath

val virtualFile = try {
project.lookupFile(filename) ?: return "$SHIRE_ERROR: File not found: $filename"
} catch (e: Exception) {
return "$SHIRE_ERROR: File not found: $filename"
}
val virtualFile = project.findFile(filename) ?: return "$SHIRE_ERROR: File not found: $filename"

val runService = FileRunService.provider(project, virtualFile)

Expand Down
19 changes: 12 additions & 7 deletions shirelang/src/test/kotlin/com/phodal/shirelang/ShireCompileTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -439,26 +439,31 @@ Summary webpage: ${'$'}fileName""", results["var2"].toString()
}

fun testShouldSupportForeignFunction() {
@Language("JavaScript")
val jsMainWithArgs = """
const args = process.argv.slice(2);
console.log(args[0]);
process.exit(0);
""".trimIndent()

myFixture.addFileToProject("hello.js", jsMainWithArgs)

@Language("Shire")
val code = """
---
name: Summary
description: "Generate Summary"
interaction: AppendCursor
functions:
normal: "hello.py"(string)
normal: "hello.js"(string)
variables:
"var2": /.*ple.shire/ { normal }
---
Summary webpage: ${'$'}fileName
""".trimIndent()
val file = myFixture.addFileToProject("sample.shire", code)

@Language("Python")
val pythonHello = """print('hello world')"""
val helloPy = myFixture.addFileToProject("hello.py", pythonHello)

myFixture.openFileInEditor(file.virtualFile)

val compile = ShireSyntaxAnalyzer(project, file as ShireFile, myFixture.editor).parse()
Expand All @@ -470,6 +475,6 @@ Summary webpage: ${'$'}fileName""", results["var2"].toString()
}
}

assertEquals("<ShireError>: File not found: hello.py", results["var2"].toString())
assertEquals("<ShireError>: [ForeignFunctionProcessor] No run service found for file: hello.js", results["var2"].toString())
}
}

0 comments on commit 2f71596

Please sign in to comment.