Skip to content

Commit da588ce

Browse files
committed
feat: add test support for mod
1 parent 06333ed commit da588ce

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

chapi-ast-rust/src/main/kotlin/chapi/ast/rustast/RustAstBaseListener.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ open class RustAstBaseListener(private val fileName: String) : RustParserBaseLis
2929

3030
private var structMap = mutableMapOf<String, CodeDataStruct>()
3131

32+
private var currentModule: String = ""
33+
/// for testing
34+
private var lastModule: String = ""
35+
3236

3337
/**
3438
* localVars will store all local variables in the current scope
@@ -82,6 +86,15 @@ open class RustAstBaseListener(private val fileName: String) : RustParserBaseLis
8286
Imports = imports
8387
}
8488

89+
override fun enterModule(ctx: RustParser.ModuleContext?) {
90+
currentModule = (ctx?.identifier()?.text ?: return)
91+
}
92+
93+
override fun exitModule(ctx: RustParser.ModuleContext?) {
94+
lastModule = currentModule
95+
currentModule = ""
96+
}
97+
8598
override fun enterUseDeclaration(ctx: RustParser.UseDeclarationContext?) {
8699
val importString: List<List<String>> = buildToPath(ctx)
87100

@@ -150,6 +163,7 @@ open class RustAstBaseListener(private val fileName: String) : RustParserBaseLis
150163

151164
val codeStruct = CodeDataStruct(
152165
NodeName = structName,
166+
Module = currentModule,
153167
Package = codeContainer.PackageName,
154168
Annotations = annotation,
155169
Fields = buildFields(ctx.structFields()),
@@ -270,6 +284,7 @@ open class RustAstBaseListener(private val fileName: String) : RustParserBaseLis
270284

271285
val codeStruct = CodeDataStruct(
272286
NodeName = enumName,
287+
Module = currentModule,
273288
Package = codeContainer.PackageName,
274289
Annotations = annotation,
275290
Fields = buildEnumFields(ctx.enumItems()),
@@ -367,6 +382,7 @@ open class RustAstBaseListener(private val fileName: String) : RustParserBaseLis
367382
} else {
368383
currentNode = CodeDataStruct(
369384
NodeName = nodeName,
385+
Module = currentModule,
370386
Package = codeContainer.PackageName,
371387
Position = buildPosition(ctx ?: return)
372388
)
@@ -417,6 +433,7 @@ open class RustAstBaseListener(private val fileName: String) : RustParserBaseLis
417433
return listOf(
418434
CodeDataStruct().apply {
419435
NodeName = fileName.substringBeforeLast('.')
436+
Module = if (lastModule == "tests") lastModule else ""
420437
Type = DataStructType.OBJECT
421438
Package = codeContainer.PackageName
422439
FilePath = codeContainer.FullName

chapi-ast-rust/src/test/kotlin/chapi/ast/rustast/RustFullIdentListenerTest.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,23 @@ class RustFullIdentListenerTest {
421421
assertEquals("actix_web::HttpServer", secondFunction.FunctionCalls[0].NodeName)
422422
assertEquals("run", secondFunction.FunctionCalls[0].FunctionName)
423423
}
424+
425+
@Test
426+
fun should_handle_test_mod() {
427+
val code = """
428+
#[cfg(test)]
429+
mod tests {
430+
use super::*;
431+
432+
#[test]
433+
fn test_add() {
434+
assert_eq!(add(1, 2), 3);
435+
}
436+
}
437+
""".trimIndent()
438+
439+
val codeContainer = RustAnalyser().analysis(code, "lib.rs")
440+
val codeDataStruct = codeContainer.DataStructures[0]
441+
assertEquals("tests", codeDataStruct.Module)
442+
}
424443
}

chapi-domain/src/main/kotlin/chapi/domain/core/CodeDataStruct.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ data class CodeDataStruct(
2727
* for Rust, if is a variable, function, it will be naming to `default`
2828
*/
2929
var NodeName: String = "",
30+
//
3031
var Module: String = "",
3132
var Type: DataStructType = DataStructType.EMPTY,
3233
var Package: String = "",

0 commit comments

Comments
 (0)