| 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 |
|
| 15 |
import from "../lobster/" |
| 16 |
import monster_test_generated |
| 17 |
import optional_scalars_generated |
| 18 |
|
| 19 |
def check_read_buffer(buf): |
| 20 |
// Check that the given buffer is evaluated correctly as the example Monster. |
| 21 |
assert flatbuffers.has_identifier(buf, "MONS") |
| 22 |
|
| 23 |
let monster = MyGame.Example.GetRootAsMonster(buf) |
| 24 |
|
| 25 |
assert monster.hp == 80 |
| 26 |
assert monster.mana == 150 |
| 27 |
assert monster.name == "MyMonster" |
| 28 |
|
| 29 |
let vec = monster.pos |
| 30 |
assert vec |
| 31 |
assert vec.x == 1.0 |
| 32 |
assert vec.y == 2.0 |
| 33 |
assert vec.z == 3.0 |
| 34 |
assert vec.test1 == 3.0 |
| 35 |
assert vec.test2 == 2 |
| 36 |
|
| 37 |
let t = vec.test3 |
| 38 |
assert t |
| 39 |
assert t.a == 5 |
| 40 |
assert t.b == 6 |
| 41 |
|
| 42 |
assert monster.test_type == MyGame.Example.Any_Monster |
| 43 |
assert monster.test_as_Monster.name == "Fred" |
| 44 |
|
| 45 |
assert monster.inventory_length == 5 |
| 46 |
assert sum(map(monster.inventory_length) i: monster.inventory(i)) == 10 |
| 47 |
|
| 48 |
for(5) i: |
| 49 |
assert monster.vector_of_longs(i) == pow(10, i * 2) |
| 50 |
|
| 51 |
assert equal([-1.7976931348623157e+308, 0.0, 1.7976931348623157e+308], |
| 52 |
(map(monster.vector_of_doubles_length) i: monster.vector_of_doubles(i))) |
| 53 |
|
| 54 |
assert monster.test4_length == 2 |
| 55 |
let test0 = monster.test4(0) |
| 56 |
let test1 = monster.test4(1) |
| 57 |
assert test0.a + test0.b + test1.a + test1.b == 100 |
| 58 |
|
| 59 |
assert monster.testarrayofstring_length == 2 |
| 60 |
assert monster.testarrayofstring(0) == "test1" |
| 61 |
assert monster.testarrayofstring(1) == "test2" |
| 62 |
|
| 63 |
assert monster.testarrayoftables_length == 0 |
| 64 |
assert monster.testnestedflatbuffer_length == 0 |
| 65 |
assert not monster.testempty() |
| 66 |
|
| 67 |
def make_monster_from_generated_code(): |
| 68 |
// Use generated code to build the example Monster. |
| 69 |
let b = flatbuffers.builder {} |
| 70 |
|
| 71 |
let name = b.CreateString("MyMonster") |
| 72 |
let fred = b.CreateString("Fred") |
| 73 |
|
| 74 |
let inv = b.MyGame.Example.MonsterCreateInventoryVector([ 0, 1, 2, 3, 4 ]) |
| 75 |
|
| 76 |
let mon2 = MyGame.Example.MonsterBuilder { b } |
| 77 |
.start() |
| 78 |
.add_name(fred) |
| 79 |
.end() |
| 80 |
|
| 81 |
b.MyGame.Example.MonsterStartTest4Vector(2) |
| 82 |
b.MyGame.Example.CreateTest(10, 20) |
| 83 |
b.MyGame.Example.CreateTest(30, 40) |
| 84 |
let test4 = b.EndVector(2) |
| 85 |
|
| 86 |
let test_array_of_string = b.MyGame.Example.MonsterCreateTestarrayofstringVector( |
| 87 |
[ b.CreateString("test1"), b.CreateString("test2") ]) |
| 88 |
|
| 89 |
let vector_of_longs = b.MyGame.Example.MonsterCreateVectorOfLongsVector( |
| 90 |
[ 1, 100, 10000, 1000000, 100000000 ]) |
| 91 |
|
| 92 |
let vector_of_doubles = b.MyGame.Example.MonsterCreateVectorOfDoublesVector( |
| 93 |
[ -1.7976931348623157e+308, 0.0, 1.7976931348623157e+308 ]) |
| 94 |
|
| 95 |
let mon = MyGame.Example.MonsterBuilder { b } |
| 96 |
.start() |
| 97 |
.add_pos(b.MyGame.Example.CreateVec3(1.0, 2.0, 3.0, 3.0, |
| 98 |
MyGame.Example.Color_Green, 5, 6)) |
| 99 |
.add_hp(80) |
| 100 |
.add_name(name) |
| 101 |
.add_inventory(inv) |
| 102 |
.add_test_type(MyGame.Example.Any_Monster) |
| 103 |
.add_test(mon2) |
| 104 |
.add_test4(test4) |
| 105 |
.add_testarrayofstring(test_array_of_string) |
| 106 |
.add_vector_of_longs(vector_of_longs) |
| 107 |
.add_vector_of_doubles(vector_of_doubles) |
| 108 |
.end() |
| 109 |
|
| 110 |
b.Finish(mon, "MONS") |
| 111 |
|
| 112 |
return b.SizedCopy() |
| 113 |
|
| 114 |
def test_optional_scalars(): |
| 115 |
def build(add_fields): |
| 116 |
let b = flatbuffers.builder {} |
| 117 |
let ss = optional_scalars.ScalarStuffBuilder { b }.start() |
| 118 |
if add_fields: |
| 119 |
ss.add_just_i8(1) |
| 120 |
ss.add_maybe_i8(1) |
| 121 |
ss.add_default_i8(1) |
| 122 |
ss.add_just_f64(1.0) |
| 123 |
ss.add_maybe_f64(1.0) |
| 124 |
ss.add_default_f64(1.0) |
| 125 |
ss.add_just_bool(true) |
| 126 |
ss.add_maybe_bool(true) |
| 127 |
ss.add_default_bool(true) |
| 128 |
ss.add_just_enum(optional_scalars.OptionalByte_Two) |
| 129 |
ss.add_maybe_enum(optional_scalars.OptionalByte_Two) |
| 130 |
ss.add_default_enum(optional_scalars.OptionalByte_Two) |
| 131 |
b.Finish(ss.end(), "NULL") |
| 132 |
let buf = b.SizedCopy() |
| 133 |
assert flatbuffers.has_identifier(buf, "NULL") |
| 134 |
return optional_scalars.GetRootAsScalarStuff(buf) |
| 135 |
|
| 136 |
var root = build(true) |
| 137 |
|
| 138 |
assert root.just_i8() == 1 and root.default_i8() == 1 |
| 139 |
var maybe_val_i8, maybe_present_i8 = root.maybe_i8() |
| 140 |
assert maybe_val_i8 == 1 and maybe_present_i8 == true |
| 141 |
|
| 142 |
assert root.just_f64() == 1.0 and root.default_f64() == 1.0 |
| 143 |
var maybe_val_f64, maybe_present_f64 = root.maybe_f64() |
| 144 |
assert maybe_val_f64 == 1.0 and maybe_present_f64 == true |
| 145 |
|
| 146 |
assert root.just_bool() == true and root.default_bool() == true |
| 147 |
var maybe_val_bool, maybe_present_bool = root.maybe_bool() |
| 148 |
assert maybe_val_bool == true and maybe_present_bool == true |
| 149 |
|
| 150 |
assert root.just_enum() == optional_scalars.OptionalByte_Two and root.default_enum() == optional_scalars.OptionalByte_Two |
| 151 |
var maybe_val_enum, maybe_present_enum = root.maybe_enum() |
| 152 |
assert maybe_val_enum == optional_scalars.OptionalByte_Two and maybe_present_enum == true |
| 153 |
|
| 154 |
root = build(false) |
| 155 |
|
| 156 |
assert root.just_i8() == 0 and root.default_i8() == 42 |
| 157 |
maybe_val_i8, maybe_present_i8 = root.maybe_i8() |
| 158 |
assert maybe_val_i8 == 0 and maybe_present_i8 == false |
| 159 |
|
| 160 |
assert root.just_f64() == 0.0 and root.default_f64() == 42.0 |
| 161 |
maybe_val_f64, maybe_present_f64 = root.maybe_f64() |
| 162 |
assert maybe_val_f64 == 0.0 and maybe_present_f64 == false |
| 163 |
|
| 164 |
assert root.just_bool() == false and root.default_bool() == true |
| 165 |
maybe_val_bool, maybe_present_bool = root.maybe_bool() |
| 166 |
assert maybe_val_bool == false and maybe_present_bool == false |
| 167 |
|
| 168 |
assert root.just_enum() == optional_scalars.OptionalByte_None and root.default_enum() == optional_scalars.OptionalByte_One |
| 169 |
maybe_val_enum, maybe_present_enum = root.maybe_enum() |
| 170 |
assert maybe_val_enum == optional_scalars.OptionalByte_None and maybe_present_enum == false |
| 171 |
|
| 172 |
|
| 173 |
// Verify that the canonical flatbuffer file (produced by the C++ implementation) |
| 174 |
// is readable by the generated Lobster code. |
| 175 |
let fb2 = read_file("monsterdata_test.mon") |
| 176 |
assert fb2 |
| 177 |
check_read_buffer(fb2) |
| 178 |
|
| 179 |
// Verify that using the generated Lobster code builds a buffer without |
| 180 |
// returning errors, and is interpreted correctly. |
| 181 |
let fb1 = make_monster_from_generated_code() |
| 182 |
check_read_buffer(fb1) |
| 183 |
// Write the result to file for no good reason. |
| 184 |
write_file("monsterdata_lobster_wire.mon", fb1) |
| 185 |
|
| 186 |
// Test converting the buffer to JSON and parsing the JSON back again. |
| 187 |
let schema = read_file("monster_test.fbs") |
| 188 |
assert schema |
| 189 |
let includedirs = [ "include_test" ] |
| 190 |
// Convert binary to JSON: |
| 191 |
let json, err1 = flatbuffers.binary_to_json(schema, fb1, includedirs) |
| 192 |
assert not err1 |
| 193 |
// Parse JSON back to binary: |
| 194 |
let fb3, err2 = flatbuffers.json_to_binary(schema, json, includedirs) |
| 195 |
assert not err2 |
| 196 |
// Check the resulting binary again (full roundtrip test): |
| 197 |
check_read_buffer(fb3) |
| 198 |
|
| 199 |
// Additional tests. |
| 200 |
test_optional_scalars() |
| 201 |
|
| 202 |
print "Lobster test successful!" |