public void HandleEchoDictionary() { var dictionary = new Dictionary(); dictionary.Entries.Add(new DictionaryEntry { Key = ProtocolBuffers.WriteValue(0, typeof(int)), Value = ProtocolBuffers.WriteValue("jeb", typeof(string)) }); dictionary.Entries.Add(new DictionaryEntry { Key = ProtocolBuffers.WriteValue(1, typeof(int)), Value = ProtocolBuffers.WriteValue("bob", typeof(string)) }); dictionary.Entries.Add(new DictionaryEntry { Key = ProtocolBuffers.WriteValue(2, typeof(int)), Value = ProtocolBuffers.WriteValue("bill", typeof(string)) }); var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.EchoDictionary(It.IsAny <IDictionary <int, string> > ())) .Returns((IDictionary <int, string> x) => x); TestService.Service = mock.Object; var response = Run(Req("TestService", "EchoDictionary", Arg(0, ProtocolBuffers.WriteMessage(dictionary)))); response.Time = 0; Assert.AreEqual("", response.Error); Assert.AreEqual(ProtocolBuffers.WriteMessage(dictionary), response.ReturnValue); mock.Verify(x => x.EchoDictionary(It.IsAny <IDictionary <int, string> > ()), Times.Once()); }
public ParameterSignature(string fullProcedureName, ProcedureParameter parameter) { Name = parameter.Name; Type = parameter.Type; // Check the parameter type is valid if (!TypeUtils.IsAValidType(Type)) { throw new ServiceException(Type + " is not a valid Procedure parameter type, in " + fullProcedureName); } // Encode the default value as a ByteString if (parameter.HasDefaultValue) { var value = parameter.DefaultValue; if (TypeUtils.IsAClassType(Type)) { DefaultArgument = ProtocolBuffers.WriteValue(ObjectStore.Instance.AddInstance(value), typeof(ulong)); } else if (TypeUtils.IsAnEnumType(Type)) { // TODO: Assumes it's underlying type is int DefaultArgument = ProtocolBuffers.WriteValue((int)value, typeof(int)); } else if (ProtocolBuffers.IsAMessageType(Type)) { DefaultArgument = ProtocolBuffers.WriteMessage(value as IMessage); } else { DefaultArgument = ProtocolBuffers.WriteValue(value, Type); } } }
/// <summary> /// Encode a collection /// </summary> ByteString EncodeCollection(Type type, object value) { if (TypeUtils.IsAListCollectionType(type)) { var encodedList = new Schema.KRPC.List(); var list = (System.Collections.IList)value; var valueType = type.GetGenericArguments().Single(); foreach (var item in list) { encodedList.Items.Add(Encode(valueType, item)); } return(ProtocolBuffers.WriteMessage(encodedList)); } else if (TypeUtils.IsADictionaryCollectionType(type)) { var keyType = type.GetGenericArguments() [0]; var valueType = type.GetGenericArguments() [1]; var encodedDictionary = new Schema.KRPC.Dictionary(); foreach (System.Collections.DictionaryEntry entry in (System.Collections.IDictionary)value) { var encodedEntry = new Schema.KRPC.DictionaryEntry(); encodedEntry.Key = Encode(keyType, entry.Key); encodedEntry.Value = Encode(valueType, entry.Value); encodedDictionary.Entries.Add(encodedEntry); } return(ProtocolBuffers.WriteMessage(encodedDictionary)); } else if (TypeUtils.IsASetCollectionType(type)) { var encodedSet = new Schema.KRPC.Set(); var set = (System.Collections.IEnumerable)value; var valueType = type.GetGenericArguments().Single(); foreach (var item in set) { encodedSet.Items.Add(Encode(valueType, item)); } return(ProtocolBuffers.WriteMessage(encodedSet)); } else // a tuple // TODO: this is ugly { var encodedTuple = new Schema.KRPC.Tuple(); var valueTypes = type.GetGenericArguments().ToArray(); var genericType = Type.GetType("KRPC.Utils.Tuple`" + valueTypes.Length); var tupleType = genericType.MakeGenericType(valueTypes); for (int i = 0; i < valueTypes.Length; i++) { var property = tupleType.GetProperty("Item" + (i + 1)); var item = property.GetGetMethod().Invoke(value, null); encodedTuple.Items.Add(Encode(valueTypes [i], item)); } return(ProtocolBuffers.WriteMessage(encodedTuple)); } }
public void HandleEchoTuple() { var tuple = new KRPC.Schema.KRPC.Tuple(); tuple.Items.Add(ProtocolBuffers.WriteValue(42, typeof(int))); tuple.Items.Add(ProtocolBuffers.WriteValue(false, typeof(bool))); var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.EchoTuple(It.IsAny <KRPC.Utils.Tuple <int, bool> > ())) .Returns((KRPC.Utils.Tuple <int, bool> x) => x); TestService.Service = mock.Object; var response = Run(Req("TestService", "EchoTuple", Arg(0, ProtocolBuffers.WriteMessage(tuple)))); response.Time = 0; Assert.AreEqual("", response.Error); Assert.AreEqual(ProtocolBuffers.WriteMessage(tuple), response.ReturnValue); mock.Verify(x => x.EchoTuple(It.IsAny <KRPC.Utils.Tuple <int, bool> > ()), Times.Once()); }
public void HandleEchoSet() { var set = new Set(); set.Items.Add(ProtocolBuffers.WriteValue(345, typeof(int))); set.Items.Add(ProtocolBuffers.WriteValue(723, typeof(int))); set.Items.Add(ProtocolBuffers.WriteValue(112, typeof(int))); var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.EchoSet(It.IsAny <HashSet <int> > ())) .Returns((HashSet <int> x) => x); TestService.Service = mock.Object; var response = Run(Req("TestService", "EchoSet", Arg(0, ProtocolBuffers.WriteMessage(set)))); response.Time = 0; Assert.AreEqual("", response.Error); Assert.AreEqual(ProtocolBuffers.WriteMessage(set), response.ReturnValue); mock.Verify(x => x.EchoSet(It.IsAny <HashSet <int> > ()), Times.Once()); }
public void HandleEchoList() { var list = new KRPC.Schema.KRPC.List(); list.Items.Add(ProtocolBuffers.WriteValue("jeb", typeof(string))); list.Items.Add(ProtocolBuffers.WriteValue("bob", typeof(string))); list.Items.Add(ProtocolBuffers.WriteValue("bill", typeof(string))); var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.EchoList(It.IsAny <IList <string> > ())) .Returns((IList <string> x) => x); TestService.Service = mock.Object; var response = Run(Req("TestService", "EchoList", Arg(0, ProtocolBuffers.WriteMessage(list)))); response.Time = 0; Assert.AreEqual("", response.Error); Assert.AreEqual(ProtocolBuffers.WriteMessage(list), response.ReturnValue); mock.Verify(x => x.EchoList(It.IsAny <IList <string> > ()), Times.Once()); }
public void HandleEchoNestedCollection() { var list0 = new KRPC.Schema.KRPC.List(); list0.Items.Add(ProtocolBuffers.WriteValue("jeb", typeof(string))); list0.Items.Add(ProtocolBuffers.WriteValue("bob", typeof(string))); var list1 = new KRPC.Schema.KRPC.List(); var list2 = new KRPC.Schema.KRPC.List(); list2.Items.Add(ProtocolBuffers.WriteValue("bill", typeof(string))); list2.Items.Add(ProtocolBuffers.WriteValue("edzor", typeof(string))); var collection = new Dictionary(); collection.Entries.Add(new DictionaryEntry { Key = ProtocolBuffers.WriteValue(0, typeof(int)), Value = ProtocolBuffers.WriteMessage(list0) }); collection.Entries.Add(new DictionaryEntry { Key = ProtocolBuffers.WriteValue(1, typeof(int)), Value = ProtocolBuffers.WriteMessage(list1) }); collection.Entries.Add(new DictionaryEntry { Key = ProtocolBuffers.WriteValue(2, typeof(int)), Value = ProtocolBuffers.WriteMessage(list2) }); var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.EchoNestedCollection(It.IsAny <IDictionary <int, IList <string> > > ())) .Returns((IDictionary <int, IList <string> > x) => x); TestService.Service = mock.Object; var response = Run(Req("TestService", "EchoNestedCollection", Arg(0, ProtocolBuffers.WriteMessage(collection)))); response.Time = 0; Assert.AreEqual("", response.Error); Assert.AreEqual(ProtocolBuffers.WriteMessage(collection), response.ReturnValue); mock.Verify(x => x.EchoNestedCollection(It.IsAny <IDictionary <int, IList <string> > > ()), Times.Once()); }
public void HandleEchoListOfObjects() { var instance0 = new TestService.TestClass("foo"); var instance1 = new TestService.TestClass("bar"); var guid0 = ObjectStore.Instance.AddInstance(instance0); var guid1 = ObjectStore.Instance.AddInstance(instance1); var list = new KRPC.Schema.KRPC.List(); list.Items.Add(ProtocolBuffers.WriteValue(guid0, typeof(ulong))); list.Items.Add(ProtocolBuffers.WriteValue(guid1, typeof(ulong))); var mock = new Mock <ITestService> (MockBehavior.Strict); mock.Setup(x => x.EchoListOfObjects(It.IsAny <IList <TestService.TestClass> > ())) .Returns((IList <TestService.TestClass> x) => x); TestService.Service = mock.Object; var response = Run(Req("TestService", "EchoListOfObjects", Arg(0, ProtocolBuffers.WriteMessage(list)))); response.Time = 0; Assert.AreEqual("", response.Error); Assert.AreEqual(ProtocolBuffers.WriteMessage(list), response.ReturnValue); mock.Verify(x => x.EchoListOfObjects(It.IsAny <IList <TestService.TestClass> > ()), Times.Once()); }
/// <summary> /// Encode a value /// </summary> ByteString Encode(Type type, object value) { if (TypeUtils.IsAClassType(type)) { return(ProtocolBuffers.WriteValue(ObjectStore.Instance.AddInstance(value), typeof(ulong))); } else if (TypeUtils.IsACollectionType(type)) { return(EncodeCollection(type, value)); } else if (ProtocolBuffers.IsAMessageType(type)) { return(ProtocolBuffers.WriteMessage(value as IMessage)); } else if (TypeUtils.IsAnEnumType(type)) { // TODO: Assumes it's underlying type is int return(ProtocolBuffers.WriteValue((int)value, typeof(int))); } else { return(ProtocolBuffers.WriteValue(value, type)); } }