• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import from "../lobster/"
16import monster_test_generated
17import optional_scalars_generated
18
19def check_read_buffer(buf):
20    // CheckReadBuffer checks that the given buffer is evaluated correctly as the example Monster.
21    let monster = MyGame_Example_GetRootAsMonster(buf)
22
23    assert monster.hp == 80
24    assert monster.mana == 150
25    assert monster.name == "MyMonster"
26
27    let vec = monster.pos
28    assert vec
29    assert vec.x == 1.0
30    assert vec.y == 2.0
31    assert vec.z == 3.0
32    assert vec.test1 == 3.0
33    assert vec.test2 == 2
34
35    let t = vec.test3
36    assert t
37    assert t.a == 5
38    assert t.b == 6
39
40    assert monster.test_type == MyGame_Example_Any_Monster
41    assert monster.test_as_Monster.name == "Fred"
42
43    assert monster.inventory_length == 5
44    assert sum(map(monster.inventory_length) i: monster.inventory(i)) == 10
45
46    for(5) i:
47        assert monster.vector_of_longs(i) == pow(10, i * 2)
48
49    assert equal([-1.7976931348623157e+308, 0, 1.7976931348623157e+308],
50                 (map(monster.vector_of_doubles_length) i: monster.vector_of_doubles(i)))
51
52    assert monster.test4_length == 2
53    let test0 = monster.test4(0)
54    let test1 = monster.test4(1)
55    assert test0.a + test0.b + test1.a + test1.b == 100
56
57    assert monster.testarrayofstring_length == 2
58    assert monster.testarrayofstring(0) == "test1"
59    assert monster.testarrayofstring(1) == "test2"
60
61    assert monster.testarrayoftables_length == 0
62    assert monster.testnestedflatbuffer_length == 0
63    assert not monster.testempty()
64
65def make_monster_from_generated_code():
66    // Use generated code to build the example Monster.
67    let b = flatbuffers_builder {}
68
69    let name = b.CreateString("MyMonster")
70    let fred = b.CreateString("Fred")
71
72    let inv = b.MyGame_Example_MonsterCreateInventoryVector([ 0, 1, 2, 3, 4 ])
73
74    let mon2 = MyGame_Example_MonsterBuilder { b }
75        .start()
76        .add_name(fred)
77        .end()
78
79    b.MyGame_Example_MonsterStartTest4Vector(2)
80    b.MyGame_Example_CreateTest(10, 20)
81    b.MyGame_Example_CreateTest(30, 40)
82    let test4 = b.EndVector(2)
83
84    let test_array_of_string = b.MyGame_Example_MonsterCreateTestarrayofstringVector(
85                                   [ b.CreateString("test1"), b.CreateString("test2") ])
86
87    let vector_of_longs = b.MyGame_Example_MonsterCreateVectorOfLongsVector(
88                              [ 1, 100, 10000, 1000000, 100000000 ])
89
90    let vector_of_doubles = b.MyGame_Example_MonsterCreateVectorOfDoublesVector(
91                                [ -1.7976931348623157e+308, 0, 1.7976931348623157e+308 ])
92
93    let mon = MyGame_Example_MonsterBuilder { b }
94        .start()
95        .add_pos(b.MyGame_Example_CreateVec3(1.0, 2.0, 3.0, 3.0,
96                 MyGame_Example_Color_Green, 5, 6))
97        .add_hp(80)
98        .add_name(name)
99        .add_inventory(inv)
100        .add_test_type(MyGame_Example_Any_Monster)
101        .add_test(mon2)
102        .add_test4(test4)
103        .add_testarrayofstring(test_array_of_string)
104        .add_vector_of_longs(vector_of_longs)
105        .add_vector_of_doubles(vector_of_doubles)
106        .end()
107
108    b.Finish(mon)
109
110    return b.SizedCopy()
111
112def test_optional_scalars():
113    def build(add_fields):
114        let b = flatbuffers_builder {}
115        let ss = optional_scalars_ScalarStuffBuilder { b }.start()
116        if add_fields:
117            ss.add_just_i8(1)
118            ss.add_maybe_i8(1)
119            ss.add_default_i8(1)
120            ss.add_just_f64(1.0)
121            ss.add_maybe_f64(1.0)
122            ss.add_default_f64(1.0)
123            ss.add_just_bool(true)
124            ss.add_maybe_bool(true)
125            ss.add_default_bool(true)
126            ss.add_just_enum(optional_scalars_OptionalByte_Two)
127            ss.add_maybe_enum(optional_scalars_OptionalByte_Two)
128            ss.add_default_enum(optional_scalars_OptionalByte_Two)
129        b.Finish(ss.end())
130        return optional_scalars_GetRootAsScalarStuff(b.SizedCopy())
131
132    var root = build(true)
133
134    assert root.just_i8() == 1 and root.default_i8() == 1
135    var maybe_val_i8, maybe_present_i8 = root.maybe_i8()
136    assert maybe_val_i8 == 1 and maybe_present_i8 == true
137
138    assert root.just_f64() == 1.0 and root.default_f64() == 1.0
139    var maybe_val_f64, maybe_present_f64 = root.maybe_f64()
140    assert maybe_val_f64 == 1.0 and maybe_present_f64 == true
141
142    assert root.just_bool() == true and root.default_bool() == true
143    var maybe_val_bool, maybe_present_bool = root.maybe_bool()
144    assert maybe_val_bool == true and maybe_present_bool == true
145
146    assert root.just_enum() == optional_scalars_OptionalByte_Two and root.default_enum() == optional_scalars_OptionalByte_Two
147    var maybe_val_enum, maybe_present_enum = root.maybe_enum()
148    assert maybe_val_enum == optional_scalars_OptionalByte_Two and maybe_present_enum == true
149
150    root = build(false)
151
152    assert root.just_i8() == 0 and root.default_i8() == 42
153    maybe_val_i8, maybe_present_i8 = root.maybe_i8()
154    assert maybe_val_i8 == 0 and maybe_present_i8 == false
155
156    assert root.just_f64() == 0.0 and root.default_f64() == 42.0
157    maybe_val_f64, maybe_present_f64 = root.maybe_f64()
158    assert maybe_val_f64 == 0.0 and maybe_present_f64 == false
159
160    assert root.just_bool() == false and root.default_bool() == true
161    maybe_val_bool, maybe_present_bool = root.maybe_bool()
162    assert maybe_val_bool == false and maybe_present_bool == false
163
164    assert root.just_enum() == optional_scalars_OptionalByte_None and root.default_enum() == optional_scalars_OptionalByte_One
165    maybe_val_enum, maybe_present_enum = root.maybe_enum()
166    assert maybe_val_enum == optional_scalars_OptionalByte_None and maybe_present_enum == false
167
168
169// Verify that the canonical flatbuffer file (produced by the C++ implementation)
170// is readable by the generated Lobster code.
171let fb2 = read_file("monsterdata_test.mon")
172assert fb2
173check_read_buffer(fb2)
174
175// Verify that using the generated Lobster code builds a buffer without
176// returning errors, and is interpreted correctly.
177let fb1 = make_monster_from_generated_code()
178check_read_buffer(fb1)
179// Write the result to file for no good reason.
180write_file("monsterdata_lobster_wire.mon", fb1)
181
182// Test converting the buffer to JSON and parsing the JSON back again.
183let schema = read_file("monster_test.fbs")
184assert schema
185let includedirs = [ "include_test" ]
186// Convert binary to JSON:
187let json, err1 = flatbuffers_binary_to_json(schema, fb1, includedirs)
188assert not err1
189// Parse JSON back to binary:
190let fb3, err2 = flatbuffers_json_to_binary(schema, json, includedirs)
191assert not err2
192// Check the resulting binary again (full roundtrip test):
193check_read_buffer(fb3)
194
195// Additional tests.
196test_optional_scalars()
197
198print "Lobster test succesful!"
199