public void AssertEqual(SingleContainer<T> other)
 {
     Assert.Equal(this.Item, other.Item);
 }
Пример #2
0
        public static void Basics(TreeFormat format)
        {
            Random r = new Random();

            // Test integers with specific values (for varying length encodings)
            TestIntegers(format);

            // Test serialization of each primitive value type (bool, string, long, double)
            Sample sample = new Sample(new Random());

            sample.AssertEqual(RoundTrip <Sample>(sample, format));

            // Test serialization of containers (read must leave last token of nested items so loop finds next property name properly)
            SingleContainer <Sample> container = new SingleContainer <Sample>(sample);

            container.AssertEqual(RoundTrip <SingleContainer <Sample> >(container, format));

            // Test diagnostics doesn't throw when over Reader
            TreeDiagnostics diagnostics = Diagnostics(sample, format);

            // Test serialization of all supported primitive array types
            RoundTripArray(new byte[] { 0, 4, 16 }, format);
            RoundTripArray(new char[] { 'S', 'o', 'A' }, format);
            RoundTripArray(new sbyte[] { 0, 4, 16 }, format);
            RoundTripArray(new ushort[] { 0, 4, 16 }, format);
            RoundTripArray(new short[] { 0, 4, 16 }, format);
            RoundTripArray(new uint[] { 0, 4, 16 }, format);
            RoundTripArray(new int[] { 0, 4, 16 }, format);
            RoundTripArray(new ulong[] { 0, 4, 16 }, format);
            RoundTripArray(new long[] { 0, 4, 16 }, format);
            RoundTripArray(new float[] { 0.0f, 100.5f, -2.05f }, format);
            RoundTripArray(new double[] { 0.0f, 100.5f, -2.05f }, format);

            // Verify exception on (expected) unsupported type
            Assert.Throws <NotSupportedException>(() => RoundTripArray(new decimal[] { 0.01M, 0.02M }, format));

            // Null/Empty array handling (currently expected to come back as empty array)
            RoundTripArray <byte>(null, format);
            RoundTripArray <byte>(new byte[] { }, format);

            // Test double Dispose handled correctly, 'Verbose == true' works, 'LeaveStreamOpen' respected
            container.AssertEqual(RoundTrip(container, format, testDoubleDispose: true));
            container.AssertEqual(RoundTrip(container, format, new TreeSerializationSettings()
            {
                LeaveStreamOpen = true
            }));
            container.AssertEqual(RoundTrip(container, format, new TreeSerializationSettings()
            {
                Verbose = true
            }));

            // Test null string handling
            sample.Name = null;
            sample.AssertEqual(RoundTrip(sample, format));

            // Test settings defaulting
            sample.AssertEqual(RoundTrip_NullSettings(sample, format));

            // Test Skip behavior works for each primitive value
            VerifySkip(sample, format);

            // Test serialization details
            using (MemoryStream stream = new MemoryStream())
            {
                TreeSerializationSettings settings = new TreeSerializationSettings()
                {
                    LeaveStreamOpen = true
                };

                using (ITreeWriter writer = Writer(format, stream, settings))
                {
                    sample.Write(writer);
                }

                long bytesWritten = stream.Position;
                stream.Seek(0, SeekOrigin.Begin);

                using (ITreeReader reader = Reader(format, stream, settings))
                {
                    // Test 'Expect' throwing (should not be 'None' when file just opened)
                    Assert.NotEqual(TreeToken.None, reader.TokenType);
                    Assert.Throws <IOException>(() => reader.Expect(TreeToken.None));

                    // Test reading back as wrong type (exception from ReadObject unexpected property name)
                    Assert.Throws <IOException>(() => new SingleContainer <Sample>().Read(reader));
                }
            }
        }