public async Task Write_WritesCharValues_ToUnderlyingStream_WhenNotBuffering()
        {
            // Arrange
            var inner = new Mock <TextWriter> {
                CallBase = true
            };
            var buffer  = new ViewBuffer(new TestViewBufferScope(), "some-name", pageSize: 4);
            var writer  = new ViewBufferTextWriter(buffer, Encoding.UTF8, new HtmlTestEncoder(), inner.Object);
            var buffer1 = new[] { 'a', 'b', 'c', 'd' };
            var buffer2 = new[] { 'd', 'e', 'f' };

            // Act
            writer.Flush();
            writer.Write('x');
            writer.Write(buffer1, 1, 2);
            writer.Write(buffer2);
            await writer.WriteAsync(buffer2, 1, 1);

            await writer.WriteLineAsync(buffer1);

            // Assert
            inner.Verify(v => v.Write('x'), Times.Once());
            inner.Verify(v => v.Write(buffer1, 1, 2), Times.Once());
            inner.Verify(v => v.Write(buffer1, 0, 4), Times.Once());
            inner.Verify(v => v.Write(buffer2, 0, 3), Times.Once());
            inner.Verify(v => v.WriteAsync(buffer2, 1, 1), Times.Once());
            inner.Verify(v => v.WriteLine(), Times.Once());
        }
        public void Write_WritesDataTypes_ToUnderlyingStream_WhenNotBuffering()
        {
            // Arrange
            var expected  = new[] { "True", "3", "18446744073709551615", "Hello world", "3.14", "2.718" };
            var inner     = new Mock <TextWriter>();
            var buffer    = new ViewBuffer(new TestViewBufferScope(), "some-name", pageSize: 4);
            var writer    = new ViewBufferTextWriter(buffer, Encoding.UTF8, new HtmlTestEncoder(), inner.Object);
            var testClass = new TestClass();

            // Act
            writer.Flush();
            writer.Write(true);
            writer.Write(3);
            writer.Write(ulong.MaxValue);
            writer.Write(testClass);
            writer.Write(3.14);
            writer.Write(2.718m);

            // Assert
            Assert.Equal(0, buffer.Count);
            foreach (var item in expected)
            {
                inner.Verify(v => v.Write(item), Times.Once());
            }
        }
        public void Write_HtmlContent_AfterFlush_GoesToStream()
        {
            // Arrange
            var inner  = new StringWriter();
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name", pageSize: 4);
            var writer = new ViewBufferTextWriter(buffer, Encoding.UTF8, new HtmlTestEncoder(), inner);

            writer.Flush();

            var content = new HtmlString("Hello, world!");

            // Act
            writer.Write(content);

            // Assert
            Assert.Equal("Hello, world!", inner.ToString());
        }
        public void Write_WritesDataTypes_ToBuffer()
        {
            // Arrange
            var expected = new object[] { "True", "3", "18446744073709551615", "Hello world", "3.14", "2.718", "m" };
            var buffer   = new ViewBuffer(new TestViewBufferScope(), "some-name", pageSize: 4);
            var writer   = new ViewBufferTextWriter(buffer, Encoding.UTF8);

            // Act
            writer.Write(true);
            writer.Write(3);
            writer.Write(ulong.MaxValue);
            writer.Write(new TestClass());
            writer.Write(3.14);
            writer.Write(2.718m);
            writer.Write('m');

            // Assert
            Assert.Equal(expected, GetValues(buffer));
        }
        public async Task Write_WritesStringValues_ToUnbufferedStream_WhenNotBuffering()
        {
            // Arrange
            var inner  = new Mock <TextWriter>();
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name", pageSize: 4);
            var writer = new ViewBufferTextWriter(buffer, Encoding.UTF8, new HtmlTestEncoder(), inner.Object);

            // Act
            await writer.FlushAsync();

            writer.Write("a");
            writer.WriteLine("ab");
            await writer.WriteAsync("ef");

            await writer.WriteLineAsync("gh");

            // Assert
            inner.Verify(v => v.Write("a"), Times.Once());
            inner.Verify(v => v.WriteLine("ab"), Times.Once());
            inner.Verify(v => v.WriteAsync("ef"), Times.Once());
            inner.Verify(v => v.WriteLineAsync("gh"), Times.Once());
        }
        public async Task Write_WritesStringBuffer()
        {
            // Arrange
            var newLine = Environment.NewLine;
            var input1  = "Hello";
            var input2  = "from";
            var input3  = "ASP";
            var input4  = ".Net";
            var buffer  = new ViewBuffer(new TestViewBufferScope(), "some-name", pageSize: 4);
            var writer  = new ViewBufferTextWriter(buffer, Encoding.UTF8);

            // Act
            writer.Write(input1);
            writer.WriteLine(input2);
            await writer.WriteAsync(input3);

            await writer.WriteLineAsync(input4);

            // Assert
            var actual = GetValues(buffer);

            Assert.Equal <object>(new[] { input1, input2, newLine, input3, input4, newLine }, actual);
        }