Skip to content

Commit

Permalink
feat(protobuf): enhance support for nested messages and enums #31
Browse files Browse the repository at this point in the history
Add the capability to properly construct nested messages within Protobuf definitions and include a new test case for enum parsing. This update ensures that the `ProtobufFullIdentListener` correctly handles nested structures and the `ProtobufAnalyserTest` reflects these changes with improved test code readability using the `@Language` annotation for Protobuf syntax highlighting.
  • Loading branch information
phodal committed Oct 25, 2024
1 parent dcd14c4 commit f856656
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ProtobufFullIdentListener(var fileName: String) : Protobuf3BaseListener()
}

is Protobuf3Parser.MessageDefContext -> {

codeDataStruct.InnerStructures += constructMessageDef(child)
}

is Protobuf3Parser.ExtendDefContext -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package chapi.ast.protobuf

import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*

Expand All @@ -8,7 +9,16 @@ class ProtobufAnalyserTest {
@Test
fun `should parse valid protobuf code and return a CodeContainer`() {
// Given
val protobufCode = "syntax = \"proto3\";\npackage example;\n\nmessage Person {\n string name = 1;\n int32 id = 2;\n}"
@Language("protobuf")
val protobufCode = """
syntax = "proto3";
package example;
message Person {
string name = 1;
int32 id = 2;
}"""
val filePath = "path/to/file.proto"
val analyser = ProtobufAnalyser()

Expand Down Expand Up @@ -37,7 +47,17 @@ class ProtobufAnalyserTest {
@Test
fun `should parse valid protobuf code with enum and return a CodeContainer`() {
// Given
val protobufCode = "syntax = \"proto3\";\npackage example;\n\nenum PhoneType {\n MOBILE = 0;\n HOME = 1;\n WORK = 2;\n}"
@Language("protobuf")
val protobufCode = """
syntax = "proto3";
package example;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}"""
val filePath = "path/to/file.proto"
val analyser = ProtobufAnalyser()

Expand All @@ -61,4 +81,42 @@ class ProtobufAnalyserTest {
assertEquals("MOBILE", field.TypeKey)
assertEquals("0", field.TypeValue)
}

/// should support message in message
@Test
fun `should parse valid protobuf code with message in message and return a CodeContainer`() {
// Given
@Language("protobuf")
val protobufCode = """syntax = "proto3";
package example;
message Person {
string name = 1;
int32 id = 2;
message Address {
string street = 1;
string city = 2;
}
}"""
val filePath = "path/to/file.proto"
val analyser = ProtobufAnalyser()

// When
val codeContainer = analyser.analysis(protobufCode, filePath)

val dataStruct = codeContainer.DataStructures.first()
val nestedDataStruct = dataStruct.InnerStructures.first()

assertEquals("Address", nestedDataStruct.NodeName)

val nestedField1 = nestedDataStruct.Fields.first()
assertEquals("string", nestedField1.TypeType)
assertEquals("street", nestedField1.TypeKey)
assertEquals("1", nestedField1.TypeValue)

val nestedField2 = nestedDataStruct.Fields[1]
assertEquals("string", nestedField2.TypeType)
assertEquals("city", nestedField2.TypeKey)
assertEquals("2", nestedField2.TypeValue)
}
}

0 comments on commit f856656

Please sign in to comment.