def test_import(foo, bar): m = capnp._MallocMessageBuilder() foo = m.init_root(foo.Foo) m2 = capnp._MallocMessageBuilder() bar = m2.init_root(bar.Bar) foo.name = 'foo' bar.foo = foo assert bar.foo.name == 'foo'
def test_failed_import(): s = capnp.SchemaParser() s2 = capnp.SchemaParser() foo = s.load(os.path.join(this_dir, 'foo.capnp')) bar = s2.load(os.path.join(this_dir, 'bar.capnp')) m = capnp._MallocMessageBuilder() foo = m.init_root(foo.Foo) m2 = capnp._MallocMessageBuilder() bar = m2.init_root(bar.Bar) foo.name = 'foo' with pytest.raises(Exception): bar.foo = foo
def test_failed_import(): s = capnp.SchemaParser() s2 = capnp.SchemaParser() foo = s.load(os.path.join(this_dir, 'foo.capnp')) bar = s2.load(os.path.join(this_dir, 'bar.capnp')) m = capnp._MallocMessageBuilder() foo = m.init_root(foo.Foo) m2 = capnp._MallocMessageBuilder() bar = m2.init_root(bar.Bar) foo.name = 'foo' with pytest.raises(ValueError): bar.foo = foo
def writeAddressBook(fd): message = capnp._MallocMessageBuilder() addressBook = message.init_root(addressbook.AddressBook) people = addressBook.init('people', 2) alice = people[0] alice.id = 123 alice.name = 'Alice' alice.email = '*****@*****.**' alicePhones = alice.init('phones', 1) alicePhones[0].number = "555-1212" alicePhones[0].type = 'mobile' alice.employment.school = "MIT" bob = people[1] bob.id = 456 bob.name = 'Bob' bob.email = '*****@*****.**' bobPhones = bob.init('phones', 2) bobPhones[0].number = "555-4567" bobPhones[0].type = 'home' bobPhones[1].number = "555-7654" bobPhones[1].type = 'work' bob.employment.unemployed = None capnp._write_packed_message_to_fd(fd, message)
def test_binary_read(all_types): f = open(os.path.join(this_dir, 'all-types.binary'), 'r') root = all_types.TestAllTypes.read(f) check_all_types(root) expectedText = open(os.path.join(this_dir, 'all-types.txt'), 'r').read() assert str(root) + '\n' == expectedText # Test set_root(). builder = capnp._MallocMessageBuilder() builder.set_root(root) check_all_types(builder.get_root(all_types.TestAllTypes)) builder2 = capnp._MallocMessageBuilder() builder2.set_root(builder.get_root(all_types.TestAllTypes)) check_all_types(builder2.get_root(all_types.TestAllTypes))
def writeAddressBook(fd): message = capnp._MallocMessageBuilder() addressBook = message.init_root(addressbook.AddressBook) people = addressBook.init("people", 2) alice = people[0] alice.id = 123 alice.name = "Alice" alice.email = "*****@*****.**" alicePhones = alice.init("phones", 1) alicePhones[0].number = "555-1212" alicePhones[0].type = "mobile" alice.employment.school = "MIT" bob = people[1] bob.id = 456 bob.name = "Bob" bob.email = "*****@*****.**" bobPhones = bob.init("phones", 2) bobPhones[0].number = "555-4567" bobPhones[0].type = "home" bobPhones[1].number = "555-7654" bobPhones[1].type = "work" bob.employment.unemployed = None capnp._write_packed_message_to_fd(fd, message)
def test_binary_read(all_types): f = open(os.path.join(this_dir, "all-types.binary"), "r", encoding="utf8") root = all_types.TestAllTypes.read(f) check_all_types(root) expectedText = open(os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8").read() assert str(root) + "\n" == expectedText # Test set_root(). builder = capnp._MallocMessageBuilder() builder.set_root(root) check_all_types(builder.get_root(all_types.TestAllTypes)) builder2 = capnp._MallocMessageBuilder() builder2.set_root(builder.get_root(all_types.TestAllTypes)) check_all_types(builder2.get_root(all_types.TestAllTypes))
def writeAddressBook(fd): message = capnp._MallocMessageBuilder() addressBook = message.init_root(addressbook.AddressBook) people = addressBook.init('people', 2) alice = people[0] alice.employment.school = "MIT" bob = people[1] bob.employment.unemployed = None capnp._write_packed_message_to_fd(fd, message)
def test_object_basic(addressbook): obj = capnp._MallocMessageBuilder().get_root_as_any() person = obj.as_struct(addressbook.Person) person.name = 'test' person.id = 1000 same_person = obj.as_struct(addressbook.Person) assert same_person.name == 'test' assert same_person.id == 1000 obj_r = obj.as_reader() same_person = obj_r.as_struct(addressbook.Person) assert same_person.name == 'test' assert same_person.id == 1000
def test_object_list(addressbook): obj = capnp._MallocMessageBuilder().get_root_as_any() listSchema = capnp.ListSchema(addressbook.Person) people = obj.init_as_list(listSchema, 2) person = people[0] person.name = 'test' person.id = 1000 person = people[1] person.name = 'test2' person.id = 1001 same_person = obj.as_list(listSchema) assert same_person[0].name == 'test' assert same_person[0].id == 1000 assert same_person[1].name == 'test2' assert same_person[1].id == 1001 obj_r = obj.as_reader() same_person = obj_r.as_list(listSchema) assert same_person[0].name == 'test' assert same_person[0].id == 1000 assert same_person[1].name == 'test2' assert same_person[1].id == 1001
def test_object_list(addressbook): obj = capnp._MallocMessageBuilder().get_root_as_any() listSchema = capnp._ListSchema(addressbook.Person) people = obj.init_as_list(listSchema, 2) person = people[0] person.name = 'test' person.id = 1000 person = people[1] person.name = 'test2' person.id = 1001 same_person = obj.as_list(listSchema) assert same_person[0].name == 'test' assert same_person[0].id == 1000 assert same_person[1].name == 'test2' assert same_person[1].id == 1001 obj_r = obj.as_reader() same_person = obj_r.as_list(listSchema) assert same_person[0].name == 'test' assert same_person[0].id == 1000 assert same_person[1].name == 'test2' assert same_person[1].id == 1001
def test_generic(): client = capability.TestGeneric._new_client(TestGeneric()) obj = capnp._MallocMessageBuilder().get_root_as_any() obj.set_as_text("anypointer_") assert client.foo(obj).wait().b == 'anypointer_test'