Exemple #1
0
        /// <summary>
        /// Class constructor.
        /// </summary>
        static JsonReaderUtils()
        {
            var readSizes = new int[][]
            {
                new int[] { Int32.MaxValue },
                new int[] { 1 },
                new int[] { 1, 7, 101 }
            };

            Func <TextReader, AssertionHandler, JsonReader>[] creatorFuncs = new Func <TextReader, AssertionHandler, JsonReader>[]
            {
                (textReader, assert) => new JsonReader(textReader, assert, isIeee754Compatible: true),
                (textReader, assert) => new BufferingJsonReader(textReader, ODataConstants.DefaultMaxRecursionDepth, assert, isIeee754Compatible: true),
                (textReader, assert) =>
                {
                    BufferingJsonReader reader = new BufferingJsonReader(textReader, ODataConstants.DefaultMaxRecursionDepth, assert, isIeee754Compatible: true);
                    reader.StartBuffering();
                    return(reader);
                },
            };

            allTestConfigurations = readSizes.SelectMany(rs =>
                                                         creatorFuncs.Select(f => new JsonReaderTestConfiguration {
                ReadSizes = rs, JsonReaderCreatorFunc = f
            }));
        }
Exemple #2
0
        /// <summary>
        /// Runs a single BufferingJsonReaderTestCaseDescriptor test with a single toggle index in it
        /// and verifies that the reader state after turning off buffering is correct.
        /// </summary>
        /// <param name="testCase">The test case descriptor to run.</param>
        /// <param name="assert"></param>
        public static void ReadAndVerifyStateAfterStopBuffering(BufferingJsonReaderTestCaseDescriptor testCase, AssertionHandler assert)
        {
            assert.AreEqual(2, testCase.ToggleBufferingCallCounts.Length, "Expected a single toggle position.");

            TextReader testReader = new StringReader(testCase.JsonText);
            Exception  exception  = TestExceptionUtils.RunCatching(() =>
            {
                BufferingJsonReader bufferingJsonReader = new BufferingJsonReader(testReader, ODataConstants.DefaultMaxRecursionDepth, assert, isIeee754Compatible: true);

                int callCount      = -1;
                int startBuffering = testCase.ToggleBufferingCallCounts[0];
                int stopBuffering  = testCase.ToggleBufferingCallCounts[1];
                bool isBuffering   = false;

                List <BufferingJsonReaderTestCaseDescriptor.ReaderNode> bufferedNodes = new List <BufferingJsonReaderTestCaseDescriptor.ReaderNode>();

                bool hasMore = false;
                do
                {
                    callCount++;

                    if (startBuffering == callCount)
                    {
                        BufferingJsonReaderTestCaseDescriptor.ReaderNode bufferedNode = new BufferingJsonReaderTestCaseDescriptor.ReaderNode(bufferingJsonReader.NodeType, bufferingJsonReader.Value);
                        bufferedNodes.Add(bufferedNode);

                        bufferingJsonReader.StartBuffering();
                        isBuffering = true;
                    }

                    if (stopBuffering == callCount)
                    {
                        bufferingJsonReader.StopBuffering();
                        isBuffering = false;

                        assert.AreEqual(bufferedNodes[0].NodeType, bufferingJsonReader.NodeType, "Node types must be equal.");
                        assert.AreEqual(bufferedNodes[0].Value, bufferingJsonReader.Value, "Values must be equal.");
                        bufferedNodes.RemoveAt(0);
                    }

                    hasMore = bufferingJsonReader.Read();
                    if (isBuffering)
                    {
                        bufferedNodes.Add(new BufferingJsonReaderTestCaseDescriptor.ReaderNode(bufferingJsonReader.NodeType, bufferingJsonReader.Value));
                    }
                    else if (bufferedNodes.Count > 0)
                    {
                        assert.AreEqual(bufferedNodes[0].NodeType, bufferingJsonReader.NodeType, "Node types must be equal.");
                        assert.AreEqual(bufferedNodes[0].Value, bufferingJsonReader.Value, "Values must be equal.");
                        bufferedNodes.RemoveAt(0);
                    }
                }while (hasMore);
            });

            assert.IsNull(exception, "Did not expect an exception.");
        }
        /// <summary>
        /// Class constructor.
        /// </summary>
        static JsonReaderUtils()
        {
            var readSizes = new int[][]
            {
                new int[] { Int32.MaxValue },
                new int[] { 1 },
                new int[] { 1, 7, 101 }
            };

            Func<TextReader, AssertionHandler, JsonReader>[] creatorFuncs = new Func<TextReader, AssertionHandler, JsonReader>[]
            {
                (textReader, assert) => new JsonReader(textReader, assert, ODataFormat.Json, isIeee754Compatible: true),
                (textReader, assert) => new BufferingJsonReader(textReader, ODataConstants.DefaultMaxRecursionDepth, assert, ODataFormat.Json, isIeee754Compatible: true),
                (textReader, assert) => 
                {
                    BufferingJsonReader reader = new BufferingJsonReader(textReader, ODataConstants.DefaultMaxRecursionDepth, assert, ODataFormat.Json, isIeee754Compatible: true);
                    reader.StartBuffering();
                    return reader;
                },
            };

            allTestConfigurations = readSizes.SelectMany(rs =>
                creatorFuncs.Select(f => new JsonReaderTestConfiguration { ReadSizes = rs, JsonReaderCreatorFunc = f }));
        }
        /// <summary>
        /// Runs a single BufferingJsonReaderTestCaseDescriptor test with a single toggle index in it
        /// and verifies that the reader state after turning off buffering is correct.
        /// </summary>
        /// <param name="testCase">The test case descriptor to run.</param>
        /// <param name="assert"></param>
        public static void ReadAndVerifyStateAfterStopBuffering(BufferingJsonReaderTestCaseDescriptor testCase, AssertionHandler assert)
        {
            assert.AreEqual(2, testCase.ToggleBufferingCallCounts.Length, "Expected a single toggle position.");

            TextReader testReader = new StringReader(testCase.JsonText);
            Exception exception = TestExceptionUtils.RunCatching(() =>
            {
                BufferingJsonReader bufferingJsonReader = new BufferingJsonReader(testReader, ODataConstants.DefaultMaxRecursionDepth, assert, ODataFormat.Json, isIeee754Compatible: true);
                   
                int callCount = -1;
                int startBuffering = testCase.ToggleBufferingCallCounts[0];
                int stopBuffering = testCase.ToggleBufferingCallCounts[1];
                bool isBuffering = false;

                List<BufferingJsonReaderTestCaseDescriptor.ReaderNode> bufferedNodes = new List<BufferingJsonReaderTestCaseDescriptor.ReaderNode>();

                bool hasMore = false;
                do
                {
                    callCount++;

                    if (startBuffering == callCount)
                    {
                        BufferingJsonReaderTestCaseDescriptor.ReaderNode bufferedNode = new BufferingJsonReaderTestCaseDescriptor.ReaderNode(bufferingJsonReader.NodeType, bufferingJsonReader.Value);
                        bufferedNodes.Add(bufferedNode);

                        bufferingJsonReader.StartBuffering();
                        isBuffering = true;
                    }

                    if (stopBuffering == callCount)
                    {
                        bufferingJsonReader.StopBuffering();
                        isBuffering = false;

                        assert.AreEqual(bufferedNodes[0].NodeType, bufferingJsonReader.NodeType, "Node types must be equal.");
                        assert.AreEqual(bufferedNodes[0].Value, bufferingJsonReader.Value, "Values must be equal.");
                        bufferedNodes.RemoveAt(0);
                    }

                    hasMore = bufferingJsonReader.Read();
                    if (isBuffering)
                    {
                        bufferedNodes.Add(new BufferingJsonReaderTestCaseDescriptor.ReaderNode(bufferingJsonReader.NodeType, bufferingJsonReader.Value));
                    }
                    else if (bufferedNodes.Count > 0)
                    {
                        assert.AreEqual(bufferedNodes[0].NodeType, bufferingJsonReader.NodeType, "Node types must be equal.");
                        assert.AreEqual(bufferedNodes[0].Value, bufferingJsonReader.Value, "Values must be equal.");
                        bufferedNodes.RemoveAt(0);
                    }
                }
                while (hasMore);
            });

            assert.IsNull(exception, "Did not expect an exception.");
        }
        /// <summary>
        /// Runs a single BufferingJsonReaderTestCaseDescriptor test.
        /// </summary>
        /// <param name="testCase">The test case descriptor to run.</param>
        /// <param name="assert"></param>
        public static void ReadAndVerifyBufferingJson(BufferingJsonReaderTestCaseDescriptor testCase, AssertionHandler assert)
        {
            TextReader testReader = new StringReader(testCase.JsonText);
            BufferingJsonReader bufferingJsonReader = new BufferingJsonReader(testReader, ODataConstants.DefaultMaxRecursionDepth, assert, ODataFormat.Json, isIeee754Compatible: true);

            bool isBuffering = false;
            int callCount = -1;
            int index = 0;
            int[] toggleCallCounts = testCase.ToggleBufferingCallCounts;
            int toggleAt = toggleCallCounts == null || toggleCallCounts.Length == 0 ? -1 : toggleCallCounts[index];

            int nonBufferingResultIndex = -1;
            int bufferingResultIndex = -1;

            do
            {
                callCount++;

                int ixToCompare;
                if (isBuffering)
                {
                    bufferingResultIndex++;
                    ixToCompare = bufferingResultIndex;
                }
                else
                {
                    nonBufferingResultIndex++;
                    ixToCompare = nonBufferingResultIndex;
                }

                assert.IsTrue(!isBuffering || bufferingResultIndex >= nonBufferingResultIndex, "Buffering index must be greater or equal than non-buffering one.");

                if (testCase.ExpectedNodes != null)
                {
                    assert.AreEqual(testCase.ExpectedNodes[ixToCompare].NodeType, bufferingJsonReader.NodeType, "Node types don't match.");
                    assert.AreEqual(testCase.ExpectedNodes[ixToCompare].Value, bufferingJsonReader.Value, "Values don't match.");
                }

                if (toggleAt == callCount)
                {
                    if (!isBuffering)
                    {
                        bufferingJsonReader.StartBuffering();
                        bufferingResultIndex = nonBufferingResultIndex;
                        isBuffering = true;
                    }
                    else
                    {
                        bufferingJsonReader.StopBuffering();
                        isBuffering = false;
                    }

                    if (index + 1 < toggleCallCounts.Length)
                    {
                        index++;
                        toggleAt = toggleCallCounts[index];
                    }
                }
            }
            while (bufferingJsonReader.Read());

            // we might have hit the end of the input in buffering mode; now empty the buffer.
            if (isBuffering)
            {
                bufferingJsonReader.StopBuffering();
                isBuffering = false;

                if (testCase.ExpectedNodes != null)
                {
                    assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].NodeType, bufferingJsonReader.NodeType, "Node types don't match.");
                    assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].Value, bufferingJsonReader.Value, "Values don't match.");
                }
            }

            while (bufferingJsonReader.Read())
            {
                nonBufferingResultIndex++;

                if (testCase.ExpectedNodes != null)
                {
                    assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].NodeType, bufferingJsonReader.NodeType, "Node types don't match.");
                    assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].Value, bufferingJsonReader.Value, "Values don't match.");
                }
            }

            // reading after end-of-input should stay in state end-of-input
            bufferingJsonReader.Read();
            assert.AreEqual(JsonNodeType.EndOfInput, bufferingJsonReader.NodeType, "Node types don't match.");
            assert.AreEqual(null, bufferingJsonReader.Value, "Values don't match.");
        }
Exemple #6
0
        /// <summary>
        /// Runs a single BufferingJsonReaderTestCaseDescriptor test.
        /// </summary>
        /// <param name="testCase">The test case descriptor to run.</param>
        /// <param name="assert"></param>
        public static void ReadAndVerifyBufferingJson(BufferingJsonReaderTestCaseDescriptor testCase, AssertionHandler assert)
        {
            TextReader          testReader          = new StringReader(testCase.JsonText);
            BufferingJsonReader bufferingJsonReader = new BufferingJsonReader(testReader, ODataConstants.DefaultMaxRecursionDepth, assert, isIeee754Compatible: true);

            bool isBuffering = false;
            int  callCount   = -1;
            int  index       = 0;

            int[] toggleCallCounts = testCase.ToggleBufferingCallCounts;
            int   toggleAt         = toggleCallCounts == null || toggleCallCounts.Length == 0 ? -1 : toggleCallCounts[index];

            int nonBufferingResultIndex = -1;
            int bufferingResultIndex    = -1;

            do
            {
                callCount++;

                int ixToCompare;
                if (isBuffering)
                {
                    bufferingResultIndex++;
                    ixToCompare = bufferingResultIndex;
                }
                else
                {
                    nonBufferingResultIndex++;
                    ixToCompare = nonBufferingResultIndex;
                }

                assert.IsTrue(!isBuffering || bufferingResultIndex >= nonBufferingResultIndex, "Buffering index must be greater or equal than non-buffering one.");

                if (testCase.ExpectedNodes != null)
                {
                    assert.AreEqual(testCase.ExpectedNodes[ixToCompare].NodeType, bufferingJsonReader.NodeType, "Node types don't match.");
                    assert.AreEqual(testCase.ExpectedNodes[ixToCompare].Value, bufferingJsonReader.Value, "Values don't match.");
                }

                if (toggleAt == callCount)
                {
                    if (!isBuffering)
                    {
                        bufferingJsonReader.StartBuffering();
                        bufferingResultIndex = nonBufferingResultIndex;
                        isBuffering          = true;
                    }
                    else
                    {
                        bufferingJsonReader.StopBuffering();
                        isBuffering = false;
                    }

                    if (index + 1 < toggleCallCounts.Length)
                    {
                        index++;
                        toggleAt = toggleCallCounts[index];
                    }
                }
            }while (bufferingJsonReader.Read());

            // we might have hit the end of the input in buffering mode; now empty the buffer.
            if (isBuffering)
            {
                bufferingJsonReader.StopBuffering();
                isBuffering = false;

                if (testCase.ExpectedNodes != null)
                {
                    assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].NodeType, bufferingJsonReader.NodeType, "Node types don't match.");
                    assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].Value, bufferingJsonReader.Value, "Values don't match.");
                }
            }

            while (bufferingJsonReader.Read())
            {
                nonBufferingResultIndex++;

                if (testCase.ExpectedNodes != null)
                {
                    assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].NodeType, bufferingJsonReader.NodeType, "Node types don't match.");
                    assert.AreEqual(testCase.ExpectedNodes[nonBufferingResultIndex].Value, bufferingJsonReader.Value, "Values don't match.");
                }
            }

            // reading after end-of-input should stay in state end-of-input
            bufferingJsonReader.Read();
            assert.AreEqual(JsonNodeType.EndOfInput, bufferingJsonReader.NodeType, "Node types don't match.");
            assert.AreEqual(null, bufferingJsonReader.Value, "Values don't match.");
        }