Пример #1
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.");
        }
Пример #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, 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.");
        }