Skip to content

Commit

Permalink
Add tests for GetRootAs* in Go and Python
Browse files Browse the repository at this point in the history
  • Loading branch information
danring committed Jun 14, 2016
1 parent 1f8e3c1 commit 4832238
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/go_test.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions tests/py_test.py
Expand Up @@ -1033,6 +1033,23 @@ def test_nondefault_monster_testhashes(self):
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. '''
Expand Down

0 comments on commit 4832238

Please sign in to comment.