From f0eed87cb95b64b524fc15a84803c2d197a67998 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 30 Jan 2024 11:51:18 +0800 Subject: [PATCH] feat(cpp): add test case for identifying class fields #14 Add a new test case to `CPPBasicIdentListenerTest.kt` that verifies the identification of class fields. The test code defines a class with two integer fields, `Id` and `No`. The test checks if the fields are correctly identified and their types are properly assigned. --- .../chapi/ast/cppast/CPPBasicIdentListener.kt | 19 ++++++++++++------- .../ast/cppast/CPPBasicIdentListenerTest.kt | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/chapi-ast-cpp/src/main/kotlin/chapi/ast/cppast/CPPBasicIdentListener.kt b/chapi-ast-cpp/src/main/kotlin/chapi/ast/cppast/CPPBasicIdentListener.kt index 909eaea3..ee5ce843 100644 --- a/chapi-ast-cpp/src/main/kotlin/chapi/ast/cppast/CPPBasicIdentListener.kt +++ b/chapi-ast-cpp/src/main/kotlin/chapi/ast/cppast/CPPBasicIdentListener.kt @@ -2,10 +2,7 @@ package chapi.ast.cppast import chapi.ast.antlr.CPP14Parser import chapi.ast.antlr.CPP14ParserBaseListener -import chapi.domain.core.CodeContainer -import chapi.domain.core.CodeDataStruct -import chapi.domain.core.CodeFunction -import chapi.domain.core.CodeProperty +import chapi.domain.core.* class CPPBasicIdentListener(fileName: String) : CPP14ParserBaseListener() { private var codeContainer: CodeContainer = CodeContainer(FullName = fileName) @@ -72,9 +69,17 @@ class CPPBasicIdentListener(fileName: String) : CPP14ParserBaseListener() { currentNode?.Implements = extends } - ctx?.memberSpecification()?.memberdeclaration()?.let { - // TODO: add member - } + val fields = ctx?.memberSpecification()?.memberdeclaration()?.map { + val type = it?.declSpecifierSeq()?.declSpecifier()?.firstOrNull()?.typeSpecifier()?.text + + it.memberDeclaratorList()?.memberDeclarator()?.map { memberDeclarator -> + val name = memberDeclarator.declarator()?.text + + CodeField(TypeKey = name ?: "", TypeType = type ?: "", TypeValue = type ?: "") + } ?: listOf() + }?.flatten() ?: listOf() + + currentNode?.Fields = fields } override fun exitClassSpecifier(ctx: CPP14Parser.ClassSpecifierContext?) { diff --git a/chapi-ast-cpp/src/test/kotlin/chapi/ast/cppast/CPPBasicIdentListenerTest.kt b/chapi-ast-cpp/src/test/kotlin/chapi/ast/cppast/CPPBasicIdentListenerTest.kt index ff0a4532..d5f2b3e6 100644 --- a/chapi-ast-cpp/src/test/kotlin/chapi/ast/cppast/CPPBasicIdentListenerTest.kt +++ b/chapi-ast-cpp/src/test/kotlin/chapi/ast/cppast/CPPBasicIdentListenerTest.kt @@ -131,4 +131,23 @@ void display(char c, int n) { assertEquals(container.DataStructures[0].NodeName, "M") } + @Test + internal fun shouldIdentifyClassField() { + val code = """ + class Entity + { + public: + int Id, No; + }; + """.trimIndent() + + val container = CPPAnalyser().analysis(code, "helloworld.cpp") + assertEquals(container.DataStructures.size, 1) + assertEquals(container.DataStructures[0].Fields.size, 2) + // fields + assertEquals(container.DataStructures[0].Fields[0].TypeType, "int") + assertEquals(container.DataStructures[0].Fields[0].TypeKey, "Id") + assertEquals(container.DataStructures[0].Fields[1].TypeType, "int") + assertEquals(container.DataStructures[0].Fields[1].TypeKey, "No") + } }