Beispiel #1
0
        public void MoveTo_ViewBuffer_TakesPage_IfOriginalIsEmpty()
        {
            // Arrange
            var scope = new TestViewBufferScope();

            var original = new ViewBuffer(scope, "original", pageSize: 4);
            var other    = new ViewBuffer(scope, "other", pageSize: 4);

            other.AppendHtml("Hi");

            var page = other[0];

            // Act
            other.MoveTo(original);

            // Assert
            Assert.Equal(0, other.Count); // Page was taken
            Assert.Equal(1, original.Count);
            Assert.Same(page, original[0]);
        }
Beispiel #2
0
        [InlineData(65)]            // Create many pages before clear
        public void Clear_ResetsBackingBufferAndIndex(int valuesToWrite)
        {
            // Arrange
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name", pageSize: 32);

            // Act
            for (var i = 0; i < valuesToWrite; i++)
            {
                buffer.AppendHtml("Hello");
            }
            buffer.Clear();
            buffer.AppendHtml("world");

            // Assert
            Assert.Equal(1, buffer.Count);
            var page = buffer[0];

            Assert.Equal(1, page.Count);
            Assert.Equal("world", page.Buffer[0].Value);
        }
Beispiel #3
0
        public IHtmlContent Render()
        {
            var defaultActions = GetDefaultActions();
            var modeViewPath   = _readOnly ? DisplayTemplateViewPath : EditorTemplateViewPath;

            foreach (string viewName in GetViewNames())
            {
                var viewEngineResult = _viewEngine.GetView(_viewContext.ExecutingFilePath, viewName, isMainPage: false);
                if (!viewEngineResult.Success)
                {
                    var fullViewName = modeViewPath + "/" + viewName;
                    viewEngineResult = _viewEngine.FindView(_viewContext, fullViewName, isMainPage: false);
                }

                if (viewEngineResult.Success)
                {
                    var viewBuffer = new ViewBuffer(_bufferScope, viewName, ViewBuffer.PartialViewPageSize);
                    using (var writer = new ViewBufferTextWriter(viewBuffer, _viewContext.Writer.Encoding))
                    {
                        // Forcing synchronous behavior so users don't have to await templates.
                        var view = viewEngineResult.View;
                        using (view as IDisposable)
                        {
                            var viewContext = new ViewContext(_viewContext, viewEngineResult.View, _viewData, writer);
                            var renderTask  = viewEngineResult.View.RenderAsync(viewContext);
                            renderTask.GetAwaiter().GetResult();
                            return(viewBuffer);
                        }
                    }
                }

                Func <IHtmlHelper, IHtmlContent> defaultAction;
                if (defaultActions.TryGetValue(viewName, out defaultAction))
                {
                    return(defaultAction(MakeHtmlHelper(_viewContext, _viewData)));
                }
            }

            throw new InvalidOperationException(
                      Resources.FormatTemplateHelpers_NoTemplate(_viewData.ModelExplorer.ModelType.FullName));
        }
        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 void MoveTo_FlattensAndClears()
        {
            // Arrange
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name", pageSize: 4);

            var nestedItems = new List <object>();
            var nested      = new HtmlContentBuilder(nestedItems);

            nested.AppendHtml("Hello");
            buffer.AppendHtml(nested);

            var destinationItems = new List <object>();
            var destination      = new HtmlContentBuilder(destinationItems);

            // Act
            buffer.MoveTo(destination);

            // Assert
            Assert.Empty(nestedItems);
            Assert.Empty(buffer.Pages);
            Assert.Equal("Hello", Assert.IsType <HtmlEncodedString>(destinationItems[0]).Value);
        }
Beispiel #6
0
        public void CopyTo_Flattens()
        {
            // Arrange
            var buffer = new ViewBuffer(new TestViewBufferScope(), "some-name", pageSize: 4);

            var nestedItems = new List <object>();
            var nested      = new HtmlContentBuilder(nestedItems);

            nested.AppendHtml("Hello");
            buffer.AppendHtml(nested);

            var destinationItems = new List <object>();
            var destination      = new HtmlContentBuilder(destinationItems);

            // Act
            buffer.CopyTo(destination);

            // Assert
            Assert.Same(nested, buffer[0].Buffer[0].Value);
            Assert.Equal("Hello", Assert.IsType <HtmlString>(nestedItems[0]).Value);
            Assert.Equal("Hello", Assert.IsType <HtmlString>(destinationItems[0]).Value);
        }
Beispiel #7
0
        private void MoveTo(ViewBuffer destination)
        {
            for (var i = 0; i < Pages.Count; i++)
            {
                var page = Pages[i];

                var destinationPage = destination.Pages.Count == 0 ? null : destination.Pages[destination.Pages.Count - 1];

                // If the source page is less or equal to than half full, let's copy it's content to the destination
                // page if possible.
                var isLessThanHalfFull = 2 * page.Count <= page.Capacity;
                if (isLessThanHalfFull &&
                    destinationPage != null &&
                    destinationPage.Capacity - destinationPage.Count >= page.Count)
                {
                    // We have room, let's copy the items.
                    Array.Copy(
                        sourceArray: page.Buffer,
                        sourceIndex: 0,
                        destinationArray: destinationPage.Buffer,
                        destinationIndex: destinationPage.Count,
                        length: page.Count);

                    destinationPage.Count += page.Count;

                    // Now we can return the source page, and it can be reused in the scope of this request.
                    Array.Clear(page.Buffer, 0, page.Count);
                    _bufferScope.ReturnSegment(page.Buffer);
                }
                else
                {
                    // Otherwise, let's just add the source page to the other buffer.
                    destination.Pages.Add(page);
                }
            }

            Pages.Clear();
        }
        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);
        }
Beispiel #9
0
        public void MoveTo_ViewBuffer_CopiesItems_IfCurrentPageHasRoom()
        {
            // Arrange
            var scope = new TestViewBufferScope();

            var original = new ViewBuffer(scope, "original", pageSize: 4);
            var other    = new ViewBuffer(scope, "other", pageSize: 4);

            for (var i = 0; i < 2; i++)
            {
                original.AppendHtml($"original-{i}");
            }

            // With two items, this is half full so we try to copy the items.
            for (var i = 0; i < 2; i++)
            {
                other.AppendHtml($"other-{i}");
            }

            var page = other[0];

            // Act
            other.MoveTo(original);

            // Assert
            Assert.Equal(0, other.Count);                        // Other is cleared
            Assert.Contains(page.Buffer, scope.ReturnedBuffers); // Buffer was returned

            Assert.Equal(1, original.Count);
            Assert.Collection(
                original[0].Buffer,
                item => Assert.Equal("original-0", item.Value),
                item => Assert.Equal("original-1", item.Value),
                item => Assert.Equal("other-0", item.Value),
                item => Assert.Equal("other-1", item.Value));
        }