def test_bundle_constructor(self): bundle = osc.OSCBundle( messages=[osc.OSCMessage('/param'), osc.OSCMessage('/param2')]) self.assertEqual(2, bundle.length)
def test_send(self, mock_socket_ctor): mock_socket = mock_socket_ctor.return_value msg = osc.OSCMessage(address='/') client = osc.OSCClient('::1', 31337) client.send(msg) self.assertTrue(mock_socket.sendto.called) mock_socket.sendto.assert_called_once_with(msg.build().dgram, ('::1', 31337))
def _send_messages(): client = osc.OSCClient('127.0.0.1', 31338) for value in [ "This is example string", 123, True, bytes("utf-8 text", "utf-8") ]: msg = osc.OSCMessage(address="/debug") msg.add(value) client.send(msg) client.close()
def test_build_complex_bundle(self): bundle = osc.OSCBundle(timestamp=osc.IMMEDIATELY) msg = osc.OSCMessage(address="/SYNC") msg.add(4.0) # Add 4 messages in the bundle, each with more arguments. bundle.add(msg) msg.add(2) bundle.add(msg) msg.add("value") bundle.add(msg) msg.add(b"\x01\x02\x03") bundle.add(msg) sub_bundle = bundle # Now add the same bundle inside itself. bundle.add(sub_bundle) bundle = bundle.build() self.assertEqual(5, bundle.length)
def test_all_param_types(self): builder = osc.OSCMessage(address="/SYNC") builder.add(4.0) builder.add(2) builder.add("value") builder.add(True) builder.add(False) builder.add(b"\x01\x02\x03") # The same args but with explicit types. builder.add(4.0, osc.OSCType.TYPE_FLOAT) builder.add(2, osc.OSCType.TYPE_INT) builder.add("value", osc.OSCType.TYPE_STRING) builder.add(True) builder.add(False) builder.add(b"\x01\x02\x03", osc.OSCType.TYPE_BLOB) # non-standard types builder.add("UTF текст €", osc.OSCType.TYPE_UTF8_STRING) builder.add(3.1415, osc.OSCType.TYPE_DOUBLE) builder.add(123456789, osc.OSCType.TYPE_INT64) args_sequence = [4.0, 2, "value", True, False, b"\x01\x02\x03", 4.0, 2, "value", True, False, b"\x01\x02\x03", "UTF текст €", 3.1415, 123456789] self.assertEqual(15, len(builder.args)) self.assertEqual("/SYNC", builder.address) # Test after dgram build builder.address = '/SEEK' msg = builder.build() self.assertEqual("/SEEK", msg.address) self.assertSequenceEqual(args_sequence, msg.args) # Test datagram parse from_dgram = osc.OSCMessage.parse(msg.dgram) self.assertEqual("/SEEK", from_dgram.address) self.assertSequenceEqual(args_sequence, from_dgram.args)
def test_build_wrong_type_raises(self): builder = osc.OSCMessage(address="/SYNC") builder.add('this is not a float', osc.OSCType.TYPE_FLOAT) self.assertRaises(osc.OSCBuildError, builder.build)
def test_message_args(self): msg = osc.OSCMessage("/param", [5, "five", 5.5, True]) self.assertEqual(4, len(msg.args)) self.assertEqual([5, "five", 5.5, True], msg.args)
def test_wrong_param_raise(self): builder = osc.OSCMessage() self.assertRaises(ValueError, builder.add, "what?", 1)
def test_just_address(self): msg = osc.OSCMessage(address="/a/b/c") self.assertEqual("/a/b/c", msg.address) self.assertEqual(0, len(msg))