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 WriteBinaryDirect() { using (Stream stream = new MemoryStream()) { TagWriter writer; writer = new BinaryTagWriter(stream); writer.WriteStartDocument(); writer.WriteStartTag("Level", TagType.Compound); writer.WriteTag("longTest", 9223372036854775807); writer.WriteTag("shortTest", (short)32767); writer.WriteTag("stringTest", "HELLO WORLD THIS IS A TEST STRING ÅÄÖ!"); writer.WriteTag("floatTest", (float)0.498231471); writer.WriteTag("intTest", 2147483647); writer.WriteStartTag("nested compound test", TagType.Compound); writer.WriteStartTag("ham", TagType.Compound); writer.WriteTag("name", "Hampus"); writer.WriteTag("value", 0.75F); writer.WriteEndTag(); writer.WriteStartTag("egg", TagType.Compound); writer.WriteTag("name", "Eggbert"); writer.WriteTag("value", 0.5F); writer.WriteEndTag(); writer.WriteEndTag(); writer.WriteStartTag("listTest (long)", TagType.List, TagType.Long, 5); writer.WriteTag((long)11); writer.WriteTag((long)12); writer.WriteTag((long)13); writer.WriteTag((long)14); writer.WriteTag((long)15); writer.WriteEndTag(); writer.WriteStartTag("listTest (compound)", TagType.List, TagType.Compound, 2); writer.WriteStartTag(TagType.Compound); writer.WriteTag("name", "Compound tag #0"); writer.WriteTag("created-on", 1264099775885); writer.WriteEndTag(); writer.WriteStartTag(TagType.Compound); writer.WriteTag("name", "Compound tag #1"); writer.WriteTag("created-on", 1264099775885); writer.WriteEndTag(); writer.WriteEndTag(); writer.WriteTag("byteTest", (byte)127); writer.WriteTag("byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))", SampleByteArray); writer.WriteTag("doubleTest", 0.49312871321823148); writer.WriteEndTag(); writer.WriteEndDocument(); } }
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); } }