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 17 18def check_read_buffer(buf): 19 // CheckReadBuffer checks that the given buffer is evaluated correctly as the example Monster. 20 let monster = MyGame_Example_GetRootAsMonster(buf) 21 22 assert monster.hp == 80 23 assert monster.mana == 150 24 assert monster.name == "MyMonster" 25 26 let vec = monster.pos 27 assert vec 28 assert vec.x == 1.0 29 assert vec.y == 2.0 30 assert vec.z == 3.0 31 assert vec.test1 == 3.0 32 assert vec.test2 == 2 33 34 let t = vec.test3 35 assert t 36 assert t.a == 5 37 assert t.b == 6 38 39 assert monster.test_type == MyGame_Example_Any_Monster 40 assert monster.test_as_Monster.name == "Fred" 41 42 assert monster.inventory_length == 5 43 assert sum(map(monster.inventory_length) i: monster.inventory(i)) == 10 44 45 for(5) i: 46 assert monster.vector_of_longs(i) == pow(10, i * 2) 47 48 assert equal([-1.7976931348623157e+308, 0, 1.7976931348623157e+308], 49 (map(monster.vector_of_doubles_length) i: monster.vector_of_doubles(i))) 50 51 assert monster.test4_length == 2 52 let test0 = monster.test4(0) 53 let test1 = monster.test4(1) 54 assert test0.a + test0.b + test1.a + test1.b == 100 55 56 assert monster.testarrayofstring_length == 2 57 assert monster.testarrayofstring(0) == "test1" 58 assert monster.testarrayofstring(1) == "test2" 59 60 assert monster.testarrayoftables_length == 0 61 assert monster.testnestedflatbuffer_length == 0 62 assert not monster.testempty() 63 64def make_monster_from_generated_code(): 65 // Use generated code to build the example Monster. 66 let b = flatbuffers_builder {} 67 68 let name = b.CreateString("MyMonster") 69 let fred = b.CreateString("Fred") 70 71 let inv = b.MyGame_Example_MonsterCreateInventoryVector([ 0, 1, 2, 3, 4 ]) 72 73 let mon2 = MyGame_Example_MonsterBuilder { b } 74 .start() 75 .add_name(fred) 76 .end() 77 78 b.MyGame_Example_MonsterStartTest4Vector(2) 79 b.MyGame_Example_CreateTest(10, 20) 80 b.MyGame_Example_CreateTest(30, 40) 81 let test4 = b.EndVector(2) 82 83 let test_array_of_string = b.MyGame_Example_MonsterCreateTestarrayofstringVector( 84 [ b.CreateString("test1"), b.CreateString("test2") ]) 85 86 let vector_of_longs = b.MyGame_Example_MonsterCreateVectorOfLongsVector( 87 [ 1, 100, 10000, 1000000, 100000000 ]) 88 89 let vector_of_doubles = b.MyGame_Example_MonsterCreateVectorOfDoublesVector( 90 [ -1.7976931348623157e+308, 0, 1.7976931348623157e+308 ]) 91 92 let mon = MyGame_Example_MonsterBuilder { b } 93 .start() 94 .add_pos(b.MyGame_Example_CreateVec3(1.0, 2.0, 3.0, 3.0, 95 MyGame_Example_Color_Green, 5, 6)) 96 .add_hp(80) 97 .add_name(name) 98 .add_inventory(inv) 99 .add_test_type(MyGame_Example_Any_Monster) 100 .add_test(mon2) 101 .add_test4(test4) 102 .add_testarrayofstring(test_array_of_string) 103 .add_vector_of_longs(vector_of_longs) 104 .add_vector_of_doubles(vector_of_doubles) 105 .end() 106 107 b.Finish(mon) 108 109 return b.SizedCopy() 110 111// Verify that the canonical flatbuffer file (produced by the C++ implementation) 112// is readable by the generated Lobster code. 113let fb2 = read_file("monsterdata_test.mon") 114assert fb2 115check_read_buffer(fb2) 116 117// Verify that using the generated Lobster code builds a buffer without 118// returning errors, and is interpreted correctly. 119let fb1 = make_monster_from_generated_code() 120check_read_buffer(fb1) 121// Write the result to file for no good reason. 122write_file("monsterdata_lobster_wire.mon", fb1) 123 124// Test converting the buffer to JSON and parsing the JSON back again. 125let schema = read_file("monster_test.fbs") 126assert schema 127let includedirs = [ "include_test" ] 128// Convert binary to JSON: 129let json, err1 = flatbuffers_binary_to_json(schema, fb1, includedirs) 130assert not err1 131// Parse JSON back to binary: 132let fb3, err2 = flatbuffers_json_to_binary(schema, json, includedirs) 133assert not err2 134// Check the resulting binary again (full roundtrip test): 135check_read_buffer(fb3) 136 137print "Lobster test succesful!"