summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSaman <100295082+enum-class@users.noreply.github.com>2022-11-22 16:21:25 -0500
committerGitHub <noreply@github.com>2022-11-22 13:21:25 -0800
commit1cba8b2b49a808a6b1bbd72fb0b3cbc27fb79f46 (patch)
treeb9b306ec3704fd38b4decfc3ad0ff053b557d77e /tests
parenteead6c62193aef3fb6643ad95adcc73a796f8002 (diff)
downloadflatbuffers-1cba8b2b49a808a6b1bbd72fb0b3cbc27fb79f46.tar.gz
flatbuffers-1cba8b2b49a808a6b1bbd72fb0b3cbc27fb79f46.tar.bz2
flatbuffers-1cba8b2b49a808a6b1bbd72fb0b3cbc27fb79f46.zip
Fix go generator undefined Package name, also throwing exception (#7632)
* Fix go generator undefined Package, also throw exception in specific examples. * Add test for go generator import problem * Add new version of generated go file. Fix conflict. * Add executable permission to generate_code.py script. * Improve test quality, remove unwanted generated files, better naming * Fix comments * clang format Co-authored-by: Derek Bailey <derekbailey@google.com>
Diffstat (limited to 'tests')
-rwxr-xr-xtests/GoTest.sh16
-rw-r--r--tests/Pizza.go78
-rw-r--r--tests/go_test.go23
-rw-r--r--tests/include_test/order.fbs8
-rw-r--r--tests/include_test/sub/no_namespace.fbs3
-rw-r--r--tests/order/Food.go102
6 files changed, 218 insertions, 12 deletions
diff --git a/tests/GoTest.sh b/tests/GoTest.sh
index 85253c17..8e73af24 100755
--- a/tests/GoTest.sh
+++ b/tests/GoTest.sh
@@ -20,26 +20,18 @@ go_path=${test_dir}/go_gen
go_src=${go_path}/src
# Emit Go code for the example schemas in the test dir:
-../flatc -g --gen-object-api -I include_test monster_test.fbs optional_scalars.fbs
+../flatc -g --gen-object-api -I include_test -o ${go_src} monster_test.fbs optional_scalars.fbs
+../flatc -g --gen-object-api -I include_test/sub -o ${go_src} include_test/order.fbs
+../flatc -g --gen-object-api -o ${go_src}/Pizza include_test/sub/no_namespace.fbs
# Go requires a particular layout of files in order to link multiple packages.
# Copy flatbuffer Go files to their own package directories to compile the
# test binary:
-mkdir -p ${go_src}/MyGame/Example
-mkdir -p ${go_src}/MyGame/Example2
mkdir -p ${go_src}/github.com/google/flatbuffers/go
mkdir -p ${go_src}/flatbuffers_test
-mkdir -p ${go_src}/optional_scalars
-cp -a MyGame/*.go ./go_gen/src/MyGame/
-cp -a MyGame/Example/*.go ./go_gen/src/MyGame/Example/
-cp -a MyGame/Example2/*.go ./go_gen/src/MyGame/Example2/
-# do not compile the gRPC generated files, which are not tested by go_test.go
-# below, but have their own test.
-rm ./go_gen/src/MyGame/Example/*_grpc.go
cp -a ../go/* ./go_gen/src/github.com/google/flatbuffers/go
cp -a ./go_test.go ./go_gen/src/flatbuffers_test/
-cp -a optional_scalars/*.go ./go_gen/src/optional_scalars
# https://stackoverflow.com/a/63545857/7024978
# We need to turn off go modules for this script
@@ -72,7 +64,7 @@ else
exit 1
fi
-NOT_FMT_FILES=$(gofmt -l MyGame)
+NOT_FMT_FILES=$(gofmt -l .)
if [[ ${NOT_FMT_FILES} != "" ]]; then
echo "These files are not well gofmt'ed:"
echo
diff --git a/tests/Pizza.go b/tests/Pizza.go
new file mode 100644
index 00000000..08df9e16
--- /dev/null
+++ b/tests/Pizza.go
@@ -0,0 +1,78 @@
+// Code generated by the FlatBuffers compiler. DO NOT EDIT.
+
+package Pizza
+
+import (
+ flatbuffers "github.com/google/flatbuffers/go"
+)
+
+type PizzaT struct {
+ Size int32 `json:"size"`
+}
+
+func (t *PizzaT) Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
+ if t == nil { return 0 }
+ PizzaStart(builder)
+ PizzaAddSize(builder, t.Size)
+ return PizzaEnd(builder)
+}
+
+func (rcv *Pizza) UnPackTo(t *PizzaT) {
+ t.Size = rcv.Size()
+}
+
+func (rcv *Pizza) UnPack() *PizzaT {
+ if rcv == nil { return nil }
+ t := &PizzaT{}
+ rcv.UnPackTo(t)
+ return t
+}
+
+type Pizza struct {
+ _tab flatbuffers.Table
+}
+
+func GetRootAsPizza(buf []byte, offset flatbuffers.UOffsetT) *Pizza {
+ n := flatbuffers.GetUOffsetT(buf[offset:])
+ x := &Pizza{}
+ x.Init(buf, n+offset)
+ return x
+}
+
+func GetSizePrefixedRootAsPizza(buf []byte, offset flatbuffers.UOffsetT) *Pizza {
+ n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:])
+ x := &Pizza{}
+ x.Init(buf, n+offset+flatbuffers.SizeUint32)
+ return x
+}
+
+func (rcv *Pizza) Init(buf []byte, i flatbuffers.UOffsetT) {
+ rcv._tab.Bytes = buf
+ rcv._tab.Pos = i
+}
+
+func (rcv *Pizza) Table() flatbuffers.Table {
+ return rcv._tab
+}
+
+func (rcv *Pizza) Size() int32 {
+ o := flatbuffers.UOffsetT(rcv._tab.Offset(4))
+ if o != 0 {
+ return rcv._tab.GetInt32(o + rcv._tab.Pos)
+ }
+ return 0
+}
+
+func (rcv *Pizza) MutateSize(n int32) bool {
+ return rcv._tab.MutateInt32Slot(4, n)
+}
+
+func PizzaStart(builder *flatbuffers.Builder) {
+ builder.StartObject(1)
+}
+func PizzaAddSize(builder *flatbuffers.Builder, size int32) {
+ builder.PrependInt32Slot(0, size, 0)
+}
+func PizzaEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
+ return builder.EndObject()
+}
diff --git a/tests/go_test.go b/tests/go_test.go
index a04ef2c9..d454b564 100644
--- a/tests/go_test.go
+++ b/tests/go_test.go
@@ -17,6 +17,8 @@
package main
import (
+ order "order"
+ pizza "Pizza"
mygame "MyGame" // refers to generated code
example "MyGame/Example" // refers to generated code
"encoding/json"
@@ -98,6 +100,24 @@ func TestTextParsing(t *testing.T) {
}
}
+func CheckNoNamespaceImport(fail func(string, ...interface{})) {
+ const size = 13
+ // Order a pizza with specific size
+ builder := flatbuffers.NewBuilder(0)
+ ordered_pizza := pizza.PizzaT{Size: size}
+ food := order.FoodT{Pizza: &ordered_pizza}
+ builder.Finish(food.Pack(builder))
+
+ // Receive order
+ received_food := order.GetRootAsFood(builder.FinishedBytes(), 0)
+ received_pizza := received_food.Pizza(nil).UnPack()
+
+ // Check if received pizza is equal to ordered pizza
+ if !reflect.DeepEqual(ordered_pizza, *received_pizza) {
+ fail(FailString("no namespace import", ordered_pizza, received_pizza))
+ }
+}
+
// TestAll runs all checks, failing if any errors occur.
func TestAll(t *testing.T) {
// Verify that the Go FlatBuffers runtime library generates the
@@ -160,6 +180,9 @@ func TestAll(t *testing.T) {
// Check a parent namespace import
CheckParentNamespace(t.Fatalf)
+ // Check a no namespace import
+ CheckNoNamespaceImport(t.Fatalf)
+
// Check size-prefixed flatbuffers
CheckSizePrefixedBuffer(t.Fatalf)
diff --git a/tests/include_test/order.fbs b/tests/include_test/order.fbs
new file mode 100644
index 00000000..4588d5fd
--- /dev/null
+++ b/tests/include_test/order.fbs
@@ -0,0 +1,8 @@
+include "no_namespace.fbs";
+
+namespace order;
+
+table Food {
+ pizza: Pizza (id: 0);
+ pizza_test:Pizza(id:1);
+}
diff --git a/tests/include_test/sub/no_namespace.fbs b/tests/include_test/sub/no_namespace.fbs
new file mode 100644
index 00000000..5f1052d1
--- /dev/null
+++ b/tests/include_test/sub/no_namespace.fbs
@@ -0,0 +1,3 @@
+table Pizza {
+ size: int;
+}
diff --git a/tests/order/Food.go b/tests/order/Food.go
new file mode 100644
index 00000000..298d6312
--- /dev/null
+++ b/tests/order/Food.go
@@ -0,0 +1,102 @@
+// Code generated by the FlatBuffers compiler. DO NOT EDIT.
+
+package order
+
+import (
+ flatbuffers "github.com/google/flatbuffers/go"
+
+ Pizza "Pizza"
+)
+
+type FoodT struct {
+ Pizza *Pizza.PizzaT `json:"pizza"`
+ PizzaTest *Pizza.PizzaT `json:"pizza_test"`
+}
+
+func (t *FoodT) Pack(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
+ if t == nil { return 0 }
+ pizzaOffset := t.Pizza.Pack(builder)
+ pizzaTestOffset := t.PizzaTest.Pack(builder)
+ FoodStart(builder)
+ FoodAddPizza(builder, pizzaOffset)
+ FoodAddPizzaTest(builder, pizzaTestOffset)
+ return FoodEnd(builder)
+}
+
+func (rcv *Food) UnPackTo(t *FoodT) {
+ t.Pizza = rcv.Pizza(nil).UnPack()
+ t.PizzaTest = rcv.PizzaTest(nil).UnPack()
+}
+
+func (rcv *Food) UnPack() *FoodT {
+ if rcv == nil { return nil }
+ t := &FoodT{}
+ rcv.UnPackTo(t)
+ return t
+}
+
+type Food struct {
+ _tab flatbuffers.Table
+}
+
+func GetRootAsFood(buf []byte, offset flatbuffers.UOffsetT) *Food {
+ n := flatbuffers.GetUOffsetT(buf[offset:])
+ x := &Food{}
+ x.Init(buf, n+offset)
+ return x
+}
+
+func GetSizePrefixedRootAsFood(buf []byte, offset flatbuffers.UOffsetT) *Food {
+ n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:])
+ x := &Food{}
+ x.Init(buf, n+offset+flatbuffers.SizeUint32)
+ return x
+}
+
+func (rcv *Food) Init(buf []byte, i flatbuffers.UOffsetT) {
+ rcv._tab.Bytes = buf
+ rcv._tab.Pos = i
+}
+
+func (rcv *Food) Table() flatbuffers.Table {
+ return rcv._tab
+}
+
+func (rcv *Food) Pizza(obj *Pizza.Pizza) *Pizza.Pizza {
+ o := flatbuffers.UOffsetT(rcv._tab.Offset(4))
+ if o != 0 {
+ x := rcv._tab.Indirect(o + rcv._tab.Pos)
+ if obj == nil {
+ obj = new(Pizza.Pizza)
+ }
+ obj.Init(rcv._tab.Bytes, x)
+ return obj
+ }
+ return nil
+}
+
+func (rcv *Food) PizzaTest(obj *Pizza.Pizza) *Pizza.Pizza {
+ o := flatbuffers.UOffsetT(rcv._tab.Offset(6))
+ if o != 0 {
+ x := rcv._tab.Indirect(o + rcv._tab.Pos)
+ if obj == nil {
+ obj = new(Pizza.Pizza)
+ }
+ obj.Init(rcv._tab.Bytes, x)
+ return obj
+ }
+ return nil
+}
+
+func FoodStart(builder *flatbuffers.Builder) {
+ builder.StartObject(2)
+}
+func FoodAddPizza(builder *flatbuffers.Builder, pizza flatbuffers.UOffsetT) {
+ builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(pizza), 0)
+}
+func FoodAddPizzaTest(builder *flatbuffers.Builder, pizzaTest flatbuffers.UOffsetT) {
+ builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(pizzaTest), 0)
+}
+func FoodEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
+ return builder.EndObject()
+}