Example #1
0
        public void ReadValueTest()
        {
            byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None);
            var    reader   = new NbtReader(new MemoryStream(testData));

            Assert.IsTrue(reader.ReadToFollowing()); // root

            Assert.IsTrue(reader.ReadToFollowing()); // byte
            Assert.AreEqual(1, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // short
            Assert.AreEqual(2, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // int
            Assert.AreEqual(3, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // long
            Assert.AreEqual(4L, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // float
            Assert.AreEqual(5f, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // double
            Assert.AreEqual(6d, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // byteArray
            CollectionAssert.AreEqual(new byte[] { 10, 11, 12 }, (byte[])reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // intArray
            CollectionAssert.AreEqual(new[] { 20, 21, 22 }, (int[])reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // string
            Assert.AreEqual("123", reader.ReadValue());

            // Skip to the very end and make sure that we can't read any more values
            reader.ReadToFollowing();
            Assert.Throws <EndOfStreamException>(() => reader.ReadValue());
        }
Example #2
0
        public void ErrorTest()
        {
            var root = new NbtCompound("root");

            byte[] testData = new NbtFile(root).SaveToBuffer(NbtCompression.None);

            // creating NbtReader without a stream, or with a non-readable stream
            Assert.Throws <ArgumentNullException>(() => new NbtReader(null));
            Assert.Throws <ArgumentException>(() => new NbtReader(new NonReadableStream()));

            // corrupt the data
            testData[0] = 123;
            var reader = new NbtReader(new MemoryStream(testData));

            // attempt to use ReadValue when not at value
            Assert.Throws <InvalidOperationException>(() => reader.ReadValue());
            reader.CacheTagValues = true;
            Assert.Throws <InvalidOperationException>(() => reader.ReadValue());

            // attempt to read a corrupt stream
            Assert.Throws <NbtFormatException>(() => reader.ReadToFollowing());

            // make sure we've properly entered the error state
            Assert.IsTrue(reader.IsInErrorState);
            Assert.IsFalse(reader.HasName);
            Assert.Throws <InvalidReaderStateException>(() => reader.ReadToFollowing());
            Assert.Throws <InvalidReaderStateException>(() => reader.ReadListAsArray <int>());
            Assert.Throws <InvalidReaderStateException>(() => reader.ReadToNextSibling());
            Assert.Throws <InvalidReaderStateException>(() => reader.ReadToDescendant("derp"));
            Assert.Throws <InvalidReaderStateException>(() => reader.ReadAsTag());
            Assert.Throws <InvalidReaderStateException>(() => reader.Skip());
        }
Example #3
0
        public void ErrorTest()
        {
            var root = new NbtCompound("root");
            byte[] testData = new NbtFile(root).SaveToBuffer(NbtCompression.None);

            // creating NbtReader without a stream, or with a non-readable stream
            Assert.Throws<ArgumentNullException>(() => new NbtReader(null));
            Assert.Throws<ArgumentException>(() => new NbtReader(new NonReadableStream()));

            // corrupt the data
            testData[0] = 123;
            var reader = new NbtReader(new MemoryStream(testData));

            // attempt to use ReadValue when not at value
            Assert.Throws<InvalidOperationException>(() => reader.ReadValue());
            reader.CacheTagValues = true;
            Assert.Throws<InvalidOperationException>(() => reader.ReadValue());

            // attempt to read a corrupt stream
            Assert.Throws<NbtFormatException>(() => reader.ReadToFollowing());

            // make sure we've properly entered the error state
            Assert.IsTrue(reader.IsInErrorState);
            Assert.IsFalse(reader.HasName);
            Assert.Throws<InvalidReaderStateException>(() => reader.ReadToFollowing());
            Assert.Throws<InvalidReaderStateException>(() => reader.ReadListAsArray<int>());
            Assert.Throws<InvalidReaderStateException>(() => reader.ReadToNextSibling());
            Assert.Throws<InvalidReaderStateException>(() => reader.ReadToDescendant("derp"));
            Assert.Throws<InvalidReaderStateException>(() => reader.ReadAsTag());
            Assert.Throws<InvalidReaderStateException>(() => reader.Skip());
        }
        public void NbtParser_ResultsShouldBeInteger()
        {
            // Arrange
            IStringReader reader = new StringReader("8");

            // Act
            NbtReader.ReadValue(reader, out INbtArgument result);

            // Assert
            Assert.IsTrue(result is NbtInteger);
        }
        public void NbtParser_ResultsShouldBeCompound()
        {
            // Arrange
            IStringReader reader = new StringReader("{foo: 'bar', baz: 3}");

            // Act
            NbtReader.ReadValue(reader, out INbtArgument result);

            // Assert
            Assert.IsTrue(result is NbtCompound);
        }
Example #6
0
        public void ReadValueTest()
        {
            NbtCompound root = new NbtCompound("root")
            {
                new NbtByte("byte", 1),
                new NbtShort("short", 2),
                new NbtInt("int", 3),
                new NbtLong("long", 4),
                new NbtFloat("float", 5),
                new NbtDouble("double", 6),
                new NbtByteArray("byteArray", new byte[] { 10, 11, 12 }),
                new NbtIntArray("intArray", new[] { 20, 21, 22 }),
                new NbtString("string", "23")
            };

            byte[]    testData = new NbtFile(root).SaveToBuffer(NbtCompression.None);
            NbtReader reader   = new NbtReader(new MemoryStream(testData));

            Assert.IsTrue(reader.ReadToFollowing());   // root

            Assert.IsTrue(reader.ReadToFollowing());   // byte
            Assert.AreEqual(reader.ReadValue(), 1);
            Assert.IsTrue(reader.ReadToFollowing());   // short
            Assert.AreEqual(reader.ReadValue(), 2);
            Assert.IsTrue(reader.ReadToFollowing());   // int
            Assert.AreEqual(reader.ReadValue(), 3);
            Assert.IsTrue(reader.ReadToFollowing());   // long
            Assert.AreEqual(reader.ReadValue(), 4);
            Assert.IsTrue(reader.ReadToFollowing());   // float
            Assert.AreEqual(reader.ReadValue(), 5f);
            Assert.IsTrue(reader.ReadToFollowing());   // double
            Assert.AreEqual(reader.ReadValue(), 6d);
            Assert.IsTrue(reader.ReadToFollowing());   // byteArray
            CollectionAssert.AreEqual((byte[])reader.ReadValue(), new byte[] { 10, 11, 12 });
            Assert.IsTrue(reader.ReadToFollowing());   // intArray
            CollectionAssert.AreEqual((int[])reader.ReadValue(), new[] { 20, 21, 22 });
            Assert.IsTrue(reader.ReadToFollowing());   // string
            Assert.AreEqual(reader.ReadValue(), "23");
        }
        public void CacheTagValuesTest()
        {
            byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None);
            var reader = new NbtReader(new MemoryStream(testData));
            Assert.False(reader.CacheTagValues);
            reader.CacheTagValues = true;
            Assert.True(reader.ReadToFollowing()); // root

            Assert.True(reader.ReadToFollowing()); // byte
            Assert.Equal((byte) 1, reader.ReadValue());
            Assert.Equal((byte) 1, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // short
            Assert.Equal((Int16) 2, reader.ReadValue());
            Assert.Equal((Int16) 2, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // int
            Assert.Equal(3, reader.ReadValue());
            Assert.Equal(3, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // long
            Assert.Equal(4L, reader.ReadValue());
            Assert.Equal(4L, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // float
            Assert.Equal(5f, reader.ReadValue());
            Assert.Equal(5f, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // double
            Assert.Equal(6d, reader.ReadValue());
            Assert.Equal(6d, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // byteArray
            Assert.Equal(new byte[] {10, 11, 12}, (byte[]) reader.ReadValue());
            Assert.Equal(new byte[] {10, 11, 12}, (byte[]) reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // intArray
            Assert.Equal(new[] {20, 21, 22}, (int[]) reader.ReadValue());
            Assert.Equal(new[] {20, 21, 22}, (int[]) reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // string
            Assert.Equal("123", reader.ReadValue());
            Assert.Equal("123", reader.ReadValue());
        }
        public void ReadValueTest()
        {
            byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None);
            var reader = new NbtReader(new MemoryStream(testData));

            Assert.True(reader.ReadToFollowing()); // root

            Assert.True(reader.ReadToFollowing()); // byte
            Assert.Equal((byte) 1, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // short
            Assert.Equal((Int16) 2, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // int
            Assert.Equal(3, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // long
            Assert.Equal(4L, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // float
            Assert.Equal(5f, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // double
            Assert.Equal(6d, reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // byteArray
            Assert.Equal(new byte[] {10, 11, 12}, (byte[]) reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // intArray
            Assert.Equal(new[] {20, 21, 22}, (int[]) reader.ReadValue());
            Assert.True(reader.ReadToFollowing()); // string
            Assert.Equal("123", reader.ReadValue());

            // Skip to the very end and make sure that we can't read any more values
            reader.ReadToFollowing();
            Assert.Throws<EndOfStreamException>(() => reader.ReadValue());
        }
Example #9
0
        public void CacheTagValuesTest()
        {
            byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None);
            var    reader   = new NbtReader(new MemoryStream(testData));

            Assert.IsFalse(reader.CacheTagValues);
            reader.CacheTagValues = true;
            Assert.IsTrue(reader.ReadToFollowing()); // root

            Assert.IsTrue(reader.ReadToFollowing()); // byte
            Assert.AreEqual(1, reader.ReadValue());
            Assert.AreEqual(1, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // short
            Assert.AreEqual(2, reader.ReadValue());
            Assert.AreEqual(2, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // int
            Assert.AreEqual(3, reader.ReadValue());
            Assert.AreEqual(3, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // long
            Assert.AreEqual(4L, reader.ReadValue());
            Assert.AreEqual(4L, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // float
            Assert.AreEqual(5f, reader.ReadValue());
            Assert.AreEqual(5f, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // double
            Assert.AreEqual(6d, reader.ReadValue());
            Assert.AreEqual(6d, reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // byteArray
            CollectionAssert.AreEqual(new byte[] { 10, 11, 12 }, (byte[])reader.ReadValue());
            CollectionAssert.AreEqual(new byte[] { 10, 11, 12 }, (byte[])reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // intArray
            CollectionAssert.AreEqual(new[] { 20, 21, 22 }, (int[])reader.ReadValue());
            CollectionAssert.AreEqual(new[] { 20, 21, 22 }, (int[])reader.ReadValue());
            Assert.IsTrue(reader.ReadToFollowing()); // string
            Assert.AreEqual("123", reader.ReadValue());
            Assert.AreEqual("123", reader.ReadValue());
        }
Example #10
0
        public void ReadValueTest()
        {
            byte[] testData = new NbtFile(TestFiles.MakeValueTest()).SaveToBuffer(NbtCompression.None);
            var reader = new NbtReader(new MemoryStream(testData));

            Assert.IsTrue(reader.ReadToFollowing()); // root

            Assert.IsTrue(reader.ReadToFollowing()); // byte
            Assert.AreEqual(reader.ReadValue(), 1);
            Assert.IsTrue(reader.ReadToFollowing()); // short
            Assert.AreEqual(reader.ReadValue(), 2);
            Assert.IsTrue(reader.ReadToFollowing()); // int
            Assert.AreEqual(reader.ReadValue(), 3);
            Assert.IsTrue(reader.ReadToFollowing()); // long
            Assert.AreEqual(reader.ReadValue(), 4L);
            Assert.IsTrue(reader.ReadToFollowing()); // float
            Assert.AreEqual(reader.ReadValue(), 5f);
            Assert.IsTrue(reader.ReadToFollowing()); // double
            Assert.AreEqual(reader.ReadValue(), 6d);
            Assert.IsTrue(reader.ReadToFollowing()); // byteArray
            CollectionAssert.AreEqual((byte[])reader.ReadValue(),
                                      new byte[] {
                                          10, 11, 12
                                      });
            Assert.IsTrue(reader.ReadToFollowing()); // intArray
            CollectionAssert.AreEqual((int[])reader.ReadValue(),
                                      new[] {
                                          20, 21, 22
                                      });
            Assert.IsTrue(reader.ReadToFollowing()); // string
            Assert.AreEqual(reader.ReadValue(), "123");
        }
Example #11
0
        public void ReadValueTest()
        {
            NbtCompound root = new NbtCompound( "root" ) {
                new NbtByte( "byte", 1 ),
                new NbtShort( "short", 2 ),
                new NbtInt( "int", 3 ),
                new NbtLong( "long", 4 ),
                new NbtFloat( "float", 5 ),
                new NbtDouble( "double", 6 ),
                new NbtByteArray( "byteArray", new byte[] { 10, 11, 12 } ),
                new NbtIntArray( "intArray", new[] { 20, 21, 22 } ),
                new NbtString( "string", "23" )
            };
            byte[] testData = new NbtFile( root ).SaveToBuffer( NbtCompression.None );
            NbtReader reader = new NbtReader( new MemoryStream( testData ) );

            Assert.IsTrue( reader.ReadToFollowing() ); // root

            Assert.IsTrue( reader.ReadToFollowing() ); // byte
            Assert.AreEqual( reader.ReadValue(), 1 );
            Assert.IsTrue( reader.ReadToFollowing() ); // short
            Assert.AreEqual( reader.ReadValue(), 2 );
            Assert.IsTrue( reader.ReadToFollowing() ); // int
            Assert.AreEqual( reader.ReadValue(), 3 );
            Assert.IsTrue( reader.ReadToFollowing() ); // long
            Assert.AreEqual( reader.ReadValue(), 4 );
            Assert.IsTrue( reader.ReadToFollowing() ); // float
            Assert.AreEqual( reader.ReadValue(), 5f );
            Assert.IsTrue( reader.ReadToFollowing() ); // double
            Assert.AreEqual( reader.ReadValue(), 6d );
            Assert.IsTrue( reader.ReadToFollowing() ); // byteArray
            CollectionAssert.AreEqual( (byte[])reader.ReadValue(), new byte[] { 10, 11, 12 } );
            Assert.IsTrue( reader.ReadToFollowing() ); // intArray
            CollectionAssert.AreEqual( (int[])reader.ReadValue(), new[] { 20, 21, 22 } );
            Assert.IsTrue( reader.ReadToFollowing() ); // string
            Assert.AreEqual( reader.ReadValue(), "23" );
        }