def testItShouldGetAnObjectReturnedByAMethod(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(None) self.reflector.replyWith(self.reflector.buildObjectReply(55512345)) response = object.callMe() assert isinstance(response, reflection.ReflectedObject) assert response._ref == 55512345
def testItShouldRaiseReflectionExceptionIfThereIsNoPropertyToSet(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(self.reflector.buildErrorReply("could not set property")) try: self.reflector.setProperty(object, "id", reflection.ReflectedType.fromNative(1, reflector=self.reflector)) assert False, "expected a ReflectionException" except reflection.ReflectionException as e: assert e.message == "could not set property"
def testItShouldDeleteFromTheObjectStore(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(self.reflector.buildSuccessReply()) response = self.reflector.delete(object) assert self.reflector.sent.type == Message.REFLECTION_REQUEST assert self.reflector.sent.reflection_request.type == Message.ReflectionRequest.DELETE assert self.reflector.sent.reflection_request.delete.object.reference == 987654321 assert response == True
def testItShouldSetAPropertyValue(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(self.reflector.buildPrimitiveReply(Message.Primitive.INT, 42)) object.setProperty = 42 assert isinstance(self.reflector.sent, Message) assert self.reflector.sent.type == Message.REFLECTION_REQUEST assert self.reflector.sent.reflection_request.type == Message.ReflectionRequest.SET_PROPERTY assert self.reflector.sent.reflection_request.set_property.property == "setProperty"
def testItShouldRaiseReflectionExceptionIfThereIsNoPropertyToGet(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(self.reflector.buildErrorReply("no property")) try: self.reflector.getProperty(object, "id") assert False, "expected a ReflectionException" except reflection.ReflectionException as e: assert e.message == "no property"
def testItShouldGetAPrimitiveReturnedByAMethod(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(None) self.reflector.replyWith(self.reflector.buildPrimitiveReply(Message.Primitive.INT, 1)) response = object.callMe() assert isinstance(response, reflection.ReflectedPrimitive) assert response.type() == "int" assert response.native() == 1
def testItShouldReturnFalseIfThereIsNoObjectToDelete(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(self.reflector.buildErrorReply("nothing to delete")) response = self.reflector.delete(object) assert self.reflector.sent.type == Message.REFLECTION_REQUEST assert self.reflector.sent.reflection_request.type == Message.ReflectionRequest.DELETE assert self.reflector.sent.reflection_request.delete.object.reference == 987654321 assert response == False
def testItShouldRaiseReflectionExceptionIfThereIsNoConstructor(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(self.reflector.buildErrorReply("no matching constructor for those parameters")) try: self.reflector.construct(object, reflection.ReflectedType.fromNative(1, reflector=self.reflector), reflection.ReflectedType.fromNative("Joe", reflector=self.reflector)) assert False, "expected a ReflectionException" except reflection.ReflectionException as e: assert e.message == "no matching constructor for those parameters"
def testItShouldConstructAClass(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(self.reflector.buildObjectReply(55512345)) response = self.reflector.construct(object) assert self.reflector.sent.type == Message.REFLECTION_REQUEST assert self.reflector.sent.reflection_request.type == Message.ReflectionRequest.CONSTRUCT assert self.reflector.sent.reflection_request.construct.object.reference == 987654321 assert isinstance(response, reflection.ReflectedObject) assert response._ref == 55512345
def testItShouldRaiseReflectionExceptionIfInvokingNonExistentMethod(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(None) self.reflector.replyWith(self.reflector.buildErrorReply("no method callMe")) try: object.callMe() assert False, "should have raised NameError" except reflection.ReflectionException as e: assert e.message == "no method callMe"
def testItShouldRaiseReflectionExceptionIfInvokingMethodWithInappropriateArguments(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(None) self.reflector.replyWith(self.reflector.buildErrorReply("no method callMe compatible with those arguments")) try: object.callMe() assert False, "should have raised NameError" except reflection.ReflectionException as e: assert e.message == "no method callMe compatible with those arguments"
def testItShouldInvokeAMethod(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(None) self.reflector.replyWith(self.reflector.buildObjectReply(55512345)) object.callMe() assert isinstance(self.reflector.sent, Message) assert self.reflector.sent.type == Message.REFLECTION_REQUEST assert self.reflector.sent.reflection_request.type == Message.ReflectionRequest.INVOKE assert self.reflector.sent.reflection_request.invoke.object.reference == 987654321 assert self.reflector.sent.reflection_request.invoke.method == "callMe"
def testItShouldSetAnObjectProperty(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(self.reflector.buildPrimitiveReply(Message.Primitive.INT, 1)) response = self.reflector.setProperty(object, "id", reflection.ReflectedType.fromNative(1, reflector=self.reflector)) assert self.reflector.sent.type == Message.REFLECTION_REQUEST assert self.reflector.sent.reflection_request.type == Message.ReflectionRequest.SET_PROPERTY assert self.reflector.sent.reflection_request.set_property.object.reference == 987654321 assert self.reflector.sent.reflection_request.set_property.property == "id" assert self.reflector.sent.reflection_request.set_property.value.primitive.type == Message.Primitive.INT assert self.reflector.sent.reflection_request.set_property.value.primitive.int == 1 assert response.reflection_response.status == Message.ReflectionResponse.SUCCESS
def testItShouldGetAnObjectProperty(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(self.reflector.buildPrimitiveReply(Message.Primitive.INT, 1)) response = self.reflector.getProperty(object, "id") assert self.reflector.sent.type == Message.REFLECTION_REQUEST assert self.reflector.sent.reflection_request.type == Message.ReflectionRequest.GET_PROPERTY assert self.reflector.sent.reflection_request.get_property.object.reference == 987654321 assert self.reflector.sent.reflection_request.get_property.property == "id" assert isinstance(response, reflection.ReflectedPrimitive) assert response.type() == 'int' assert response.native() == 1
def testItShouldInvokeAMethod(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(self.reflector.buildObjectReply(55512345)) response = self.reflector.invoke(object, "callMe") assert self.reflector.sent.type == Message.REFLECTION_REQUEST assert self.reflector.sent.reflection_request.type == Message.ReflectionRequest.INVOKE assert self.reflector.sent.reflection_request.invoke.object.reference == 987654321 assert self.reflector.sent.reflection_request.invoke.method == "callMe" assert len(self.reflector.sent.reflection_request.invoke.argument) == 0 assert isinstance(response, reflection.ReflectedObject) assert response._ref == 55512345
def testItShouldConstructAClassWithArguments(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(self.reflector.buildObjectReply(55512345)) response = self.reflector.construct(object, reflection.ReflectedType.fromNative(1, reflector=self.reflector), reflection.ReflectedType.fromNative("Joe", reflector=self.reflector)) assert self.reflector.sent.type == Message.REFLECTION_REQUEST assert self.reflector.sent.reflection_request.type == Message.ReflectionRequest.CONSTRUCT assert self.reflector.sent.reflection_request.construct.object.reference == 987654321 assert len(self.reflector.sent.reflection_request.construct.argument) == 2 assert self.reflector.sent.reflection_request.construct.argument[0].type == Message.Argument.PRIMITIVE assert self.reflector.sent.reflection_request.construct.argument[1].type == Message.Argument.STRING assert isinstance(response, reflection.ReflectedObject) assert response._ref == 55512345
def testItShouldInvokeAMethodWithArguments(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(self.reflector.buildObjectReply(55512345)) response = self.reflector.invoke(object, "callMe", reflection.ReflectedType.fromNative(1, reflector=self.reflector), reflection.ReflectedType.fromNative("Joe", reflector=self.reflector)) assert self.reflector.sent.type == Message.REFLECTION_REQUEST assert self.reflector.sent.reflection_request.type == Message.ReflectionRequest.INVOKE assert self.reflector.sent.reflection_request.invoke.object.reference == 987654321 assert self.reflector.sent.reflection_request.invoke.method == "callMe" assert len(self.reflector.sent.reflection_request.invoke.argument) == 2 assert self.reflector.sent.reflection_request.invoke.argument[0].type == Message.Argument.PRIMITIVE assert self.reflector.sent.reflection_request.invoke.argument[1].type == Message.Argument.STRING assert isinstance(response, reflection.ReflectedObject) assert response._ref == 55512345
def testItShouldInvokeAMethodWithArguments(self): object = reflection.ReflectedObject(987654321, reflector=self.reflector) self.reflector.replyWith(None) self.reflector.replyWith(self.reflector.buildObjectReply(55512345)) object.callMe(1, "Joe") assert isinstance(self.reflector.sent, Message) assert self.reflector.sent.type == Message.REFLECTION_REQUEST assert self.reflector.sent.reflection_request.type == Message.ReflectionRequest.INVOKE assert self.reflector.sent.reflection_request.invoke.object.reference == 987654321 assert self.reflector.sent.reflection_request.invoke.method == "callMe" assert len(self.reflector.sent.reflection_request.invoke.argument) == 2 assert self.reflector.sent.reflection_request.invoke.argument[0].type == Message.Argument.PRIMITIVE assert self.reflector.sent.reflection_request.invoke.argument[0].primitive.type == Message.Primitive.INT assert self.reflector.sent.reflection_request.invoke.argument[0].primitive.int == 1 assert self.reflector.sent.reflection_request.invoke.argument[1].type == Message.Argument.STRING assert self.reflector.sent.reflection_request.invoke.argument[1].string == "Joe"