Exemple #1
0
        public async Task RunAsync_CallsInitPriorToProcessAsync()
        {
            // Arrange
            var runner = new TagHelperRunner();
            var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var incrementer = 0;
            var callbackTagHelper = new CallbackTagHelper(
                initCallback: () =>
                {
                    Assert.Equal(0, incrementer);

                    incrementer++;
                },
                processAsyncCallback: () =>
                {
                    Assert.Equal(1, incrementer);

                    incrementer++;
                });
            executionContext.Add(callbackTagHelper);

            // Act
            await runner.RunAsync(executionContext);

            // Assert
            Assert.Equal(2, incrementer);
        }
        public async Task SetOutputContentAsync_SetsOutputsContent()
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();
            var content = "Hello from child content";
            var executionContext = new TagHelperExecutionContext(
                "p",
                tagMode: TagMode.StartTagAndEndTag,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () =>
                {
                    tagHelperContent.SetContent(content);

                    return Task.FromResult(result: true);
                },
                startTagHelperWritingScope: _ => { },
                endTagHelperWritingScope: () => tagHelperContent);

            // Act
            await executionContext.SetOutputContentAsync();

            // Assert
            Assert.Equal(content, executionContext.Output.Content.GetContent());
        }
        public async Task ExecutionContext_Reinitialize_UpdatesTagHelperOutputAsExpected()
        {
            // Arrange
            var tagName = "div";
            var tagMode = TagMode.StartTagOnly;
            var callCount = 0;
            Func<Task> executeChildContentAsync = () =>
            {
                callCount++;
                return Task.FromResult(true);
            };
            Action<HtmlEncoder> startTagHelperWritingScope = _ => { };
            Func<TagHelperContent> endTagHelperWritingScope = () => null;
            var executionContext = new TagHelperExecutionContext(
                tagName,
                tagMode,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: executeChildContentAsync,
                startTagHelperWritingScope: startTagHelperWritingScope,
                endTagHelperWritingScope: endTagHelperWritingScope);
            var updatedTagName = "p";
            var updatedTagMode = TagMode.SelfClosing;
            var updatedCallCount = 0;
            Func<Task> updatedExecuteChildContentAsync = () =>
            {
                updatedCallCount++;
                return Task.FromResult(true);
            };
            executionContext.AddHtmlAttribute(new TagHelperAttribute("something"));

            // Act - 1
            executionContext.Reinitialize(
                updatedTagName,
                updatedTagMode,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: updatedExecuteChildContentAsync);
            executionContext.AddHtmlAttribute(new TagHelperAttribute("Another attribute"));

            // Assert - 1
            var output = executionContext.Output;
            Assert.Equal(updatedTagName, output.TagName);
            Assert.Equal(updatedTagMode, output.TagMode);
            var attribute = Assert.Single(output.Attributes);
            Assert.Equal("Another attribute", attribute.Name);

            // Act - 2
            await output.GetChildContentAsync();

            // Assert - 2
            Assert.Equal(callCount, 0);
            Assert.Equal(updatedCallCount, 1);
        }
        public async Task RunAsync_AllowsDataRetrievalFromTagHelperContext()
        {
            // Arrange
            var runner           = new TagHelperRunner();
            var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var tagHelper        = new TagHelperContextTouchingTagHelper();

            // Act
            executionContext.Add(tagHelper);
            executionContext.AddTagHelperAttribute("foo", true);
            await runner.RunAsync(executionContext);

            // Assert
            Assert.Equal("True", executionContext.Output.Attributes["foo"].Value);
        }
Exemple #5
0
        public async Task RunAsync_ConfiguresTagHelperContextWithExecutionContextsItems()
        {
            // Arrange
            var runner           = new TagHelperRunner();
            var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var tagHelper        = new ContextInspectingTagHelper();

            executionContext.Add(tagHelper);

            // Act
            await runner.RunAsync(executionContext);

            // Assert
            Assert.NotNull(tagHelper.ContextProcessedWith);
            Assert.Same(tagHelper.ContextProcessedWith.Items, executionContext.Items);
        }
Exemple #6
0
        public async Task RunAsync_SetsTagHelperOutputTagMode(TagMode tagMode)
        {
            // Arrange
            var runner           = new TagHelperRunner();
            var executionContext = new TagHelperExecutionContext("p", tagMode);
            var tagHelper        = new TagHelperContextTouchingTagHelper();

            executionContext.Add(tagHelper);
            executionContext.AddTagHelperAttribute("foo", true, HtmlAttributeValueStyle.DoubleQuotes);

            // Act
            await runner.RunAsync(executionContext);

            // Assert
            Assert.Equal(tagMode, executionContext.Output.TagMode);
        }
        public void ExecutionContext_Reinitialize_UpdatesTagHelperContextAsExpected()
        {
            // Arrange
            var         tagName   = "div";
            var         tagMode   = TagMode.StartTagOnly;
            var         items     = new Dictionary <object, object>();
            var         uniqueId  = "some unique id";
            var         callCount = 0;
            Func <Task> executeChildContentAsync = () =>
            {
                callCount++;
                return(Task.FromResult(true));
            };
            Action <HtmlEncoder>    startWritingScope = _ => { };
            Func <TagHelperContent> endWritingScope   = () => null;
            var executionContext = new TagHelperExecutionContext(
                tagName,
                tagMode,
                items,
                uniqueId,
                executeChildContentAsync,
                startWritingScope,
                endWritingScope);
            var updatedItems    = new Dictionary <object, object>();
            var updatedUniqueId = "another unique id";

            executionContext.AddHtmlAttribute(new TagHelperAttribute("something"));

            // Act
            executionContext.Reinitialize(
                tagName,
                tagMode,
                updatedItems,
                updatedUniqueId,
                executeChildContentAsync);
            executionContext.AddHtmlAttribute(new TagHelperAttribute("Another attribute"));

            // Assert
            var context   = executionContext.Context;
            var attribute = Assert.Single(context.AllAttributes);

            Assert.Equal(tagName, context.TagName);
            Assert.Equal("Another attribute", attribute.Name);
            Assert.Equal(updatedUniqueId, context.UniqueId);
            Assert.Same(updatedItems, context.Items);
        }
Exemple #8
0
        public async Task RunAsync_ProcessesAllTagHelpers()
        {
            // Arrange
            var runner               = new TagHelperRunner();
            var executionContext     = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var executableTagHelper1 = new ExecutableTagHelper();
            var executableTagHelper2 = new ExecutableTagHelper();

            // Act
            executionContext.Add(executableTagHelper1);
            executionContext.Add(executableTagHelper2);
            await runner.RunAsync(executionContext);

            // Assert
            Assert.True(executableTagHelper1.Processed);
            Assert.True(executableTagHelper2.Processed);
        }
        public void Add_MaintainsMultipleTagHelpers()
        {
            // Arrange
            var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var tagHelper1       = new PTagHelper();
            var tagHelper2       = new PTagHelper();

            // Act
            executionContext.Add(tagHelper1);
            executionContext.Add(tagHelper2);

            // Assert
            var tagHelpers = executionContext.TagHelpers.ToArray();

            Assert.Equal(2, tagHelpers.Length);
            Assert.Same(tagHelper1, tagHelpers[0]);
            Assert.Same(tagHelper2, tagHelpers[1]);
        }
        public async Task GetChildContentAsync_StartsWritingScopeWithGivenEncoder(HtmlEncoder encoder)
        {
            // Arrange
            HtmlEncoder passedEncoder    = null;
            var         executionContext = new TagHelperExecutionContext(
                "p",
                tagMode: TagMode.StartTagAndEndTag,
                items: new Dictionary <object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () => Task.FromResult(result: true),
                startTagHelperWritingScope: encoderArgument => passedEncoder = encoderArgument,
                endTagHelperWritingScope: () => new DefaultTagHelperContent());

            // Act
            await executionContext.GetChildContentAsync(useCachedResult : true, encoder : encoder);

            // Assert
            Assert.Same(encoder, passedEncoder);
        }
        public async Task GetChildContentAsync_ReturnsNewObjectEveryTimeItIsCalled(bool useCachedResult)
        {
            // Arrange
            var executionContext = new TagHelperExecutionContext(
                "p",
                tagMode: TagMode.StartTagAndEndTag,
                items: new Dictionary <object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () => Task.FromResult(result: true),
                startTagHelperWritingScope: _ => { },
                endTagHelperWritingScope: () => new DefaultTagHelperContent());

            // Act
            var content1 = await executionContext.GetChildContentAsync(useCachedResult, encoder : null);

            var content2 = await executionContext.GetChildContentAsync(useCachedResult, encoder : null);

            // Assert
            Assert.NotSame(content1, content2);
        }
Exemple #12
0
        public async Task RunAsync_AllowsModificationOfTagHelperOutput()
        {
            // Arrange
            var runner              = new TagHelperRunner();
            var executionContext    = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var executableTagHelper = new ExecutableTagHelper();

            // Act
            executionContext.Add(executableTagHelper);
            executionContext.AddHtmlAttribute("class", "btn", HtmlAttributeValueStyle.DoubleQuotes);
            await runner.RunAsync(executionContext);

            // Assert
            var output = executionContext.Output;

            Assert.Equal("foo", output.TagName);
            Assert.Equal("somethingelse", output.Attributes["class"].Value);
            Assert.Equal("world", output.Attributes["hello"].Value);
            Assert.Equal(TagMode.SelfClosing, output.TagMode);
        }
        public void AddHtmlAttribute_MaintainsHtmlAttributes()
        {
            // Arrange
            var executionContext   = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var expectedAttributes = new TagHelperAttributeList
            {
                { "class", "btn" },
                { "foo", "bar" }
            };

            // Act
            executionContext.AddHtmlAttribute("class", "btn");
            executionContext.AddHtmlAttribute("foo", "bar");
            var output = executionContext.Output;

            // Assert
            Assert.Equal(
                expectedAttributes,
                output.Attributes,
                CaseSensitiveTagHelperAttributeComparer.Default);
        }
        public void AddHtmlAttribute_MaintainsMinimizedHtmlAttributes()
        {
            // Arrange
            var executionContext   = new TagHelperExecutionContext("input", tagMode: TagMode.StartTagOnly);
            var expectedAttributes = new TagHelperAttributeList
            {
                new TagHelperAttribute("checked"),
                new TagHelperAttribute("visible"),
            };

            // Act
            executionContext.AddHtmlAttribute(new TagHelperAttribute("checked"));
            executionContext.AddHtmlAttribute(new TagHelperAttribute("visible"));
            var output = executionContext.Output;

            // Assert
            Assert.Equal(
                expectedAttributes,
                output.Attributes,
                CaseSensitiveTagHelperAttributeComparer.Default);
        }
        public void ParentItems_SetsItemsProperty()
        {
            // Arrange
            var expectedItems = new Dictionary <object, object>
            {
                { "test-entry", 1234 }
            };

            // Act
            var executionContext = new TagHelperExecutionContext(
                "p",
                tagMode: TagMode.StartTagAndEndTag,
                items: expectedItems,
                uniqueId: string.Empty,
                executeChildContentAsync: async() => await Task.FromResult(result: true),
                startTagHelperWritingScope: _ => { },
                endTagHelperWritingScope: () => new DefaultTagHelperContent());

            // Assert
            Assert.NotNull(executionContext.Items);
            Assert.Same(expectedItems, executionContext.Items);
        }
        public void AddHtmlAttribute_MaintainsHtmlAttributes()
        {
            // Arrange
            var executionContext   = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var expectedAttributes = new TagHelperAttributeList
            {
                { "class", "btn" },
            };

            expectedAttributes.Add(new TagHelperAttribute("type", "text", HtmlAttributeValueStyle.SingleQuotes));

            // Act
            executionContext.AddHtmlAttribute("class", "btn", HtmlAttributeValueStyle.DoubleQuotes);
            executionContext.AddHtmlAttribute("type", "text", HtmlAttributeValueStyle.SingleQuotes);
            var output = executionContext.Output;

            // Assert
            Assert.Equal(
                expectedAttributes,
                output.Attributes,
                CaseSensitiveTagHelperAttributeComparer.Default);
        }
        public void TagHelperExecutionContext_MaintainsAllAttributes()
        {
            // Arrange
            var executionContext   = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var expectedAttributes = new TagHelperAttributeList
            {
                { "class", "btn" },
                { "something", true },
                { "foo", "bar" }
            };

            // Act
            executionContext.AddHtmlAttribute("class", "btn");
            executionContext.AddTagHelperAttribute("something", true);
            executionContext.AddHtmlAttribute("foo", "bar");
            var context = executionContext.Context;

            // Assert
            Assert.Equal(
                expectedAttributes,
                context.AllAttributes,
                CaseSensitiveTagHelperAttributeComparer.Default);
        }
Exemple #18
0
        /// <summary>
        /// Calls the <see cref="ITagHelper.ProcessAsync"/> method on <see cref="ITagHelper"/>s.
        /// </summary>
        /// <param name="executionContext">Contains information associated with running <see cref="ITagHelper"/>s.
        /// </param>
        /// <returns>Resulting <see cref="TagHelperOutput"/> from processing all of the
        /// <paramref name="executionContext"/>'s <see cref="ITagHelper"/>s.</returns>
        public async Task RunAsync(TagHelperExecutionContext executionContext)
        {
            if (executionContext == null)
            {
                throw new ArgumentNullException(nameof(executionContext));
            }

            var tagHelperContext = executionContext.Context;

            OrderTagHelpers(executionContext.TagHelpers);

            for (var i = 0; i < executionContext.TagHelpers.Count; i++)
            {
                executionContext.TagHelpers[i].Init(tagHelperContext);
            }

            var tagHelperOutput = executionContext.Output;

            for (var i = 0; i < executionContext.TagHelpers.Count; i++)
            {
                await executionContext.TagHelpers[i].ProcessAsync(tagHelperContext, tagHelperOutput);
            }
        }
Exemple #19
0
        /// <summary>
        /// Calls the <see cref="ITagHelperComponent.ProcessAsync"/> method on <see cref="ITagHelper"/>s.
        /// </summary>
        /// <param name="executionContext">Contains information associated with running <see cref="ITagHelper"/>s.
        /// </param>
        /// <returns>Resulting <see cref="TagHelperOutput"/> from processing all of the
        /// <paramref name="executionContext"/>'s <see cref="ITagHelper"/>s.</returns>
        public async Task RunAsync(TagHelperExecutionContext executionContext)
        {
            if (executionContext == null)
            {
                throw new ArgumentNullException(nameof(executionContext));
            }

            var tagHelperContext = executionContext.Context;

            OrderTagHelpers(executionContext.TagHelpers);

            for (var i = 0; i < executionContext.TagHelpers.Count; i++)
            {
                executionContext.TagHelpers[i].Init(tagHelperContext);
            }

            var tagHelperOutput = executionContext.Output;

            for (var i = 0; i < executionContext.TagHelpers.Count; i++)
            {
                await executionContext.TagHelpers[i].ProcessAsync(tagHelperContext, tagHelperOutput);
            }
        }
Exemple #20
0
        public async Task RunAsync_OrdersTagHelpers(
            int[] tagHelperOrders,
            int[] expectedTagHelperOrders)
        {
            // Arrange
            var runner           = new TagHelperRunner();
            var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var processOrder     = new List <int>();

            foreach (var order in tagHelperOrders)
            {
                var orderedTagHelper = new OrderedTagHelper(order)
                {
                    ProcessOrderTracker = processOrder
                };
                executionContext.Add(orderedTagHelper);
            }

            // Act
            await runner.RunAsync(executionContext);

            // Assert
            Assert.Equal(expectedTagHelperOrders, processOrder);
        }
        public async Task Process_AddsHiddenInputTag_FromEndOfFormContent_WithCachedBody(
            List<TagBuilder> tagBuilderList,
            string expectedOutput)
        {
            // Arrange
            var viewContext = new ViewContext();
            var runner = new TagHelperRunner();
            var tagHelperExecutionContext = new TagHelperExecutionContext(
                "form",
                TagMode.StartTagAndEndTag,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () =>
                {
                    foreach (var item in tagBuilderList)
                    {
                        viewContext.FormContext.EndOfFormContent.Add(item);
                    }

                    return Task.FromResult(true);
                },
                startTagHelperWritingScope: _ => { },
                endTagHelperWritingScope: () => new DefaultTagHelperContent());

            // This TagHelper will pre-execute the child content forcing the body to be cached.
            tagHelperExecutionContext.Add(new ChildContentInvoker());
            tagHelperExecutionContext.Add(new RenderAtEndOfFormTagHelper
            {
                ViewContext = viewContext
            });

            // Act
            await runner.RunAsync(tagHelperExecutionContext);

            // Assert
            Assert.Equal(expectedOutput, tagHelperExecutionContext.Output.PostContent.GetContent());
        }
        public void ExecutionContext_Reinitialize_UpdatesTagHelperContextAsExpected()
        {
            // Arrange
            var tagName = "div";
            var tagMode = TagMode.StartTagOnly;
            var items = new Dictionary<object, object>();
            var uniqueId = "some unique id";
            var callCount = 0;
            Func<Task> executeChildContentAsync = () =>
            {
                callCount++;
                return Task.FromResult(true);
            };
            Action<HtmlEncoder> startWritingScope = _ => { };
            Func<TagHelperContent> endWritingScope = () => null;
            var executionContext = new TagHelperExecutionContext(
                tagName,
                tagMode,
                items,
                uniqueId,
                executeChildContentAsync,
                startWritingScope,
                endWritingScope);
            var updatedItems = new Dictionary<object, object>();
            var updatedUniqueId = "another unique id";
            executionContext.AddHtmlAttribute(new TagHelperAttribute("something"));

            // Act
            executionContext.Reinitialize(
                tagName,
                tagMode,
                updatedItems,
                updatedUniqueId,
                executeChildContentAsync);
            executionContext.AddHtmlAttribute(new TagHelperAttribute("Another attribute"));

            // Assert
            var context = executionContext.Context;
            var attribute = Assert.Single(context.AllAttributes);
            Assert.Equal(attribute.Name, "Another attribute");
            Assert.Equal(updatedUniqueId, context.UniqueId);
            Assert.Same(updatedItems, context.Items);
        }
        public async Task GetChildContentAsync_CachesValuePerEncoderInstance(HtmlEncoder encoder)
        {
            // Arrange
            var executionCount = 0;
            var executionContext = new TagHelperExecutionContext(
                "p",
                tagMode: TagMode.StartTagAndEndTag,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () =>
                {
                    executionCount++;
                    return Task.FromResult(result: true);
                },
                startTagHelperWritingScope: _ => { },
                endTagHelperWritingScope: () => new DefaultTagHelperContent());

            // HtmlEncoderData includes another HtmlTestEncoder instance but method compares HtmlEncoder instances.
            var firstEncoder = new HtmlTestEncoder();

            // Act
            var content1 = await executionContext.GetChildContentAsync(useCachedResult: true, encoder: firstEncoder);
            var content2 = await executionContext.GetChildContentAsync(useCachedResult: true, encoder: encoder);

            // Assert
            Assert.Equal(2, executionCount);
        }
Exemple #24
0
        public async Task RunAsync_SetsTagHelperOutputTagMode(TagMode tagMode)
        {
            // Arrange
            var runner = new TagHelperRunner();
            var executionContext = new TagHelperExecutionContext("p", tagMode);
            var tagHelper = new TagHelperContextTouchingTagHelper();

            executionContext.Add(tagHelper);
            executionContext.AddTagHelperAttribute("foo", true, HtmlAttributeValueStyle.DoubleQuotes);

            // Act
            await runner.RunAsync(executionContext);

            // Assert
            Assert.Equal(tagMode, executionContext.Output.TagMode);
        }
Exemple #25
0
        public async Task RunAsync_AllowsModificationOfTagHelperOutput()
        {
            // Arrange
            var runner = new TagHelperRunner();
            var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var executableTagHelper = new ExecutableTagHelper();

            // Act
            executionContext.Add(executableTagHelper);
            executionContext.AddHtmlAttribute("class", "btn", HtmlAttributeValueStyle.DoubleQuotes);
            await runner.RunAsync(executionContext);

            // Assert
            var output = executionContext.Output;
            Assert.Equal("foo", output.TagName);
            Assert.Equal("somethingelse", output.Attributes["class"].Value);
            Assert.Equal("world", output.Attributes["hello"].Value);
            Assert.Equal(TagMode.SelfClosing, output.TagMode);
        }
Exemple #26
0
        public async Task RunAsync_ConfiguresTagHelperContextWithExecutionContextsItems()
        {
            // Arrange
            var runner = new TagHelperRunner();
            var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var tagHelper = new ContextInspectingTagHelper();
            executionContext.Add(tagHelper);

            // Act
            await runner.RunAsync(executionContext);

            // Assert
            Assert.NotNull(tagHelper.ContextProcessedWith);
            Assert.Same(tagHelper.ContextProcessedWith.Items, executionContext.Items);
        }
Exemple #27
0
            public TagHelperExecutionContext Rent(
                string tagName,
                TagMode tagMode,
                IDictionary<object, object> items,
                string uniqueId,
                Func<Task> executeChildContentAsync)
            {
                TagHelperExecutionContext tagHelperExecutionContext;

                if (_nextIndex == _executionContexts.Count)
                {
                    tagHelperExecutionContext = new TagHelperExecutionContext(
                        tagName,
                        tagMode,
                        items,
                        uniqueId,
                        executeChildContentAsync,
                        _startTagHelperWritingScope,
                        _endTagHelperWritingScope);

                    _executionContexts.Add(tagHelperExecutionContext);
                }
                else
                {
                    tagHelperExecutionContext = _executionContexts[_nextIndex];
                    tagHelperExecutionContext.Reinitialize(tagName, tagMode, items, uniqueId, executeChildContentAsync);
                }

                _nextIndex++;

                return tagHelperExecutionContext;
            }
        public void TagHelperExecutionContext_MaintainsAllAttributes()
        {
            // Arrange
            var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var expectedAttributes = new TagHelperAttributeList
            {
                { "class", "btn" },
            };
            expectedAttributes.Add(new TagHelperAttribute("something", true, HtmlAttributeValueStyle.SingleQuotes));
            expectedAttributes.Add(new TagHelperAttribute("type", "text", HtmlAttributeValueStyle.NoQuotes));

            // Act
            executionContext.AddHtmlAttribute("class", "btn", HtmlAttributeValueStyle.DoubleQuotes);
            executionContext.AddTagHelperAttribute("something", true, HtmlAttributeValueStyle.SingleQuotes);
            executionContext.AddHtmlAttribute("type", "text", HtmlAttributeValueStyle.NoQuotes);
            var context = executionContext.Context;

            // Assert
            Assert.Equal(
                expectedAttributes,
                context.AllAttributes,
                CaseSensitiveTagHelperAttributeComparer.Default);
        }
        public void AddHtmlAttribute_MaintainsHtmlAttributes_VariousStyles()
        {
            // Arrange
            var executionContext = new TagHelperExecutionContext("input", tagMode: TagMode.SelfClosing);
            var expectedAttributes = new TagHelperAttributeList
            {
                { "class", "btn" },
                { "foo", "bar" }
            };
            expectedAttributes.Add(new TagHelperAttribute("valid", "true", HtmlAttributeValueStyle.NoQuotes));
            expectedAttributes.Add(new TagHelperAttribute("type", "text", HtmlAttributeValueStyle.SingleQuotes));
            expectedAttributes.Add(new TagHelperAttribute(name: "checked"));
            expectedAttributes.Add(new TagHelperAttribute(name: "visible"));

            // Act
            executionContext.AddHtmlAttribute("class", "btn", HtmlAttributeValueStyle.DoubleQuotes);
            executionContext.AddHtmlAttribute("foo", "bar", HtmlAttributeValueStyle.DoubleQuotes);
            executionContext.AddHtmlAttribute("valid", "true", HtmlAttributeValueStyle.NoQuotes);
            executionContext.AddHtmlAttribute("type", "text", HtmlAttributeValueStyle.SingleQuotes);
            executionContext.AddHtmlAttribute(new TagHelperAttribute("checked"));
            executionContext.AddHtmlAttribute(new TagHelperAttribute("visible"));
            var output = executionContext.Output;

            // Assert
            Assert.Equal(
                expectedAttributes,
                output.Attributes,
                CaseSensitiveTagHelperAttributeComparer.Default);
        }
        public void ParentItems_SetsItemsProperty()
        {
            // Arrange
            var expectedItems = new Dictionary<object, object>
            {
                { "test-entry", 1234 }
            };

            // Act
            var executionContext = new TagHelperExecutionContext(
                "p",
                tagMode: TagMode.StartTagAndEndTag,
                items: expectedItems,
                uniqueId: string.Empty,
                executeChildContentAsync: async () => await Task.FromResult(result: true),
                startTagHelperWritingScope: _ => { },
                endTagHelperWritingScope: () => new DefaultTagHelperContent());

            // Assert
            Assert.NotNull(executionContext.Items);
            Assert.Same(expectedItems, executionContext.Items);
        }
Exemple #31
0
        public void MoveTo_MovesToBuilder(TagHelperOutput output, string expected)
        {
            // Arrange
            var writer = new StringWriter();
            var testEncoder = new HtmlTestEncoder();

            var buffer = new HtmlContentBuilder();

            var tagHelperExecutionContext = new TagHelperExecutionContext(
                tagName: output.TagName,
                tagMode: output.TagMode,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () => Task.FromResult(result: true),
                startTagHelperWritingScope: _ => { },
                endTagHelperWritingScope: () => new DefaultTagHelperContent());
            tagHelperExecutionContext.Output = output;

            // Act
            ((IHtmlContentContainer)output).MoveTo(buffer);

            // Assert
            buffer.WriteTo(writer, testEncoder);

            Assert.Equal(string.Empty, output.PreElement.GetContent());
            Assert.Equal(string.Empty, output.PreContent.GetContent());
            Assert.Equal(string.Empty, output.Content.GetContent());
            Assert.Equal(string.Empty, output.PostContent.GetContent());
            Assert.Equal(string.Empty, output.PostElement.GetContent());
            Assert.Empty(output.Attributes);

            Assert.Equal(expected, writer.ToString(), StringComparer.Ordinal);
        }
        public async Task GetChildContentAsync_ReturnsExpectedContent(HtmlEncoder encoder)
        {
            // Arrange
            var tagHelperContent = new DefaultTagHelperContent();
            var executionCount = 0;
            var content = "Hello from child content";
            var expectedContent = $"HtmlEncode[[{content}]]";
            var executionContext = new TagHelperExecutionContext(
                "p",
                tagMode: TagMode.StartTagAndEndTag,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () =>
                {
                    executionCount++;
                    tagHelperContent.SetContent(content);

                    return Task.FromResult(result: true);
                },
                startTagHelperWritingScope: _ => { },
                endTagHelperWritingScope: () => tagHelperContent);

            // Act
            var actualContent = await executionContext.GetChildContentAsync(useCachedResult: true, encoder: encoder);

            // Assert
            Assert.Equal(expectedContent, actualContent.GetContent(new HtmlTestEncoder()));
        }
        public async Task GetChildContentAsync_CanExecuteChildrenMoreThanOnce(HtmlEncoder encoder)
        {
            // Arrange
            var executionCount = 0;
            var executionContext = new TagHelperExecutionContext(
                "p",
                tagMode: TagMode.StartTagAndEndTag,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () =>
                {
                    executionCount++;
                    return Task.FromResult(result: true);
                },
                startTagHelperWritingScope: _ => { },
                endTagHelperWritingScope: () => new DefaultTagHelperContent());

            // Act
            await executionContext.GetChildContentAsync(useCachedResult: false, encoder: encoder);
            await executionContext.GetChildContentAsync(useCachedResult: false, encoder: encoder);

            // Assert
            Assert.Equal(2, executionCount);
        }
Exemple #34
0
        public async Task RunAsync_OrdersTagHelpers(
            int[] tagHelperOrders,
            int[] expectedTagHelperOrders)
        {
            // Arrange
            var runner = new TagHelperRunner();
            var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var processOrder = new List<int>();

            foreach (var order in tagHelperOrders)
            {
                var orderedTagHelper = new OrderedTagHelper(order)
                {
                    ProcessOrderTracker = processOrder
                };
                executionContext.Add(orderedTagHelper);
            }

            // Act
            await runner.RunAsync(executionContext);

            // Assert
            Assert.Equal(expectedTagHelperOrders, processOrder);
        }
        public void Add_MaintainsTagHelpers()
        {
            // Arrange
            var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var tagHelper = new PTagHelper();

            // Act
            executionContext.Add(tagHelper);

            // Assert
            var singleTagHelper = Assert.Single(executionContext.TagHelpers);
            Assert.Same(tagHelper, singleTagHelper);
        }
        public void Add_MaintainsMultipleTagHelpers()
        {
            // Arrange
            var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var tagHelper1 = new PTagHelper();
            var tagHelper2 = new PTagHelper();

            // Act
            executionContext.Add(tagHelper1);
            executionContext.Add(tagHelper2);

            // Assert
            var tagHelpers = executionContext.TagHelpers.ToArray();
            Assert.Equal(2, tagHelpers.Length);
            Assert.Same(tagHelper1, tagHelpers[0]);
            Assert.Same(tagHelper2, tagHelpers[1]);
        }
Exemple #37
0
 public Task RunAsync(TagHelperExecutionContext executionContext)
 {
     throw null;
 }
Exemple #38
0
        public void AddHtmlAttributeValues_AddsToHtmlAttributesAsExpected(
            Tuple<string, int, object, int, bool>[] attributeValues,
            string expectedValue)
        {
            // Arrange
            var page = CreatePage(p => { });
            page.HtmlEncoder = new HtmlTestEncoder();
            var executionContext = new TagHelperExecutionContext(
                "p",
                tagMode: TagMode.StartTagAndEndTag,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () => Task.FromResult(result: true),
                startTagHelperWritingScope: _ => { },
                endTagHelperWritingScope: () => new DefaultTagHelperContent());

            // Act
            page.BeginAddHtmlAttributeValues(executionContext, "someattr", attributeValues.Length, HtmlAttributeValueStyle.SingleQuotes);
            foreach (var value in attributeValues)
            {
                page.AddHtmlAttributeValue(value.Item1, value.Item2, value.Item3, value.Item4, 0, value.Item5);
            }
            page.EndAddHtmlAttributeValues(executionContext);

            // Assert
            var output = executionContext.Output;
            var htmlAttribute = Assert.Single(output.Attributes);
            Assert.Equal("someattr", htmlAttribute.Name, StringComparer.Ordinal);
            var htmlContent = Assert.IsAssignableFrom<IHtmlContent>(htmlAttribute.Value);
            Assert.Equal(expectedValue, HtmlContentUtilities.HtmlContentToString(htmlContent), StringComparer.Ordinal);
            Assert.Equal(HtmlAttributeValueStyle.SingleQuotes, htmlAttribute.ValueStyle);

            var context = executionContext.Context;
            var allAttribute = Assert.Single(context.AllAttributes);
            Assert.Equal("someattr", allAttribute.Name, StringComparer.Ordinal);
            htmlContent = Assert.IsAssignableFrom<IHtmlContent>(allAttribute.Value);
            Assert.Equal(expectedValue, HtmlContentUtilities.HtmlContentToString(htmlContent), StringComparer.Ordinal);
            Assert.Equal(HtmlAttributeValueStyle.SingleQuotes, allAttribute.ValueStyle);
        }
Exemple #39
0
        public async Task RunAsync_AllowsDataRetrievalFromTagHelperContext()
        {
            // Arrange
            var runner = new TagHelperRunner();
            var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var tagHelper = new TagHelperContextTouchingTagHelper();

            // Act
            executionContext.Add(tagHelper);
            executionContext.AddTagHelperAttribute("foo", true, HtmlAttributeValueStyle.DoubleQuotes);
            await runner.RunAsync(executionContext);

            // Assert
            Assert.Equal("True", executionContext.Output.Attributes["foo"].Value);
        }
Exemple #40
0
        public void AddHtmlAttributeValues_AddsAttributeNameAsValueWhenValueIsUnprefixedTrue()
        {
            // Arrange
            var page = CreatePage(p => { });
            page.HtmlEncoder = new HtmlTestEncoder();
            var executionContext = new TagHelperExecutionContext(
                "p",
                tagMode: TagMode.StartTagAndEndTag,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () => Task.FromResult(result: true),
                startTagHelperWritingScope: _ => { },
                endTagHelperWritingScope: () => new DefaultTagHelperContent());

            // Act
            page.BeginAddHtmlAttributeValues(executionContext, "someattr", 1, HtmlAttributeValueStyle.NoQuotes);
            page.AddHtmlAttributeValue(string.Empty, 9, true, 9, valueLength: 0, isLiteral: false);
            page.EndAddHtmlAttributeValues(executionContext);

            // Assert
            var output = executionContext.Output;
            var htmlAttribute = Assert.Single(output.Attributes);
            Assert.Equal("someattr", htmlAttribute.Name, StringComparer.Ordinal);
            Assert.Equal("someattr", (string)htmlAttribute.Value, StringComparer.Ordinal);
            Assert.Equal(HtmlAttributeValueStyle.NoQuotes, htmlAttribute.ValueStyle);
            var context = executionContext.Context;
            var allAttribute = Assert.Single(context.AllAttributes);
            Assert.Equal("someattr", allAttribute.Name, StringComparer.Ordinal);
            Assert.Equal("someattr", (string)allAttribute.Value, StringComparer.Ordinal);
            Assert.Equal(HtmlAttributeValueStyle.NoQuotes, allAttribute.ValueStyle);
        }
Exemple #41
0
        public async Task RunAsync_ProcessesAllTagHelpers()
        {
            // Arrange
            var runner = new TagHelperRunner();
            var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var executableTagHelper1 = new ExecutableTagHelper();
            var executableTagHelper2 = new ExecutableTagHelper();

            // Act
            executionContext.Add(executableTagHelper1);
            executionContext.Add(executableTagHelper2);
            await runner.RunAsync(executionContext);

            // Assert
            Assert.True(executableTagHelper1.Processed);
            Assert.True(executableTagHelper2.Processed);
        }
        public void ExecutionContext_CreateTagHelperOutput_ReturnsExpectedTagMode(TagMode tagMode)
        {
            // Arrange
            var executionContext = new TagHelperExecutionContext("p", tagMode);

            // Act
            var output = executionContext.Output;

            // Assert
            Assert.Equal(tagMode, output.TagMode);
        }
        public async Task GetChildContentAsync_ReturnsNewObjectEveryTimeItIsCalled(bool useCachedResult)
        {
            // Arrange
            var executionContext = new TagHelperExecutionContext(
                "p",
                tagMode: TagMode.StartTagAndEndTag,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () => Task.FromResult(result: true),
                startTagHelperWritingScope: _ => { },
                endTagHelperWritingScope: () => new DefaultTagHelperContent());

            // Act
            var content1 = await executionContext.GetChildContentAsync(useCachedResult, encoder: null);
            var content2 = await executionContext.GetChildContentAsync(useCachedResult, encoder: null);

            // Assert
            Assert.NotSame(content1, content2);
        }
        public async Task GetChildContentAsync_StartsWritingScopeWithGivenEncoder(HtmlEncoder encoder)
        {
            // Arrange
            HtmlEncoder passedEncoder = null;
            var executionContext = new TagHelperExecutionContext(
                "p",
                tagMode: TagMode.StartTagAndEndTag,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () => Task.FromResult(result: true),
                startTagHelperWritingScope: encoderArgument => passedEncoder = encoderArgument,
                endTagHelperWritingScope: () => new DefaultTagHelperContent());

            // Act
            await executionContext.GetChildContentAsync(useCachedResult: true, encoder: encoder);

            // Assert
            Assert.Same(encoder, passedEncoder);
        }
        public void AddHtmlAttribute_MaintainsMinimizedHtmlAttributes()
        {
            // Arrange
            var executionContext = new TagHelperExecutionContext("input", tagMode: TagMode.StartTagOnly);
            var expectedAttributes = new TagHelperAttributeList
            {
                new TagHelperAttribute("checked"),
                new TagHelperAttribute("visible"),
            };

            // Act
            executionContext.AddHtmlAttribute(new TagHelperAttribute("checked"));
            executionContext.AddHtmlAttribute(new TagHelperAttribute("visible"));
            var output = executionContext.Output;

            // Assert
            Assert.Equal(
                expectedAttributes,
                output.Attributes,
                CaseSensitiveTagHelperAttributeComparer.Default);
        }
Exemple #46
0
        public void WriteTo_WritesFormattedTagHelper(TagHelperOutput output, string expected)
        {
            // Arrange
            var writer = new StringWriter();
            var tagHelperExecutionContext = new TagHelperExecutionContext(
                tagName: output.TagName,
                tagMode: output.TagMode,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () => Task.FromResult(result: true),
                startTagHelperWritingScope: _ => { },
                endTagHelperWritingScope: () => new DefaultTagHelperContent());
            tagHelperExecutionContext.Output = output;
            var testEncoder = new HtmlTestEncoder();

            // Act
            output.WriteTo(writer, testEncoder);

            // Assert
            Assert.Equal(expected, writer.ToString(), StringComparer.Ordinal);
        }