//tests that both XML and Binary serialisation is invertible private void TestSerialisationIsInvertible(string xiFilename) { Console.Out.WriteLine("Testing file {0}", xiFilename); using (Stream inStr = File.OpenRead(xiFilename)) { Level levelFromBinary = new Level(); levelFromBinary.Deserialise(inStr); StringWriter sw = new StringWriter(); XmlSerializer xs = new XmlSerializer(typeof(Level)); xs.Serialize(sw, levelFromBinary); //now the XML version of the level is in sw Level levelFromXMLfromBinary = (Level)xs.Deserialize(new StringReader(sw.ToString())); //first check that the XML serialisation is the same StringWriter sw2 = new StringWriter(); xs.Serialize(sw2, levelFromBinary); Assert.AreEqual(sw.ToString(), sw2.ToString()); //now see if the XML-ified level will still write binary correctly: MemoryStream memStr = new MemoryStream(); inStr.Seek(0, SeekOrigin.Begin); DebugOutputStreamWithExpectations outStr = new DebugOutputStreamWithExpectations(inStr, memStr); levelFromXMLfromBinary.Serialise(outStr); inStr.Seek(0, SeekOrigin.Begin); memStr.Seek(0, SeekOrigin.Begin); Assert.IsTrue(StreamUtils.StreamsAreEqual(inStr, memStr)); } }
/* private string[] GetSampleLevelFiles() { string baseDir = GetBaseDir(); if (false) { //qq just one level for now (TODO: make the tests below work for all levels) return new string[] { baseDir + @"\MICRO\BREAKY\BREAKY1.DAT" }; } else { ArrayList acc = new ArrayList(); foreach (string file in Directory.GetFiles(baseDir + @"\MICRO", "*.DAT", SearchOption.AllDirectories)) { if (Regex.IsMatch(file, "[A-Z]+\\\\[A-Z]+[0-9]\\.DAT$")) acc.Add(file); } return (string[])acc.ToArray(typeof(string)); } } */ //this is a weaker test than TestSerialisationIsInvertible //but has been left here as it's sometimes handy for debugging private void TestBinarySerialisationIsInvertible(string xiFilename) { Console.Out.WriteLine("Testing file {0}", xiFilename); using (Stream inStr = File.OpenRead(xiFilename)) { Level deserLev = new Level(); deserLev.Deserialise(inStr); MemoryStream memStr = new MemoryStream(); inStr.Seek(0, SeekOrigin.Begin); DebugOutputStreamWithExpectations outStr = new DebugOutputStreamWithExpectations(inStr, memStr); deserLev.Serialise(outStr); inStr.Seek(0, SeekOrigin.Begin); memStr.Seek(0, SeekOrigin.Begin); Assert.IsTrue(StreamUtils.StreamsAreEqual(inStr, memStr)); } }