Skip to content

Commit

Permalink
feat(cpp): add test case for identifying class fields #14
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phodal committed Jan 30, 2024
1 parent d382bdd commit f0eed87
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit f0eed87

Please sign in to comment.