private void ReadTest <T>(T expected, Action <TagWriter, T> write, Func <TR2, T> read) { using (Stream stream = new MemoryStream()) { // arrange TR2 target; T actual; using (BinaryTagWriter writer = new BinaryTagWriter(stream)) { writer.WriteStartDocument(); writer.WriteStartTag(TagType.Compound); write(writer, expected); writer.WriteEndTag(); writer.WriteEndDocument(); } stream.Position = 0; target = new TR2(stream); target.MoveToNextElement(); // compound // act actual = read(target); // assert Assert.AreEqual(expected, actual); } }
public void ReadList_throws_exception_if_list_type_is_invalid() { using (MemoryStream stream = new MemoryStream()) { // arrange TagReader reader; TagWriter writer; reader = this.CreateReader(stream); writer = new BinaryTagWriter(stream); writer.WriteStartDocument(); writer.WriteStartTag("list", TagType.List, (TagType)182, 0); writer.WriteEndTag(); writer.WriteEndDocument(); stream.Position = 0; reader.ReadTagType(); reader.ReadTagName(); // act reader.ReadList(); } }
public void WritePredefinedBinaryDocument() { using (Stream stream = new MemoryStream()) { TagWriter writer = new BinaryTagWriter(stream); writer.WriteStartDocument(); writer.WriteTag(_predefined); writer.WriteEndDocument(); } }
public void WriteBinaryDocument() { TagCompound root; TagCompound compound; TagCompound child; TagList list; root = new TagCompound(); root.Name = "Level"; root.Value.Add("longTest", 9223372036854775807); root.Value.Add("shortTest", (short)32767); root.Value.Add("stringTest", "HELLO WORLD THIS IS A TEST STRING ÅÄÖ!"); root.Value.Add("floatTest", (float)0.498231471); root.Value.Add("intTest", 2147483647); compound = (TagCompound)root.Value.Add("nested compound test", TagType.Compound); child = (TagCompound)compound.Value.Add("ham", TagType.Compound); child.Value.Add("name", "Hampus"); child.Value.Add("value", (float)0.75); child = (TagCompound)compound.Value.Add("egg", TagType.Compound); child.Value.Add("name", "Eggbert"); child.Value.Add("value", (float)0.5); list = (TagList)root.Value.Add("listTest (long)", TagType.List, TagType.Long); list.Value.Add((long)11); list.Value.Add((long)12); list.Value.Add((long)13); list.Value.Add((long)14); list.Value.Add((long)15); list = (TagList)root.Value.Add("listTest (compound)", TagType.List, TagType.Compound); child = (TagCompound)list.Value.Add(TagType.Compound); child.Value.Add("name", "Compound tag #0"); child.Value.Add("created-on", 1264099775885); child = (TagCompound)list.Value.Add(TagType.Compound); child.Value.Add("name", "Compound tag #1"); child.Value.Add("created-on", 1264099775885); root.Value.Add("byteTest", (byte)127); root.Value.Add("byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))", SampleByteArray); root.Value.Add("doubleTest", 0.49312871321823148); using (Stream stream = new MemoryStream()) { TagWriter writer; writer = new BinaryTagWriter(stream); writer.WriteStartDocument(); writer.WriteTag(root); writer.WriteEndDocument(); } }
public void SaveCompressedTest() { // arrange BinaryTagWriter writer; TagCompound tag; tag = this.GetComplexData(); writer = new BinaryTagWriter(); // act writer.Write(tag, this.OutputFileName, NbtOptions.Compress | NbtOptions.Header); // assert this.CompareTags(tag, new NbtDocument(this.OutputFileName).DocumentRoot); }
public void ReadIntArray_throws_exception_if_data_invalid() { using (MemoryStream stream = new MemoryStream()) { // arrange TagReader reader; TagWriter writer; reader = this.CreateReader(stream); writer = new BinaryTagWriter(stream); // TODO: WriteValue is currently protected //writer.WriteValue(100); this.WriteValue(stream, 100); stream.Position = 0; // act Assert.Throws <InvalidDataException>(() => reader.ReadIntArray()); } }
public void ReadString_throws_exception_if_data_invalid() { using (MemoryStream stream = new MemoryStream()) { // arrange TagReader reader; TagWriter writer; reader = this.CreateReader(stream); writer = new BinaryTagWriter(stream); // TODO: WriteValue is currently protected //writer.WriteValue((short)100); this.WriteValue(stream, (short)100); stream.Position = 0; // act reader.ReadString(); } }
public void ReadList_throws_exception_if_list_type_is_invalid() { using (var stream = new MemoryStream()) { // arrange var reader = CreateReader(stream); TagWriter writer = new BinaryTagWriter(stream); writer.WriteStartDocument(); writer.WriteStartTag("list", TagType.List, (TagType)182, 0); writer.WriteEndTag(); writer.WriteEndDocument(); stream.Position = 0; reader.ReadTagType(); reader.ReadTagName(); // act Exception e = Assert.Throws <InvalidDataException>(() => reader.ReadList()); Assert.Equal("Unexpected list type '182' found.", e.Message); } }
public virtual byte[] GetValue() { byte[] result; using (MemoryStream stream = new MemoryStream()) { ITagWriter writer; writer = new BinaryTagWriter(stream); writer.WriteTag(this, WriteTagOptions.None); result = stream.ToArray(); } return result; }
public void SaveBinaryTest() { // arrange TagCompound target; TagWriter writer; string fileName; byte[] source; byte[] destination; fileName = this.ComplexDataFileName; target = new NbtDocument(fileName).DocumentRoot; using (FileStream file = File.OpenRead(fileName)) source = this.Decompress(file); // act using (MemoryStream stream = new MemoryStream()) { writer = new BinaryTagWriter(stream, NbtOptions.Header); writer.Write(target); destination = stream.ToArray(); } File.WriteAllBytes(this.OutputFileName, destination); // assert CollectionAssert.AreEqual(source, destination); new NbtDocument(this.OutputFileName); }