summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Ring <dfring@gmail.com>2016-01-06 17:47:57 -0800
committerDan Ring <dfring@gmail.com>2016-06-14 18:41:33 -0400
commit483223870852aac6819a4147605447b552bb3b33 (patch)
tree06792702dc94e9141bd47e6e88a8c5ef2b2e0113
parent1f8e3c13e6bbdedd30abc70db0e0cd6c538bf9d8 (diff)
downloadflatbuffers-483223870852aac6819a4147605447b552bb3b33.tar.gz
flatbuffers-483223870852aac6819a4147605447b552bb3b33.tar.bz2
flatbuffers-483223870852aac6819a4147605447b552bb3b33.zip
Add tests for GetRootAs* in Go and Python
-rw-r--r--tests/go_test.go28
-rw-r--r--tests/py_test.py17
2 files changed, 45 insertions, 0 deletions
diff --git a/tests/go_test.go b/tests/go_test.go
index 9d94277f..4d0067f4 100644
--- a/tests/go_test.go
+++ b/tests/go_test.go
@@ -75,6 +75,9 @@ func TestAll(t *testing.T) {
CheckStructIsNotInlineError(t.Fatalf)
CheckFinishedBytesError(t.Fatalf)
+ // Verify that GetRootAs works for non-root tables
+ CheckGetRootAsForNonRootTable(t.Fatalf)
+
// Verify that using the generated Go code builds a buffer without
// returning errors:
generated, off := CheckGeneratedBuild(t.Fatalf)
@@ -964,6 +967,31 @@ func CheckManualBuild(fail func(string, ...interface{})) ([]byte, flatbuffers.UO
return b.Bytes, b.Head()
}
+func CheckGetRootAsForNonRootTable(fail func(string, ...interface{})) {
+ b := flatbuffers.NewBuilder(0)
+ str := b.CreateString("MyStat")
+ example.StatStart(b)
+ example.StatAddId(b, str)
+ example.StatAddVal(b, 12345678)
+ example.StatAddCount(b, 12345)
+ stat_end := example.StatEnd(b)
+ b.Finish(stat_end)
+
+ stat := example.GetRootAsStat(b.Bytes, b.Head())
+
+ if got := stat.Id(); !bytes.Equal([]byte("MyStat"), got) {
+ fail(FailString("stat.Id()", "MyStat", got))
+ }
+
+ if got := stat.Val(); 12345678 != got {
+ fail(FailString("stat.Val()", 12345678, got))
+ }
+
+ if got := stat.Count(); 12345 != got {
+ fail(FailString("stat.Count()", 12345, got))
+ }
+}
+
// CheckGeneratedBuild uses generated code to build the example Monster.
func CheckGeneratedBuild(fail func(string, ...interface{})) ([]byte, flatbuffers.UOffsetT) {
b := flatbuffers.NewBuilder(0)
diff --git a/tests/py_test.py b/tests/py_test.py
index d12cfb43..9129de70 100644
--- a/tests/py_test.py
+++ b/tests/py_test.py
@@ -1033,6 +1033,23 @@ class TestAllCodePathsOfExampleSchema(unittest.TestCase):
self.assertEqual(7, mon2.Testhashs64Fnv1a())
self.assertEqual(8, mon2.Testhashu64Fnv1a())
+ def test_getrootas_for_nonroot_table(self):
+ b = flatbuffers.Builder(0)
+ string = b.CreateString("MyStat")
+
+ MyGame.Example.Stat.StatStart(b)
+ MyGame.Example.Stat.StatAddId(b, string)
+ MyGame.Example.Stat.StatAddVal(b, 12345678)
+ MyGame.Example.Stat.StatAddCount(b, 12345)
+ stat = MyGame.Example.Stat.StatEnd(b)
+ b.Finish(stat)
+
+ stat2 = MyGame.Example.Stat.Stat.GetRootAsStat(b.Bytes, b.Head())
+
+ self.assertEqual(b"MyStat", stat2.Id())
+ self.assertEqual(12345678, stat2.Val())
+ self.assertEqual(12345, stat2.Count())
+
class TestVtableDeduplication(unittest.TestCase):
''' TestVtableDeduplication verifies that vtables are deduplicated. '''