Rework test for binary API union sizes 31/30531/5
authorVladimir Lavor <vlavor@cisco.com>
Tue, 22 Dec 2020 14:35:31 +0000 (15:35 +0100)
committerOndrej Fabry <ofabry@cisco.com>
Mon, 1 Feb 2021 12:31:39 +0000 (12:31 +0000)
The test now generates various unions from union.api.json
and tests correct sizes of generated types.

Change-Id: Ifaf18a8ce650e71a8ca8b2d5cfb9d7eed2d757c6
Signed-off-by: Vladimir Lavor <vlavor@cisco.com>
binapigen/generator_test.go
binapigen/vppapi/testdata/union.api.json [new file with mode: 0644]
binapigen/vppapi/vppapi_test.go
core/request_handler.go

index 7f334a9..3e654d7 100644 (file)
 package binapigen
 
 import (
+       "bufio"
        "fmt"
        "git.fd.io/govpp.git/binapigen/vppapi"
        . "github.com/onsi/gomega"
+       "os"
+       "strings"
        "testing"
 )
 
@@ -58,57 +61,40 @@ func TestBinapiTypeSizes(t *testing.T) {
 
 func TestBinapiUnionSizes(t *testing.T) {
        RegisterTestingT(t)
-       tests := []struct {
-               testName string
-               input    *Union
-               expsize  int
-       }{
-               {testName: "union_alias", input: typeTestData{
-                       typ: "union", fields: []*typeTestData{{typ: "alias", value: U16},
-                       }}.getUnion("union1"), expsize: 2},
-               {testName: "union_enum", input: typeTestData{
-                       typ: "union", fields: []*typeTestData{{typ: "enum", value: U32},
-                       }}.getUnion("union2"), expsize: 4},
-               {testName: "union_struct", input: typeTestData{
-                       typ: "union", fields: []*typeTestData{
-                               {typ: "struct", fields: []*typeTestData{{value: U8}, {value: U16}, {value: U32}}},
-                       }}.getUnion("union3"), expsize: 7},
-               {testName: "union_structs", input: typeTestData{
-                       typ: "union", fields: []*typeTestData{
-                               {typ: "struct", fields: []*typeTestData{{value: U8}, {value: BOOL}}},
-                               {typ: "struct", fields: []*typeTestData{{value: U16}, {value: U32}}},
-                               {typ: "struct", fields: []*typeTestData{{value: U32}, {value: U64}}},
-                       }}.getUnion("union4"), expsize: 12},
-               {testName: "union_unions", input: typeTestData{
-                       typ: "union", fields: []*typeTestData{
-                               {typ: "union", fields: []*typeTestData{
-                                       {typ: "struct", fields: []*typeTestData{{value: STRING}}},
-                               }},
-                               {typ: "union", fields: []*typeTestData{
-                                       {typ: "struct", fields: []*typeTestData{{value: U32}}},
-                               }},
-                               {typ: "union", fields: []*typeTestData{
-                                       {typ: "struct", fields: []*typeTestData{{value: U64}}},
-                               }},
-                       }}.getUnion("union5"), expsize: 8},
-               {testName: "union_combined", input: typeTestData{
-                       typ: "union", fields: []*typeTestData{
-                               {typ: "alias", value: U8},
-                               {typ: "enum", value: U16},
-                               {typ: "struct", fields: []*typeTestData{{value: U8}, {value: U16}, {value: U32}}}, // <-
-                               {typ: "union", fields: []*typeTestData{
-                                       {typ: "alias", value: U16},
-                                       {typ: "enum", value: U16},
-                                       {typ: "struct", fields: []*typeTestData{{value: U32}}},
-                               }},
-                       }}.getUnion("union6"), expsize: 7},
-       }
-       for _, test := range tests {
-               t.Run(test.testName, func(t *testing.T) {
-                       size := getUnionSize(test.input)
-                       Expect(size).To(Equal(test.expsize))
-               })
+
+       // order of the union sizes in file generated from union.api.json
+       var sizes = []int{16, 4, 32, 16, 64, 111}
+
+       // remove directory created during test
+       defer func() {
+               err := os.RemoveAll(testOutputDir)
+               Expect(err).ToNot(HaveOccurred())
+       }()
+
+       err := GenerateFromFile("vppapi/testdata/union.api.json", Options{OutputDir: testOutputDir})
+       Expect(err).ShouldNot(HaveOccurred())
+
+       file, err := os.Open(testOutputDir + "/union/union.ba.go")
+       Expect(err).ShouldNot(HaveOccurred())
+       defer func() {
+               err := file.Close()
+               Expect(err).ToNot(HaveOccurred())
+       }()
+
+       // the generated line with union size is in format XXX_UnionData [<size>]byte
+       // the prefix identifies these lines (the starting tab is important)
+       prefix := fmt.Sprintf("\t%s", "XXX_UnionData [")
+
+       index := 0
+       scanner := bufio.NewScanner(file)
+       for scanner.Scan() {
+               if strings.HasPrefix(scanner.Text(), prefix) {
+                       Expect(scanner.Text()).To(Equal(prefix + fmt.Sprintf("%d]byte", sizes[index])))
+                       index++
+               }
        }
+       // ensure all union sizes were found and tested
+       Expect(index).To(Equal(len(sizes)))
 }
 
 // Typed data used for union size evaluation testing.
diff --git a/binapigen/vppapi/testdata/union.api.json b/binapigen/vppapi/testdata/union.api.json
new file mode 100644 (file)
index 0000000..0811f22
--- /dev/null
@@ -0,0 +1,231 @@
+{
+  "services": [],
+  "vl_api_version": "0x1db2ece9",
+  "enums": [
+    [
+      "enum1",
+      [
+        "ENUM_1_VALUE_1",
+        1
+      ],
+      [
+        "ENUM_1_VALUE_2",
+        2
+      ],
+      {
+        "enumtype": "u16"
+      }
+    ],
+    [
+      "enum2",
+      [
+        "ENUM_2_VALUE_1",
+        10
+      ],
+      [
+        "ENUM_2_VALUE_2",
+        20
+      ],
+      {
+        "enumtype": "u32"
+      }
+    ]
+  ],
+  "messages": [],
+  "types": [
+    [
+      "type1",
+      [
+        "u8",
+        "field1",
+        16
+      ],
+      [
+        "u8",
+        "field2",
+        16
+      ]
+    ],
+    [
+      "type2",
+      [
+        "u16",
+        "field1"
+      ],
+      [
+        "u32",
+        "field2"
+      ],
+      [
+        "u32",
+        "field3"
+      ]
+    ],
+    [
+      "type3",
+      [
+        "u8",
+        "field1",
+        64
+      ]
+    ],
+    [
+      "type4",
+      [
+        "u8",
+        "field1"
+      ],
+      [
+        "u8",
+        "field2",
+        16
+      ]
+    ],
+    [
+      "type5",
+      [
+        "u32",
+        "field1"
+      ],
+      [
+        "union5",
+        "field2"
+      ]
+    ],
+    [
+      "type6",
+      [
+        "u16",
+        "field1"
+      ],
+      [
+        "u32",
+        "field2"
+      ],
+      [
+        "type4",
+        "field3"
+      ],
+      [
+        "u16",
+        "field4"
+      ],
+      [
+        "u32",
+        "field5"
+      ],
+      [
+        "u32",
+        "field6"
+      ]
+    ],
+    [
+      "complex_type",
+      [
+        "u32",
+        "field1"
+      ],
+      [
+        "u8",
+        "field2"
+      ],
+      [
+        "u8",
+        "field3"
+      ],
+      [
+        "u32",
+        "field4"
+      ],
+      [
+        "type5",
+        "field5"
+      ],
+      [
+        "type6",
+        "field6"
+      ]
+    ]
+  ],
+  "unions": [
+    [
+      "union1",
+      [
+        "vl_api_alias1_t",
+        "alias1"
+      ],
+      [
+        "vl_api_alias2_t",
+        "alias2"
+      ]
+    ],
+    [
+      "union2",
+      [
+        "vl_api_enum1_t",
+        "enum1"
+      ],
+      [
+        "vl_api_enum2_t",
+        "enum2"
+      ]
+    ],
+    [
+      "union3",
+      [
+        "vl_api_type1_t",
+        "type1"
+      ],
+      [
+        "vl_api_type2_t",
+        "type2"
+      ]
+    ],
+    [
+      "union4",
+      [
+        "vl_api_union1_t",
+        "union1"
+      ],
+      [
+        "vl_api_union2_t",
+        "union2"
+      ]
+    ],
+    [
+      "union5",
+      [
+        "vl_api_type1_t",
+        "type1"
+      ],
+      [
+        "vl_api_type3_t",
+        "type3"
+      ]
+    ],
+    [
+      "union6",
+      [
+        "vl_api_type1_t",
+        "type1"
+      ],
+      [
+        "vl_api_complex_type_t",
+        "type3"
+      ]
+    ]
+  ],
+  "aliases": {
+    "alias1": {
+      "type": "u8",
+      "length": 4
+    },
+    "alias2": {
+      "type": "u8",
+      "length": 16
+    },
+    "alias3": {
+      "type": "u32"
+    }
+  }
+}
index 027cc1f..a555d9f 100644 (file)
@@ -27,7 +27,7 @@ func TestGetInputFiles(t *testing.T) {
 
        result, err := FindFiles("testdata", 1)
        Expect(err).ShouldNot(HaveOccurred())
-       Expect(result).To(HaveLen(5))
+       Expect(result).To(HaveLen(6))
        for _, file := range result {
                Expect(file).To(BeAnExistingFile())
        }
index 29685f6..95bd924 100644 (file)
@@ -237,7 +237,7 @@ func (c *Connection) msgCallback(msgID uint16, data []byte) {
                        "is_multi": isMulti,
                        "seq_num":  seqNum,
                        "msg_crc":  crc,
-               }).Debugf("<-- govpp RECEIVE: %s %+v", name)
+               }).Debugf("<-- govpp RECEIVE: %s", name)
        }
 
        if context == 0 || c.isNotificationMessage(msgID) {