Пример #1
0
        public void Write_WritesDataTypes_ToUnderlyingStream_WhenNotBuffering()
        {
            // Arrange
            var expected = new[] { "True", "3", "18446744073709551615", "Hello world", "3.14", "2.718" };
            var unbufferedWriter = new Mock<TextWriter>();
            unbufferedWriter.SetupGet(w => w.Encoding).Returns(Encoding.UTF8);
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name");
            var writer = new RazorTextWriter(unbufferedWriter.Object, buffer, new HtmlTestEncoder());
            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.Null(buffer.BufferSegments);
            foreach (var item in expected)
            {
                unbufferedWriter.Verify(v => v.Write(item), Times.Once());
            }
        }
Пример #2
0
        public void Write_WritesDataTypes_ToUnderlyingStream_WhenNotBuffering()
        {
            // Arrange
            var expected         = new[] { "True", "3", "18446744073709551615", "Hello world", "3.14", "2.718" };
            var unbufferedWriter = new Mock <TextWriter>();

            unbufferedWriter.SetupGet(w => w.Encoding).Returns(Encoding.UTF8);
            var buffer    = new ViewBuffer(new TestViewBufferScope(), "some-name");
            var writer    = new RazorTextWriter(unbufferedWriter.Object, buffer, new HtmlTestEncoder());
            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.Null(buffer.BufferSegments);
            foreach (var item in expected)
            {
                unbufferedWriter.Verify(v => v.Write(item), Times.Once());
            }
        }
Пример #3
0
        public async Task Write_WritesCharValues_ToUnderlyingStream_WhenNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock<TextWriter> { CallBase = true };
            var writer = new RazorTextWriter(unbufferedWriter.Object, Encoding.UTF8);
            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
            Assert.Empty(writer.BufferedWriter.Buffer.BufferEntries);
            unbufferedWriter.Verify(v => v.Write('x'), Times.Once());
            unbufferedWriter.Verify(v => v.Write(buffer1, 1, 2), Times.Once());
            unbufferedWriter.Verify(v => v.Write(buffer1, 0, 4), Times.Once());
            unbufferedWriter.Verify(v => v.Write(buffer2, 0, 3), Times.Once());
            unbufferedWriter.Verify(v => v.WriteAsync(buffer2, 1, 1), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLine(), Times.Once());
        }
Пример #4
0
        public async Task Write_WritesCharValues_ToUnderlyingStream_WhenNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock <TextWriter> {
                CallBase = true
            };
            var writer  = new RazorTextWriter(unbufferedWriter.Object, Encoding.UTF8);
            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
            Assert.Empty(writer.BufferedWriter.Content.Entries);
            unbufferedWriter.Verify(v => v.Write('x'), Times.Once());
            unbufferedWriter.Verify(v => v.Write(buffer1, 1, 2), Times.Once());
            unbufferedWriter.Verify(v => v.Write(buffer1, 0, 4), Times.Once());
            unbufferedWriter.Verify(v => v.Write(buffer2, 0, 3), Times.Once());
            unbufferedWriter.Verify(v => v.WriteAsync(buffer2, 1, 1), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLine(), Times.Once());
        }
Пример #5
0
        public async Task Write_WritesStringValues_ToUnbufferedStream_WhenNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock <TextWriter>();

            unbufferedWriter.SetupGet(w => w.Encoding).Returns(Encoding.UTF8);
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name");
            var writer = new RazorTextWriter(unbufferedWriter.Object, buffer, new HtmlTestEncoder());

            // Act
            await writer.FlushAsync();

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

            await writer.WriteLineAsync("gh");

            // Assert
            Assert.Null(buffer.BufferSegments);
            unbufferedWriter.Verify(v => v.Write("a"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLine("ab"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteAsync("ef"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLineAsync("gh"), Times.Once());
        }
Пример #6
0
        public async Task Write_WritesCharValues_ToUnderlyingStream_WhenNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock <TextWriter> {
                CallBase = true
            };

            unbufferedWriter.SetupGet(w => w.Encoding).Returns(Encoding.UTF8);
            var buffer  = new ViewBuffer(new TestViewBufferScope(), "some-name");
            var writer  = new RazorTextWriter(unbufferedWriter.Object, buffer, new HtmlTestEncoder());
            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
            Assert.Null(buffer.BufferSegments);
            unbufferedWriter.Verify(v => v.Write('x'), Times.Once());
            unbufferedWriter.Verify(v => v.Write(buffer1, 1, 2), Times.Once());
            unbufferedWriter.Verify(v => v.Write(buffer1, 0, 4), Times.Once());
            unbufferedWriter.Verify(v => v.Write(buffer2, 0, 3), Times.Once());
            unbufferedWriter.Verify(v => v.WriteAsync(buffer2, 1, 1), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLine(), Times.Once());
        }
Пример #7
0
        public async Task Write_WritesCharValues_ToUnderlyingStream_WhenNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock<TextWriter> { CallBase = true };
            unbufferedWriter.SetupGet(w => w.Encoding).Returns(Encoding.UTF8);
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name");
            var writer = new RazorTextWriter(unbufferedWriter.Object, buffer, new HtmlTestEncoder());
            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
            Assert.Null(buffer.BufferSegments);
            unbufferedWriter.Verify(v => v.Write('x'), Times.Once());
            unbufferedWriter.Verify(v => v.Write(buffer1, 1, 2), Times.Once());
            unbufferedWriter.Verify(v => v.Write(buffer1, 0, 4), Times.Once());
            unbufferedWriter.Verify(v => v.Write(buffer2, 0, 3), Times.Once());
            unbufferedWriter.Verify(v => v.WriteAsync(buffer2, 1, 1), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLine(), Times.Once());
        }
Пример #8
0
        public async Task WriteLines_WritesCharBuffer()
        {
            // Arrange
            var newLine = Environment.NewLine;
            var writer = new RazorTextWriter(Encoding.UTF8);

            // Act
            writer.WriteLine();
            await writer.WriteLineAsync();

            // Assert
            var actual = writer.Buffer.BufferEntries;
            Assert.Equal<object>(new[] { newLine, newLine }, actual);
        }
Пример #9
0
        public void Write_Object_HtmlContent_AddsToEntries()
        {
            // Arrange
            var writer  = new RazorTextWriter(TextWriter.Null, Encoding.UTF8, new CommonTestEncoder());
            var content = new HtmlString("Hello, world!");

            // Act
            writer.Write((object)content);

            // Assert
            Assert.Collection(
                writer.BufferedWriter.Entries,
                item => Assert.Same(content, item));
        }
Пример #10
0
        public async Task WriteLines_WritesCharBuffer()
        {
            // Arrange
            var newLine = Environment.NewLine;
            var writer  = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);

            // Act
            writer.WriteLine();
            await writer.WriteLineAsync();

            // Assert
            var actual = writer.BufferedWriter.Content.Entries;

            Assert.Equal <object>(new[] { newLine, newLine }, actual);
        }
Пример #11
0
        public void WriteLine_WritesDataTypes_ToBuffer()
        {
            // Arrange 
            var newLine = Environment.NewLine;
            var expected = new List<object> { "False", newLine, "1.1", newLine, "3", newLine };
            var writer = new RazorTextWriter(Encoding.UTF8);

            // Act
            writer.WriteLine(false);
            writer.WriteLine(1.1f);
            writer.WriteLine(3L);

            // Assert
            Assert.Equal(expected, writer.Buffer.BufferEntries);
        }
Пример #12
0
        public async Task WriteLines_WritesCharBuffer()
        {
            // Arrange
            var newLine = Environment.NewLine;
            var buffer  = new ViewBuffer(new TestViewBufferScope(), "some-name");
            var writer  = new RazorTextWriter(TextWriter.Null, buffer, new HtmlTestEncoder());

            // Act
            writer.WriteLine();
            await writer.WriteLineAsync();

            // Assert
            var actual = GetValues(buffer);

            Assert.Equal <object>(new[] { newLine, newLine }, actual);
        }
Пример #13
0
        public async Task CopyAsync_WritesContent_IfTargetTextWriterIsARazorTextWriterAndBuffering()
        {
            // Arrange
            var source = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);
            var target = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);

            // Act
            source.WriteLine("Hello world");
            source.Write(new[] { 'x', 'a', 'b', 'c' }, 1, 3);
            await source.CopyToAsync(target);

            // Assert
            Assert.Equal(3, source.BufferedWriter.Content.Entries.Count);
            Assert.Equal(1, target.BufferedWriter.Content.Entries.Count);
            Assert.Equal(source.BufferedWriter.Content, Assert.Single(target.BufferedWriter.Content.Entries));
        }
Пример #14
0
        public void Copy_WritesContent_IfTargetTextWriterIsNotARazorTextWriter()
        {
            // Arrange
            var source   = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);
            var target   = new StringWriter();
            var expected = @"Hello world
abc";

            // Act
            source.WriteLine("Hello world");
            source.Write(new[] { 'x', 'a', 'b', 'c' }, 1, 3);
            source.CopyTo(target);

            // Assert
            Assert.Equal(expected, target.ToString());
        }
Пример #15
0
        public void Write_HtmlContent_AfterFlush_GoesToStream()
        {
            // Arrange
            var stringWriter = new StringWriter();
            var buffer       = new ViewBuffer(new TestViewBufferScope(), "some-name");
            var writer       = new RazorTextWriter(stringWriter, buffer, new HtmlTestEncoder());

            writer.Flush();

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

            // Act
            writer.Write(content);

            // Assert
            Assert.Equal("Hello, world!", stringWriter.ToString());
        }
Пример #16
0
        public void WriteLine_WritesDataTypes_ToBuffer()
        {
            // Arrange
            var newLine  = Environment.NewLine;
            var expected = new List <object> {
                "False", newLine, "1.1", newLine, "3", newLine
            };
            var writer = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);

            // Act
            writer.WriteLine(false);
            writer.WriteLine(1.1f);
            writer.WriteLine(3L);

            // Assert
            Assert.Equal(expected, writer.BufferedWriter.Content.Entries);
        }
Пример #17
0
        public void Write_HtmlContent_AfterFlush_GoesToStream()
        {
            // Arrange
            var stringWriter = new StringWriter();

            var writer = new RazorTextWriter(stringWriter, Encoding.UTF8, new CommonTestEncoder());

            writer.Flush();

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

            // Act
            writer.Write(content);

            // Assert
            Assert.Equal("Hello, world!", stringWriter.ToString());
        }
Пример #18
0
        public async Task CopyAsync_WritesContent_IfTargetTextWriterIsNotARazorTextWriter()
        {
            // Arrange
            var source   = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);
            var target   = new StringWriter();
            var expected = @"Hello world
";

            // Act
            source.Write("Hello ");
            await source.WriteLineAsync(new[] { 'w', 'o', 'r', 'l', 'd' });

            await source.CopyToAsync(target);

            // Assert
            Assert.Equal(expected, target.ToString());
        }
Пример #19
0
        public void Copy_CopiesContent_IfTargetTextWriterIsARazorTextWriterAndBuffering()
        {
            // Arrange
            var source = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);
            var target = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);

            // Act
            source.Write("Hello world");
            source.Write(new char[1], 0, 1);
            source.CopyTo(target);

            // Assert
            // Make sure content was written to the source.
            Assert.Equal(2, source.BufferedWriter.Content.Entries.Count);
            Assert.Equal(1, target.BufferedWriter.Content.Entries.Count);
            Assert.Same(source.BufferedWriter.Content, Assert.Single(target.BufferedWriter.Content.Entries));
        }
Пример #20
0
        public void WriteLine_WritesDataTypes_ToBuffer()
        {
            // Arrange
            var newLine  = Environment.NewLine;
            var expected = new List <object> {
                "False", newLine, "1.1", newLine, "3", newLine
            };
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name");
            var writer = new RazorTextWriter(TextWriter.Null, buffer, new HtmlTestEncoder());

            // Act
            writer.WriteLine(false);
            writer.WriteLine(1.1f);
            writer.WriteLine(3L);

            // Assert
            Assert.Equal(expected, GetValues(buffer));
        }
Пример #21
0
        public void Write_WritesDataTypes_ToBuffer()
        {
            // Arrange
            var expected = new[] { "True", "3", "18446744073709551615", "Hello world", "3.14", "2.718", "m" };
            var writer = new RazorTextWriter(TextWriter.Null, 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<object>(expected, writer.BufferedWriter.Buffer.BufferEntries);
        }
Пример #22
0
        public void Write_WritesDataTypes_ToBuffer()
        {
            // Arrange
            var expected = new[] { "True", "3", "18446744073709551615", "Hello world", "3.14", "2.718", "m" };
            var writer   = new RazorTextWriter(TextWriter.Null, 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, writer.BufferedWriter.Content.Entries);
        }
Пример #23
0
        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");
            var writer   = new RazorTextWriter(TextWriter.Null, buffer, new HtmlTestEncoder());

            // 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));
        }
Пример #24
0
        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");
            var writer = new RazorTextWriter(TextWriter.Null, buffer, new HtmlTestEncoder());

            // 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));
        }
Пример #25
0
        public void WriteLine_WritesDataTypes_ToUnbufferedStream_WhenNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock <TextWriter>();
            var writer           = new RazorTextWriter(unbufferedWriter.Object, Encoding.UTF8);

            // Act
            writer.Flush();
            writer.WriteLine(false);
            writer.WriteLine(1.1f);
            writer.WriteLine(3L);

            // Assert
            Assert.Empty(writer.BufferedWriter.Content.ToString());
            unbufferedWriter.Verify(v => v.Write("False"), Times.Once());
            unbufferedWriter.Verify(v => v.Write("1.1"), Times.Once());
            unbufferedWriter.Verify(v => v.Write("3"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLine(), Times.Exactly(3));
        }
Пример #26
0
        public void Copy_CopiesContent_IfTargetTextWriterIsARazorTextWriterAndNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock <TextWriter>();
            var source           = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);
            var target           = new RazorTextWriter(unbufferedWriter.Object, Encoding.UTF8);

            // Act
            target.Flush();
            source.Write("Hello world");
            source.Write(new[] { 'a', 'b', 'c', 'd' }, 1, 2);
            source.CopyTo(target);

            // Assert
            // Make sure content was written to the source.
            Assert.Equal(2, source.BufferedWriter.Content.Entries.Count);
            Assert.Empty(target.BufferedWriter.Content.ToString());
            unbufferedWriter.Verify(v => v.Write("Hello world"), Times.Once());
            unbufferedWriter.Verify(v => v.Write("bc"), Times.Once());
        }
Пример #27
0
        public async Task Write_WritesCharBuffer()
        {
            // Arrange
            var input1 = new ArraySegment<char>(new char[] { 'a', 'b', 'c', 'd' }, 1, 3);
            var input2 = new ArraySegment<char>(new char[] { 'e', 'f' }, 0, 2);
            var input3 = new ArraySegment<char>(new char[] { 'g', 'h', 'i', 'j' }, 3, 1);
            var writer = new RazorTextWriter(Encoding.UTF8);

            // Act
            writer.Write(input1.Array, input1.Offset, input1.Count);
            await writer.WriteAsync(input2.Array, input2.Offset, input2.Count);
            await writer.WriteLineAsync(input3.Array, input3.Offset, input3.Count);

            // Assert
            var buffer = writer.Buffer.BufferEntries;
            Assert.Equal(4, buffer.Count);
            Assert.Equal("bcd", buffer[0]);
            Assert.Equal("ef", buffer[1]);
            Assert.Equal("j", buffer[2]);
            Assert.Equal(Environment.NewLine, buffer[3]);
        }
Пример #28
0
        public async Task Write_WritesStringBuffer()
        {
            // Arrange
            var newLine = Environment.NewLine;
            var input1  = "Hello";
            var input2  = "from";
            var input3  = "ASP";
            var input4  = ".Net";
            var writer  = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);

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

            await writer.WriteLineAsync(input4);

            // Assert
            var actual = writer.BufferedWriter.Content.Entries;

            Assert.Equal <object>(new[] { input1, input2, newLine, input3, input4, newLine }, actual);
        }
Пример #29
0
        public void WriteLine_WritesDataTypes_ToUnbufferedStream_WhenNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock <TextWriter>();

            unbufferedWriter.SetupGet(w => w.Encoding).Returns(Encoding.UTF8);
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name");
            var writer = new RazorTextWriter(unbufferedWriter.Object, buffer, new HtmlTestEncoder());

            // Act
            writer.Flush();
            writer.WriteLine(false);
            writer.WriteLine(1.1f);
            writer.WriteLine(3L);

            // Assert
            Assert.Null(buffer.BufferSegments);
            unbufferedWriter.Verify(v => v.Write("False"), Times.Once());
            unbufferedWriter.Verify(v => v.Write("1.1"), Times.Once());
            unbufferedWriter.Verify(v => v.Write("3"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLine(), Times.Exactly(3));
        }
Пример #30
0
        public async Task Write_WritesStringValues_ToUnbufferedStream_WhenNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock <TextWriter>();
            var writer           = new RazorTextWriter(unbufferedWriter.Object, Encoding.UTF8);

            // Act
            await writer.FlushAsync();

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

            await writer.WriteLineAsync("gh");

            // Assert
            Assert.Empty(writer.BufferedWriter.Content.Entries);
            unbufferedWriter.Verify(v => v.Write("a"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLine("ab"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteAsync("ef"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLineAsync("gh"), Times.Once());
        }
Пример #31
0
        public async Task Write_WritesCharBuffer()
        {
            // Arrange
            var input1 = new ArraySegment <char>(new char[] { 'a', 'b', 'c', 'd' }, 1, 3);
            var input2 = new ArraySegment <char>(new char[] { 'e', 'f' }, 0, 2);
            var input3 = new ArraySegment <char>(new char[] { 'g', 'h', 'i', 'j' }, 3, 1);
            var writer = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);

            // Act
            writer.Write(input1.Array, input1.Offset, input1.Count);
            await writer.WriteAsync(input2.Array, input2.Offset, input2.Count);

            await writer.WriteLineAsync(input3.Array, input3.Offset, input3.Count);

            // Assert
            var buffer = writer.BufferedWriter.Content.Entries;

            Assert.Equal(4, buffer.Count);
            Assert.Equal("bcd", buffer[0]);
            Assert.Equal("ef", buffer[1]);
            Assert.Equal("j", buffer[2]);
            Assert.Equal(Environment.NewLine, buffer[3]);
        }
Пример #32
0
        //[Fact]
        // IHtmlContent currently does not support async writes. Hence disabling this test.
        public async Task CopyAsync_WritesContent_IfTargetTextWriterIsARazorTextWriterAndNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock <TextWriter>();
            var source           = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);
            var target           = new RazorTextWriter(unbufferedWriter.Object, Encoding.UTF8);

            // Act
            await target.FlushAsync();

            source.WriteLine("Hello from Asp.Net");
            await source.WriteAsync(new[] { 'x', 'y', 'z', 'u' }, 0, 3);

            await source.CopyToAsync(target);

            // Assert
            // Make sure content was written to the source.
            Assert.Equal(3, source.BufferedWriter.Content.Entries.Count);
            Assert.Empty(target.BufferedWriter.Content.ToString());
            unbufferedWriter.Verify(v => v.WriteAsync("Hello from Asp.Net"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteAsync(Environment.NewLine), Times.Once());
            unbufferedWriter.Verify(v => v.WriteAsync("xyz"), Times.Once());
        }
Пример #33
0
        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");
            var writer  = new RazorTextWriter(TextWriter.Null, buffer, new HtmlTestEncoder());

            // 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);
        }
Пример #34
0
        public void Write_WritesDataTypes_ToUnderlyingStream_WhenNotBuffering()
        {
            // Arrange
            var expected = new[] { "True", "3", "18446744073709551615", "Hello world", "3.14", "2.718" };
            var unbufferedWriter = new Mock<TextWriter>();
            var writer = new RazorTextWriter(unbufferedWriter.Object, Encoding.UTF8);
            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.Empty(writer.BufferedWriter.Buffer.BufferEntries);
            foreach (var item in expected)
            {
                unbufferedWriter.Verify(v => v.Write(item), Times.Once());
            }
        }
Пример #35
0
        public void Write_WritesDataTypes_ToUnderlyingStream_WhenNotBuffering()
        {
            // Arrange
            var expected         = new[] { "True", "3", "18446744073709551615", "Hello world", "3.14", "2.718" };
            var unbufferedWriter = new Mock <TextWriter>();
            var writer           = new RazorTextWriter(unbufferedWriter.Object, Encoding.UTF8);
            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.Empty(writer.BufferedWriter.Content.Entries);
            foreach (var item in expected)
            {
                unbufferedWriter.Verify(v => v.Write(item), Times.Once());
            }
        }
Пример #36
0
        public async Task CopyAsync_WritesContent_IfTargetTextWriterIsARazorTextWriterAndNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock<TextWriter>();
            var source = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);
            var target = new RazorTextWriter(unbufferedWriter.Object, Encoding.UTF8);

            // Act
            await target.FlushAsync();
            source.WriteLine("Hello from Asp.Net");
            await source.WriteAsync(new[] { 'x', 'y', 'z', 'u' }, 0, 3);
            await source.CopyToAsync(target);

            // Assert
            // Make sure content was written to the source.
            Assert.Equal(3, source.BufferedWriter.Buffer.BufferEntries.Count);
            Assert.Empty(target.BufferedWriter.Buffer.BufferEntries);
            unbufferedWriter.Verify(v => v.WriteAsync("Hello from Asp.Net"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteAsync(Environment.NewLine), Times.Once());
            unbufferedWriter.Verify(v => v.WriteAsync("xyz"), Times.Once());
        }
Пример #37
0
        public async Task CopyAsync_WritesContent_IfTargetTextWriterIsNotARazorTextWriter()
        {
            // Arrange
            var source = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);
            var target = new StringWriter();
            var expected = @"Hello world
";

            // Act
            source.Write("Hello ");
            await source.WriteLineAsync(new[] { 'w', 'o', 'r', 'l', 'd' });
            await source.CopyToAsync(target);

            // Assert
            Assert.Equal(expected, target.ToString());
        }
Пример #38
0
        public void Copy_WritesContent_IfTargetTextWriterIsNotARazorTextWriter()
        {
            // Arrange
            var source = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);
            var target = new StringWriter();
            var expected = @"Hello world
abc";

            // Act
            source.WriteLine("Hello world");
            source.Write(new[] { 'x', 'a', 'b', 'c' }, 1, 3);
            source.CopyTo(target);

            // Assert
            Assert.Equal(expected, target.ToString());
        }
Пример #39
0
        public async Task CopyAsync_WritesContent_IfTargetTextWriterIsARazorTextWriterAndBuffering()
        {
            // Arrange
            var source = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);
            var target = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);

            // Act
            source.WriteLine("Hello world");
            source.Write(new[] { 'x', 'a', 'b', 'c' }, 1, 3);
            await source.CopyToAsync(target);

            // Assert
            Assert.Equal(3, source.BufferedWriter.Buffer.BufferEntries.Count);
            Assert.Equal(1, target.BufferedWriter.Buffer.BufferEntries.Count);
            Assert.Same(source.BufferedWriter.Buffer.BufferEntries, target.BufferedWriter.Buffer.BufferEntries[0]);
        }
Пример #40
0
        public void Copy_CopiesContent_IfTargetTextWriterIsARazorTextWriterAndBuffering()
        {
            // Arrange
            var source = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);
            var target = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);

            // Act
            source.Write("Hello world");
            source.Write(new char[1], 0, 1);
            source.CopyTo(target);

            // Assert
            // Make sure content was written to the source.
            Assert.Equal(2, source.BufferedWriter.Buffer.BufferEntries.Count);
            Assert.Equal(1, target.BufferedWriter.Buffer.BufferEntries.Count);
            Assert.Same(source.BufferedWriter.Buffer.BufferEntries, target.BufferedWriter.Buffer.BufferEntries[0]);
        }
Пример #41
0
        public void Copy_CopiesContent_IfTargetTextWriterIsARazorTextWriterAndNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock<TextWriter>();
            var source = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);
            var target = new RazorTextWriter(unbufferedWriter.Object, Encoding.UTF8);

            // Act
            target.Flush();
            source.Write("Hello world");
            source.Write(new[] { 'a', 'b', 'c', 'd' }, 1, 2);
            source.CopyTo(target);

            // Assert
            // Make sure content was written to the source.
            Assert.Equal(2, source.BufferedWriter.Buffer.BufferEntries.Count);
            Assert.Empty(target.BufferedWriter.Buffer.BufferEntries);
            unbufferedWriter.Verify(v => v.Write("Hello world"), Times.Once());
            unbufferedWriter.Verify(v => v.Write("bc"), Times.Once());
        }
Пример #42
0
        public async Task Write_WritesStringValues_ToUnbufferedStream_WhenNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock<TextWriter>();
            unbufferedWriter.SetupGet(w => w.Encoding).Returns(Encoding.UTF8);
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name");
            var writer = new RazorTextWriter(unbufferedWriter.Object, buffer, new HtmlTestEncoder());

            // Act
            await writer.FlushAsync();
            writer.Write("a");
            writer.WriteLine("ab");
            await writer.WriteAsync("ef");
            await writer.WriteLineAsync("gh");

            // Assert
            Assert.Null(buffer.BufferSegments);
            unbufferedWriter.Verify(v => v.Write("a"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLine("ab"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteAsync("ef"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLineAsync("gh"), Times.Once());
        }
Пример #43
0
        public async Task Write_WritesStringBuffer()
        {
            // Arrange
            var newLine = Environment.NewLine;
            var input1 = "Hello";
            var input2 = "from";
            var input3 = "ASP";
            var input4 = ".Net";
            var writer = new RazorTextWriter(TextWriter.Null, Encoding.UTF8);

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

            // Assert
            var actual = writer.BufferedWriter.Buffer.BufferEntries;
            Assert.Equal<object>(new[] { input1, input2, newLine, input3, input4, newLine }, actual);
        }
Пример #44
0
        public void Write_HtmlContent_AfterFlush_GoesToStream()
        {
            // Arrange
            var stringWriter = new StringWriter();

            var writer = new RazorTextWriter(stringWriter, Encoding.UTF8, new CommonTestEncoder());
            writer.Flush();

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

            // Act
            writer.Write(content);

            // Assert
            Assert.Equal("Hello, world!", stringWriter.ToString());
        }
Пример #45
0
        public void WriteLine_WritesDataTypes_ToUnbufferedStream_WhenNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock<TextWriter>();
            var writer = new RazorTextWriter(unbufferedWriter.Object, Encoding.UTF8);

            // Act
            writer.Flush();
            writer.WriteLine(false);
            writer.WriteLine(1.1f);
            writer.WriteLine(3L);

            // Assert
            Assert.Empty(writer.BufferedWriter.Buffer.BufferEntries);
            unbufferedWriter.Verify(v => v.Write("False"), Times.Once());
            unbufferedWriter.Verify(v => v.Write("1.1"), Times.Once());
            unbufferedWriter.Verify(v => v.Write("3"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLine(), Times.Exactly(3));
        }
Пример #46
0
        public void WriteLine_Object_HtmlContent_AddsToEntries()
        {
            // Arrange
            var writer = new RazorTextWriter(TextWriter.Null, Encoding.UTF8, new CommonTestEncoder());
            var content = new HtmlString("Hello, world!");

            // Act
            writer.WriteLine(content);

            // Assert
            Assert.Collection(
                writer.BufferedWriter.Entries,
                item => Assert.Same(content, item),
                item => Assert.Equal(Environment.NewLine, item));
        }
Пример #47
0
        public void Write_HtmlContent_AfterFlush_GoesToStream()
        {
            // Arrange
            var stringWriter = new StringWriter();
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name");
            var writer = new RazorTextWriter(stringWriter, buffer, new HtmlTestEncoder());
            writer.Flush();

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

            // Act
            writer.Write(content);

            // Assert
            Assert.Equal("Hello, world!", stringWriter.ToString());
        }
Пример #48
0
        public void WriteLine_WritesDataTypes_ToUnbufferedStream_WhenNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock<TextWriter>();
            unbufferedWriter.SetupGet(w => w.Encoding).Returns(Encoding.UTF8);
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name");
            var writer = new RazorTextWriter(unbufferedWriter.Object, buffer, new HtmlTestEncoder());

            // Act
            writer.Flush();
            writer.WriteLine(false);
            writer.WriteLine(1.1f);
            writer.WriteLine(3L);

            // Assert
            Assert.Null(buffer.BufferSegments);
            unbufferedWriter.Verify(v => v.Write("False"), Times.Once());
            unbufferedWriter.Verify(v => v.Write("1.1"), Times.Once());
            unbufferedWriter.Verify(v => v.Write("3"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLine(), Times.Exactly(3));
        }
Пример #49
0
        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");
            var writer = new RazorTextWriter(TextWriter.Null, buffer, new HtmlTestEncoder());

            // 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);
        }
Пример #50
0
        public async Task WriteLines_WritesCharBuffer()
        {
            // Arrange
            var newLine = Environment.NewLine;
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name");
            var writer = new RazorTextWriter(TextWriter.Null, buffer, new HtmlTestEncoder());

            // Act
            writer.WriteLine();
            await writer.WriteLineAsync();

            // Assert
            var actual = GetValues(buffer);
            Assert.Equal<object>(new[] { newLine, newLine }, actual);
        }
Пример #51
0
        public async Task Write_WritesStringValues_ToUnbufferedStream_WhenNotBuffering()
        {
            // Arrange
            var unbufferedWriter = new Mock<TextWriter>();
            var writer = new RazorTextWriter(unbufferedWriter.Object, Encoding.UTF8);

            // Act
            await writer.FlushAsync();
            writer.Write("a");
            writer.WriteLine("ab");
            await writer.WriteAsync("ef");
            await writer.WriteLineAsync("gh");

            // Assert
            Assert.Empty(writer.BufferedWriter.Buffer.BufferEntries);
            unbufferedWriter.Verify(v => v.Write("a"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLine("ab"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteAsync("ef"), Times.Once());
            unbufferedWriter.Verify(v => v.WriteLineAsync("gh"), Times.Once());
        }
Пример #52
0
        public void WriteLine_WritesDataTypes_ToBuffer()
        {
            // Arrange
            var newLine = Environment.NewLine;
            var expected = new List<object> { "False", newLine, "1.1", newLine, "3", newLine };
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name");
            var writer = new RazorTextWriter(TextWriter.Null, buffer, new HtmlTestEncoder());

            // Act
            writer.WriteLine(false);
            writer.WriteLine(1.1f);
            writer.WriteLine(3L);

            // Assert
            Assert.Equal(expected, GetValues(buffer));
        }