Home | History | Annotate | Download | only in tests
      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 include from "../lobster/"
     16 include "monster_test_generated.lobster"
     17 
     18 def 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 
     64 def 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     b.MyGame_Example_MonsterStart()
     74     b.MyGame_Example_MonsterAddName(fred)
     75     let mon2 = b.MyGame_Example_MonsterEnd()
     76 
     77     b.MyGame_Example_MonsterStartTest4Vector(2)
     78     b.MyGame_Example_CreateTest(10, 20)
     79     b.MyGame_Example_CreateTest(30, 40)
     80     let test4 = b.EndVector(2)
     81 
     82     let test_array_of_string = b.MyGame_Example_MonsterCreateTestarrayofstringVector(
     83                                    [ b.CreateString("test1"), b.CreateString("test2") ])
     84 
     85     let vector_of_longs = b.MyGame_Example_MonsterCreateVectorOfLongsVector(
     86                               [ 1, 100, 10000, 1000000, 100000000 ])
     87 
     88     let vector_of_doubles = b.MyGame_Example_MonsterCreateVectorOfDoublesVector(
     89                                 [ -1.7976931348623157e+308, 0, 1.7976931348623157e+308 ])
     90 
     91     b.MyGame_Example_MonsterStart()
     92     b.MyGame_Example_MonsterAddPos(b.MyGame_Example_CreateVec3(1.0, 2.0, 3.0, 3.0, 2, 5, 6))
     93     b.MyGame_Example_MonsterAddHp(80)
     94     b.MyGame_Example_MonsterAddName(name)
     95     b.MyGame_Example_MonsterAddInventory(inv)
     96     b.MyGame_Example_MonsterAddTestType(MyGame_Example_Any_Monster)
     97     b.MyGame_Example_MonsterAddTest(mon2)
     98     b.MyGame_Example_MonsterAddTest4(test4)
     99     b.MyGame_Example_MonsterAddTestarrayofstring(test_array_of_string)
    100     b.MyGame_Example_MonsterAddVectorOfLongs(vector_of_longs)
    101     b.MyGame_Example_MonsterAddVectorOfDoubles(vector_of_doubles)
    102     let mon = b.MyGame_Example_MonsterEnd()
    103 
    104     b.Finish(mon)
    105 
    106     return b.SizedCopy()
    107 
    108 // Verify that the canonical flatbuffer file (produced by the C++ implementation)
    109 // is readable by the generated Lobster code.
    110 let fb2 = read_file("monsterdata_test.mon")
    111 assert fb2
    112 check_read_buffer(fb2)
    113 
    114 // Verify that using the generated Lobster code builds a buffer without
    115 // returning errors, and is interpreted correctly.
    116 let fb1 = make_monster_from_generated_code()
    117 check_read_buffer(fb1)
    118 // Write the result to file for no good reason.
    119 write_file("monsterdata_lobster_wire.mon", fb1)
    120 
    121 // Test converting the buffer to JSON and parsing the JSON back again.
    122 schema := read_file("monster_test.fbs")
    123 assert schema
    124 includedirs := [ "include_test" ]
    125 // Convert binary to JSON:
    126 json, err1 := flatbuffers_binary_to_json(schema, fb1, includedirs)
    127 assert not err1
    128 // Parse JSON back to binary:
    129 fb3, err2 := flatbuffers_json_to_binary(schema, json, includedirs)
    130 assert not err2
    131 // Check the resulting binary again (full roundtrip test):
    132 check_read_buffer(fb3)
    133 
    134 print "Lobster test succesful!"