Exemplo n.º 1
0
        public void SaveTest()
        {
            // arrange
            NbtDocument target;

            var fileName = GetWorkFile();
            var expected = new NbtDocument(CreateComplexData())
            {
                FileName = fileName
            };

            // act
            try
            {
                expected.Save();
                target = NbtDocument.LoadDocument(fileName);
            }
            finally
            {
                DeleteFile(fileName);
            }

            // assert
            NbtAssert.Equal(expected, target);
        }
Exemplo n.º 2
0
        public void EmptyListXmlTest()
        {
            // arrange
            NbtDocument reloaded;

            var fileName = GetWorkFile();
            var 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
            {
                DeleteFile(fileName);
            }

            // assert
            // this test is essentially ensuring that an infinite loop when reloading an XML document is no longer present
            NbtAssert.Equal(target.DocumentRoot, reloaded.DocumentRoot);
        }
Exemplo n.º 3
0
        protected void WriteTest <T, T2>(Func <Stream, T> createWriter, Func <Stream, T2> createReader)
            where T : TagWriter where T2 : TagReader
        {
            // arrange
            Stream stream = new MemoryStream();

            var expected = CreateComplexData();

            TagWriter 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);
            TagReader reader = createReader(stream);
            var       actual = reader.ReadDocument();

            NbtAssert.Equal(expected, actual);
        }
Exemplo n.º 4
0
        public void LoadWithFileTest()
        {
            // arrange
            var fileName = ComplexDataFileName;
            var expected = new NbtDocument(CreateComplexData());
            var target   = new NbtDocument();

            // act
            target.Load(fileName);

            // assert
            NbtAssert.Equal(expected, target);
        }
Exemplo n.º 5
0
        public void LoadDocument_loads_data_from_stream()
        {
            // arrange
            NbtDocument actual;
            var         fileName = ComplexDataFileName;
            var         expected = new NbtDocument(CreateComplexData());

            // act
            using (Stream stream = File.OpenRead(fileName))
            {
                actual = NbtDocument.LoadDocument(stream);
            }

            // assert
            NbtAssert.Equal(expected, actual);
        }
Exemplo n.º 6
0
        protected void WriteDocumentTest <T, T2>(Func <Stream, T> createWriter, Func <Stream, T2> createReader)
            where T : TagWriter where T2 : TagReader
        {
            // arrange
            Stream stream = new MemoryStream();

            var expected = CreateComplexData();

            TagWriter target = createWriter(stream);

            // act
            target.WriteStartDocument();
            target.WriteTag(expected);
            target.WriteEndDocument();

            // assert
            stream.Seek(0, SeekOrigin.Begin);
            TagReader reader = createReader(stream);
            var       actual = reader.ReadDocument();

            NbtAssert.Equal(expected, actual);
        }
Exemplo n.º 7
0
        public void FormatTest()
        {
            // arrange
            NbtDocument expected;
            NbtDocument target;
            bool        file1IsBinary;
            bool        file2IsXml;

            var fileName1 = GetWorkFile();
            var fileName2 = GetWorkFile();
            var source    = new NbtDocument(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
            {
                DeleteFile(fileName1);
                DeleteFile(fileName2);
            }

            // assert
            Assert.True(file1IsBinary);
            Assert.True(file2IsXml);
            NbtAssert.Equal(expected, target);
        }