From c0c73d34a7f5eae44e7c9230ddccc0cfcb201084 Mon Sep 17 00:00:00 2001 From: Vladimir Lavor Date: Tue, 22 Dec 2020 15:35:31 +0100 Subject: Rework test for binary API union sizes 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 --- binapigen/generator_test.go | 86 +++++------- binapigen/vppapi/testdata/union.api.json | 231 +++++++++++++++++++++++++++++++ binapigen/vppapi/vppapi_test.go | 2 +- core/request_handler.go | 2 +- 4 files changed, 269 insertions(+), 52 deletions(-) create mode 100644 binapigen/vppapi/testdata/union.api.json diff --git a/binapigen/generator_test.go b/binapigen/generator_test.go index 7f334a9..3e654d7 100644 --- a/binapigen/generator_test.go +++ b/binapigen/generator_test.go @@ -15,9 +15,12 @@ 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 []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 index 0000000..0811f22 --- /dev/null +++ b/binapigen/vppapi/testdata/union.api.json @@ -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" + } + } +} diff --git a/binapigen/vppapi/vppapi_test.go b/binapigen/vppapi/vppapi_test.go index 027cc1f..a555d9f 100644 --- a/binapigen/vppapi/vppapi_test.go +++ b/binapigen/vppapi/vppapi_test.go @@ -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()) } diff --git a/core/request_handler.go b/core/request_handler.go index 29685f6..95bd924 100644 --- a/core/request_handler.go +++ b/core/request_handler.go @@ -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) { -- cgit 1.2.3-korg