public void TestSerialize() { var stream = new MemoryStream(); var group = new TestGroup(); group.Type = TestGroupType.TestGroupTypeBaz; group.Baz = 4; var enumFormatter = new Mock <ICauterizeTypeFormatter>(); var byteFormatter = new Mock <ICauterizeTypeFormatter>(); var factory = new Mock <CauterizeTypeFormatterFactory>(); factory.Setup(f => f.GetFormatter(It.IsAny <Type>())).Returns((Type t) => { if (t == typeof(TestGroupType)) { return(enumFormatter.Object); } else if (t == typeof(Byte)) { return(byteFormatter.Object); } else { return(null); } }); enumFormatter.Setup(f => f.Serialize(stream, TestGroupType.TestGroupTypeBaz)); byteFormatter.Setup(f => f.Serialize(stream, (Byte)4)); var formatter = new CauterizeGroupFormatter(factory.Object); formatter.Serialize(stream, group); enumFormatter.VerifyAll(); byteFormatter.VerifyAll(); }
public void TestDeserialize() { var stream = new MemoryStream(); var factory = new Mock <CauterizeTypeFormatterFactory>(); var enumFormatter = new Mock <ICauterizeTypeFormatter>(); var intFormatter = new Mock <ICauterizeTypeFormatter>(); factory.Setup(f => f.GetFormatter(It.IsAny <Type>())).Returns((Type t) => { if (t == typeof(TestGroupType)) { return(enumFormatter.Object); } else if (t == typeof(int)) { return(intFormatter.Object); } else { return(null); } }); enumFormatter.Setup(f => f.Deserialize(stream, typeof(TestGroupType))) .Returns(TestGroupType.TestGroupTypeFoo); intFormatter.Setup(f => f.Deserialize(stream, typeof(int))) .Returns(1024); var formatter = new CauterizeGroupFormatter(factory.Object); var result = (TestGroup)formatter.Deserialize(stream, typeof(TestGroup)); Assert.AreEqual(TestGroupType.TestGroupTypeFoo, result.Type); Assert.AreEqual(1024, result.Foo); }