示例#1
0
        public void CopyConstructor_CopiesExpectedProperties()
        {
            // Arrange
            var httpContext = new DefaultHttpContext();
            var originalContext = new ViewContext(
                new ActionContext(httpContext, new RouteData(), new ActionDescriptor()),
                view: Mock.Of<IView>(),
                viewData: new ViewDataDictionary(metadataProvider: new EmptyModelMetadataProvider()),
                tempData: new TempDataDictionary(httpContext, Mock.Of<ITempDataProvider>()),
                writer: TextWriter.Null,
                htmlHelperOptions: new HtmlHelperOptions());
            var view = Mock.Of<IView>();
            var viewData = new ViewDataDictionary(originalContext.ViewData);
            var writer = new HtmlContentWrapperTextWriter(new HtmlContentBuilder(), Encoding.UTF8);

            // Act
            var context = new ViewContext(originalContext, view, viewData, writer);

            // Assert
            Assert.Same(originalContext.ActionDescriptor, context.ActionDescriptor);
            Assert.Equal(originalContext.ClientValidationEnabled, context.ClientValidationEnabled);
            Assert.Same(originalContext.ExecutingFilePath, context.ExecutingFilePath);
            Assert.Same(originalContext.FormContext, context.FormContext);
            Assert.Equal(originalContext.Html5DateRenderingMode, context.Html5DateRenderingMode);
            Assert.Same(originalContext.HttpContext, context.HttpContext);
            Assert.Same(originalContext.ModelState, context.ModelState);
            Assert.Same(originalContext.RouteData, context.RouteData);
            Assert.Same(originalContext.TempData, context.TempData);
            Assert.Same(originalContext.ValidationMessageElement, context.ValidationMessageElement);
            Assert.Same(originalContext.ValidationSummaryMessageElement, context.ValidationSummaryMessageElement);

            Assert.Same(view, context.View);
            Assert.Same(viewData, context.ViewData);
            Assert.Same(writer, context.Writer);
        }
示例#2
0
        /// <summary>
        /// Creates a new instance of <see cref="RazorTextWriter"/>.
        /// </summary>
        /// <param name="unbufferedWriter">The <see cref="TextWriter"/> to write output to when this instance
        /// is no longer buffering.</param>
        /// <param name="buffer">The <see cref="ViewBuffer"/> to buffer output to.</param>
        /// <param name="encoder">The HTML encoder.</param>
        public RazorTextWriter(TextWriter unbufferedWriter, ViewBuffer buffer, HtmlEncoder encoder)
        {
            UnbufferedWriter = unbufferedWriter;
            Buffer = buffer;
            HtmlEncoder = encoder;

            BufferedWriter = new HtmlContentWrapperTextWriter(buffer, unbufferedWriter.Encoding);
            TargetWriter = BufferedWriter;
        }
示例#3
0
        public void Write_Object_HtmlContent_AddsToEntries()
        {
            // Arrange
            var buffer  = new TestHtmlContentBuilder();
            var writer  = new HtmlContentWrapperTextWriter(buffer, Encoding.UTF8);
            var content = new HtmlString("Hello, world!");

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

            // Assert
            Assert.Collection(
                buffer.Values,
                item => Assert.Same(content, item));
        }
        public void Write_Object_HtmlContent_AddsToEntries()
        {
            // Arrange
            var buffer = new TestHtmlContentBuilder();
            var writer = new HtmlContentWrapperTextWriter(buffer, Encoding.UTF8);
            var content = new HtmlString("Hello, world!");

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

            // Assert
            Assert.Collection(
                buffer.Values,
                item => Assert.Same(content, item));
        }
示例#5
0
        public void Write_SplitsCharBuffer_Into1kbStrings()
        {
            // Arrange
            var charArray = Enumerable.Range(0, 2050).Select(_ => 'a').ToArray();
            var buffer    = new TestHtmlContentBuilder();
            var writer    = new HtmlContentWrapperTextWriter(buffer, Encoding.UTF8);

            // Act
            writer.Write(charArray);

            // Assert
            Assert.Collection(
                buffer.Values,
                value => Assert.Equal(new string('a', 1024), value),
                value => Assert.Equal(new string('a', 1024), value),
                value => Assert.Equal("aa", value));
        }
        public void Write_SplitsCharBuffer_Into1kbStrings()
        {
            // Arrange
            var charArray = Enumerable.Range(0, 2050).Select(_ => 'a').ToArray();
            var buffer = new TestHtmlContentBuilder();
            var writer = new HtmlContentWrapperTextWriter(buffer, Encoding.UTF8);

            // Act
            writer.Write(charArray);

            // Assert
            Assert.Collection(
                buffer.Values,
                value => Assert.Equal(new string('a', 1024), value),
                value => Assert.Equal(new string('a', 1024), value),
                value => Assert.Equal("aa", value));
        }
示例#7
0
        /// <inheritdoc />
        public async Task <IHtmlContent> PartialAsync(
            string partialViewName,
            object model,
            ViewDataDictionary viewData)
        {
            if (partialViewName == null)
            {
                throw new ArgumentNullException(nameof(partialViewName));
            }

            var viewBuffer = new ViewBuffer(_bufferScope, partialViewName);

            using (var writer = new HtmlContentWrapperTextWriter(viewBuffer, Encoding.UTF8))
            {
                await RenderPartialCoreAsync(partialViewName, model, viewData, writer);

                return(writer.ContentBuilder);
            }
        }
示例#8
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 TestHtmlContentBuilder();
            var writer  = new HtmlContentWrapperTextWriter(buffer, Encoding.UTF8);

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

            await writer.WriteLineAsync(input4);

            // Assert
            Assert.Equal(new[] { input1, input2, newLine, input3, input4, newLine }, buffer.Values);
        }
        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 buffer = new TestHtmlContentBuilder();
            var writer = new HtmlContentWrapperTextWriter(buffer, 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
            Assert.Collection(buffer.Values,
                value => Assert.Equal("bcd", value),
                value => Assert.Equal("ef", value),
                value => Assert.Equal("j", value),
                value => Assert.Equal(Environment.NewLine, value));
        }
示例#10
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 buffer = new TestHtmlContentBuilder();
            var writer = new HtmlContentWrapperTextWriter(buffer, 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
            Assert.Collection(buffer.Values,
                              value => Assert.Equal("bcd", value),
                              value => Assert.Equal("ef", value),
                              value => Assert.Equal("j", value),
                              value => Assert.Equal(Environment.NewLine, value));
        }
        private async Task<IHtmlContent> InvokeCoreAsync(
            ViewComponentDescriptor descriptor,
            object arguments)
        {
            var viewBuffer = new ViewBuffer(_viewBufferScope, descriptor.FullName);
            using (var writer = new HtmlContentWrapperTextWriter(viewBuffer, _viewContext.Writer.Encoding))
            {
                var context = new ViewComponentContext(
                    descriptor,
                    PropertyHelper.ObjectToDictionary(arguments),
                    _htmlEncoder,
                    _viewContext,
                    writer);

                var invoker = _invokerFactory.CreateInstance(context);
                if (invoker == null)
                {
                    throw new InvalidOperationException(
                        Resources.FormatViewComponent_IViewComponentFactory_ReturnedNull(descriptor.FullName));
                }

                await invoker.InvokeAsync(context);
                return writer.ContentBuilder;
            }
        }
        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 TestHtmlContentBuilder();
            var writer = new HtmlContentWrapperTextWriter(buffer, Encoding.UTF8);

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

            // Assert
            Assert.Equal(new[] { input1, input2, newLine, input3, input4, newLine }, buffer.Values);
        }