summaryrefslogtreecommitdiff
path: root/tests/lobstertest.lobster
diff options
context:
space:
mode:
authorWouter van Oortmerssen <aardappel@gmail.com>2020-08-18 14:00:02 -0700
committerWouter van Oortmerssen <aardappel@gmail.com>2020-08-18 14:00:02 -0700
commit77f966f89fcf76fa67485adf941ac6e90d28029c (patch)
tree4f168bc6acfafa09c71e428b966b33021bbaa28e /tests/lobstertest.lobster
parente86d5b8e97c09d4eac46dbbb8541f180d05f1da2 (diff)
downloadflatbuffers-77f966f89fcf76fa67485adf941ac6e90d28029c.tar.gz
flatbuffers-77f966f89fcf76fa67485adf941ac6e90d28029c.tar.bz2
flatbuffers-77f966f89fcf76fa67485adf941ac6e90d28029c.zip
[Lobster] optional scalars support
Diffstat (limited to 'tests/lobstertest.lobster')
-rw-r--r--tests/lobstertest.lobster28
1 files changed, 27 insertions, 1 deletions
diff --git a/tests/lobstertest.lobster b/tests/lobstertest.lobster
index 7ea9b388..ac42c2e8 100644
--- a/tests/lobstertest.lobster
+++ b/tests/lobstertest.lobster
@@ -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!" \ No newline at end of file
+// Additional tests.
+test_optional_scalars()
+
+print "Lobster test succesful!"