[Lobster] optional scalars support

This commit is contained in:
Wouter van Oortmerssen
2020-08-18 14:00:02 -07:00
parent e86d5b8e97
commit 77f966f89f
7 changed files with 237 additions and 6 deletions

View File

@@ -14,6 +14,7 @@
import from "../lobster/"
import monster_test_generated
import optional_scalars_generated
def check_read_buffer(buf):
// CheckReadBuffer checks that the given buffer is evaluated correctly as the example Monster.
@@ -108,6 +109,28 @@ def make_monster_from_generated_code():
return b.SizedCopy()
def test_optional_scalars():
def build(add_fields):
let b = flatbuffers_builder {}
let ss = optional_scalars_ScalarStuffBuilder { b }.start()
if add_fields:
ss.add_just_i8(1)
ss.add_maybe_i8(1)
ss.add_default_i8(1)
b.Finish(ss.end())
return optional_scalars_GetRootAsScalarStuff(b.SizedCopy())
var root = build(true)
assert root.just_i8() == 1 and root.default_i8() == 1
var maybe_val, maybe_present = root.maybe_i8()
assert maybe_val == 1 and maybe_present == true
root = build(false)
assert root.just_i8() == 0 and root.default_i8() == 42
maybe_val, maybe_present = root.maybe_i8()
assert maybe_val == 0 and maybe_present == false
// Verify that the canonical flatbuffer file (produced by the C++ implementation)
// is readable by the generated Lobster code.
let fb2 = read_file("monsterdata_test.mon")
@@ -134,4 +157,7 @@ assert not err2
// Check the resulting binary again (full roundtrip test):
check_read_buffer(fb3)
print "Lobster test succesful!"
// Additional tests.
test_optional_scalars()
print "Lobster test succesful!"