public void SaveTest() { // arrange NbtDocument expected; NbtDocument target; string fileName; fileName = this.GetWorkFile(); expected = new NbtDocument(this.CreateComplexData()); expected.FileName = fileName; // act try { expected.Save(); target = NbtDocument.LoadDocument(fileName); } finally { this.DeleteFile(fileName); } // assert NbtAssert.AreEqual(expected, target); }
public void EmptyListXmlTest() { // arrange NbtDocument target; NbtDocument reloaded; string fileName; fileName = this.GetWorkFile(); target = new NbtDocument { Format = NbtFormat.Xml }; target.DocumentRoot.Name = "Test"; target.DocumentRoot.Value.Add("EmptyList", TagType.List, TagType.Compound); // act try { target.Save(fileName); reloaded = NbtDocument.LoadDocument(fileName); } finally { this.DeleteFile(fileName); } // assert // this test is essentially ensuring that an infinite loop when reloading an XML document is no longer present NbtAssert.AreEqual(target.DocumentRoot, reloaded.DocumentRoot); }
protected void WriteTest <T, T2>(Func <Stream, T> createWriter, Func <Stream, T2> createReader) where T : TagWriter where T2 : TagReader { // arrange TagWriter target; TagReader reader; TagCompound expected; TagCompound actual; Stream stream = new MemoryStream(); expected = this.CreateComplexData(); target = createWriter(stream); // act target.WriteStartDocument(); target.WriteStartTag("Level", TagType.Compound); target.WriteTag("longTest", 9223372036854775807); target.WriteTag("shortTest", (short)32767); target.WriteTag("stringTest", "HELLO WORLD THIS IS A TEST STRING едж!"); target.WriteTag("floatTest", (float)0.498231471); target.WriteTag("intTest", 2147483647); target.WriteStartTag("nested compound test", TagType.Compound); target.WriteStartTag("ham", TagType.Compound); target.WriteTag("name", "Hampus"); target.WriteTag("value", 0.75F); target.WriteEndTag(); target.WriteStartTag("egg", TagType.Compound); target.WriteTag("name", "Eggbert"); target.WriteTag("value", 0.5F); target.WriteEndTag(); target.WriteEndTag(); target.WriteStartTag("listTest (long)", TagType.List, TagType.Long, 5); target.WriteTag((long)11); target.WriteTag((long)12); target.WriteTag((long)13); target.WriteTag((long)14); target.WriteTag((long)15); target.WriteEndTag(); target.WriteStartTag("listTest (compound)", TagType.List, TagType.Compound, 2); target.WriteStartTag(TagType.Compound); target.WriteTag("name", "Compound tag #0"); target.WriteTag("created-on", 1264099775885); target.WriteEndTag(); target.WriteStartTag(TagType.Compound); target.WriteTag("name", "Compound tag #1"); target.WriteTag("created-on", 1264099775885); target.WriteEndTag(); target.WriteEndTag(); target.WriteTag("byteTest", (byte)127); target.WriteTag("byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))", ComplexData.SampleByteArray); target.WriteTag("doubleTest", 0.49312871321823148); target.WriteEndTag(); target.WriteEndDocument(); // assert stream.Seek(0, SeekOrigin.Begin); reader = createReader(stream); actual = reader.ReadDocument(); NbtAssert.AreEqual(expected, actual); }
public void LoadWithFileTest() { // arrange NbtDocument expected; NbtDocument target; string fileName; fileName = this.ComplexDataFileName; expected = new NbtDocument(this.CreateComplexData()); target = new NbtDocument(); // act target.Load(fileName); // assert NbtAssert.AreEqual(expected, target); }
public void LoadDocument_loads_data_from_stream() { // arrange NbtDocument expected; NbtDocument actual; string fileName; fileName = this.ComplexDataFileName; expected = new NbtDocument(this.CreateComplexData()); // act using (Stream stream = File.OpenRead(fileName)) { actual = NbtDocument.LoadDocument(stream); } // assert NbtAssert.AreEqual(expected, actual); }
public void FormatTest() { // arrange NbtDocument source; NbtDocument expected; NbtDocument target; string fileName1; string fileName2; bool file1IsBinary; bool file2IsXml; fileName1 = this.GetWorkFile(); fileName2 = this.GetWorkFile(); source = new NbtDocument(this.CreateComplexData()); // act try { source.Format = NbtFormat.Binary; source.Save(fileName1); source.Format = NbtFormat.Xml; source.Save(fileName2); expected = NbtDocument.LoadDocument(fileName1); target = NbtDocument.LoadDocument(fileName2); file1IsBinary = expected.Format == NbtFormat.Binary; file2IsXml = target.Format == NbtFormat.Xml; } finally { this.DeleteFile(fileName1); this.DeleteFile(fileName2); } // assert Assert.IsTrue(file1IsBinary); Assert.IsTrue(file2IsXml); NbtAssert.AreEqual(expected, target); }
protected void WriteDocumentTest <T, T2>(Func <Stream, T> createWriter, Func <Stream, T2> createReader) where T : TagWriter where T2 : TagReader { // arrange TagWriter target; TagReader reader; TagCompound expected; TagCompound actual; Stream stream = new MemoryStream(); expected = this.CreateComplexData(); target = createWriter(stream); // act target.WriteStartDocument(); target.WriteTag(expected); target.WriteEndDocument(); // assert stream.Seek(0, SeekOrigin.Begin); reader = createReader(stream); actual = reader.ReadDocument(); NbtAssert.AreEqual(expected, actual); }
public void TestName() { using (Stream stream = File.OpenRead(this.ComplexDataFileName)) { using (Stream decompressorStream = new GZipStream(stream, CompressionMode.Decompress)) { // arrange TR2 target; ComplexData actual; ComplexData expected; ComplextDataLoadState state; ComplexData.Compound1 currentCompound1; ComplexData.Compound2 currentCompound2; actual = new ComplexData(); expected = ComplexData.Default; target = new TR2(decompressorStream); state = ComplextDataLoadState.NotInitialized; currentCompound1 = null; currentCompound2 = null; // act while (target.Read()) { if (target.IsStartElement()) { Trace.WriteLine(target.Type); Trace.WriteLine(target.Name); //Trace.WriteLine(target.Value) switch (target.Name) { case "Level": state = ComplextDataLoadState.Level; break; case "longTest": if (state == ComplextDataLoadState.Level) { actual.LongValue = target.ReadLong2(); } else { throw new InvalidDataException($"Unexpected token '{target.Name}'"); } break; case "shortTest": if (state == ComplextDataLoadState.Level) { actual.ShortValue = target.ReadShort2(); } else { throw new InvalidDataException($"Unexpected token '{target.Name}'"); } break; case "stringTest": if (state == ComplextDataLoadState.Level) { actual.StringValue = target.ReadString2(); } else { throw new InvalidDataException($"Unexpected token '{target.Name}'"); } break; case "floatTest": if (state == ComplextDataLoadState.Level) { actual.FloatValue = target.ReadFloat2(); } else { throw new InvalidDataException($"Unexpected token '{target.Name}'"); } break; case "intTest": if (state == ComplextDataLoadState.Level) { actual.IntegerValue = target.ReadInt2(); } else { throw new InvalidDataException($"Unexpected token '{target.Name}'"); } break; case "nested compound test": state = ComplextDataLoadState.Compound; break; case "ham": case "egg": if (state == ComplextDataLoadState.Compound) { currentCompound1 = new ComplexData.Compound1(); } else { throw new InvalidDataException($"Unexpected token '{target.Name}'"); } break; case "name": if (state == ComplextDataLoadState.Compound) { state = ComplextDataLoadState.CompoundItem; currentCompound1.Name = target.ReadString2(); actual.CompoundValue.Add(currentCompound1.Name, currentCompound1); } else { throw new InvalidDataException($"Unexpected token '{target.Name}'"); } break; case "value": if (state == ComplextDataLoadState.CompoundItem) { currentCompound1.Value = target.ReadFloat2(); } else { throw new InvalidDataException($"Unexpected token '{target.Name}'"); } break; case "listTest (long)": state = ComplextDataLoadState.LongList; break; } } else if (target.IsEndElement()) { switch (state) { case ComplextDataLoadState.NotInitialized: break; case ComplextDataLoadState.Level: break; case ComplextDataLoadState.CompoundItem: state = ComplextDataLoadState.Compound; break; case ComplextDataLoadState.Compound: state = ComplextDataLoadState.Level; break; case ComplextDataLoadState.LongList: break; case ComplextDataLoadState.CompoundList: break; } } } // assert NbtAssert.AreEqual(expected, actual); } } }