public void CopyConstructorCopiesValuesAsExpected(IReadOnlyTagHelperAttribute readOnlyTagHelperAttribute)
        {
            // Act
            var tagHelperAttribute = new TagHelperAttribute(readOnlyTagHelperAttribute);

            // Assert
            Assert.Equal(
                readOnlyTagHelperAttribute,
                tagHelperAttribute,
                CaseSensitiveTagHelperAttributeComparer.Default);
        }
        public void IntIndexer_GetsExpectedAttribute(
            IEnumerable<TagHelperAttribute> initialAttributes,
            int indexToLookup,
            TagHelperAttribute expectedAttribute)
        {
            // Arrange
            var attributes = new TagHelperAttributeList(initialAttributes);

            // Act
            var attribute = attributes[indexToLookup];

            // Assert
            Assert.Equal(expectedAttribute, attribute, CaseSensitiveTagHelperAttributeComparer.Default);
        }
        public void IntIndexer_SetsAttributeAtExpectedIndex(
            IEnumerable<TagHelperAttribute> initialAttributes,
            int indexToSet,
            TagHelperAttribute setValue,
            IEnumerable<TagHelperAttribute> expectedAttributes)
        {
            // Arrange
            var attributes = new TagHelperAttributeList(initialAttributes);

            // Act
            attributes[indexToSet] = setValue;

            // Assert
            Assert.Equal(expectedAttributes, attributes, CaseSensitiveTagHelperAttributeComparer.Default);
        }
        public void CopyTo_CopiesAttributes(
            IEnumerable <TagHelperAttribute> initialAttributes,
            TagHelperAttribute[] attributesToCopy,
            int locationToCopy,
            IEnumerable <TagHelperAttribute> expectedAttributes)
        {
            // Arrange
            var attributes           = new TagHelperAttributeList(initialAttributes);
            var attributeDestination = new TagHelperAttribute[expectedAttributes.Count()];

            attributes.ToArray().CopyTo(attributeDestination, 0);

            // Act
            attributesToCopy.CopyTo(attributeDestination, locationToCopy);

            // Assert
            Assert.Equal(expectedAttributes, attributeDestination, CaseSensitiveTagHelperAttributeComparer.Default);
        }
Example #5
0
        public async Task ProcessAsync_BindsRouteValues()
        {
            // Arrange
            var testViewContext = CreateViewContext();
            var context = new TagHelperContext(
                allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
                    Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
                items: new Dictionary<object, object>(),
                uniqueId: "test",
                getChildContentAsync: () =>
                {
                    var tagHelperContent = new DefaultTagHelperContent();
                    tagHelperContent.SetContent("Something");
                    return Task.FromResult<TagHelperContent>(tagHelperContent);
                });
            var expectedAttribute = new TagHelperAttribute("asp-ROUTEE-NotRoute", "something");
            var output = new TagHelperOutput(
                "form",
                attributes: new TagHelperAttributeList());
            output.Attributes.Add(expectedAttribute);

            var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
            generator
                .Setup(mock => mock.GenerateForm(
                    It.IsAny<ViewContext>(),
                    It.IsAny<string>(),
                    It.IsAny<string>(),
                    It.IsAny<object>(),
                    It.IsAny<string>(),
                    It.IsAny<object>()))
                .Callback<ViewContext, string, string, object, string, object>(
                    (viewContext, actionName, controllerName, routeValues, method, htmlAttributes) =>
                    {
                        // Fixes Roslyn bug with lambdas
                        generator.ToString();

                        var routeValueDictionary = (Dictionary<string, object>)routeValues;

                        Assert.Equal(2, routeValueDictionary.Count);
                        var routeValue = Assert.Single(routeValueDictionary, attr => attr.Key.Equals("val"));
                        Assert.Equal("hello", routeValue.Value);
                        routeValue = Assert.Single(routeValueDictionary, attr => attr.Key.Equals("-Name"));
                        Assert.Equal("Value", routeValue.Value);
                    })
                .Returns(new TagBuilder("form", new CommonTestEncoder()))
                .Verifiable();
            var formTagHelper = new FormTagHelper(generator.Object)
            {
                Action = "Index",
                Antiforgery = false,
                ViewContext = testViewContext,
                RouteValues =
                {
                    { "val", "hello" },
                    { "-Name", "Value" },
                },
            };

            // Act & Assert
            await formTagHelper.ProcessAsync(context, output);

            Assert.Equal("form", output.TagName);
            Assert.False(output.SelfClosing);
            var attribute = Assert.Single(output.Attributes);
            Assert.Equal(expectedAttribute, attribute);
            Assert.Empty(output.PreContent.GetContent());
            Assert.True(output.Content.IsEmpty);
            Assert.Empty(output.PostContent.GetContent());
            generator.Verify();
        }
        private static void CopyHtmlAttribute(
            int allAttributeIndex,
            TagHelperOutput tagHelperOutput,
            TagHelperContext context)
        {
            var existingAttribute = context.AllAttributes[allAttributeIndex];
            var copiedAttribute = new TagHelperAttribute
            {
                Name = existingAttribute.Name,
                Value = existingAttribute.Value,
                Minimized = existingAttribute.Minimized
            };

            // Move backwards through context.AllAttributes from the provided index until we find a familiar attribute
            // in tagHelperOutput where we can insert the copied value after the familiar one.
            for (var i = allAttributeIndex - 1; i >= 0; i--)
            {
                var previousName = context.AllAttributes[i].Name;
                var index = IndexOfFirstMatch(previousName, tagHelperOutput.Attributes);
                if (index != -1)
                {
                    tagHelperOutput.Attributes.Insert(index + 1, copiedAttribute);
                    return;
                }
            }

            // Move forward through context.AllAttributes from the provided index until we find a familiar attribute in
            // tagHelperOutput where we can insert the copied value.
            for (var i = allAttributeIndex + 1; i < context.AllAttributes.Count; i++)
            {
                var nextName = context.AllAttributes[i].Name;
                var index = IndexOfFirstMatch(nextName, tagHelperOutput.Attributes);
                if (index != -1)
                {
                    tagHelperOutput.Attributes.Insert(index, copiedAttribute);
                    return;
                }
            }

            // Couldn't determine the attribute's location, add it to the end.
            tagHelperOutput.Attributes.Add(copiedAttribute);
        }
        public void IntIndexer_Setter_ThrowsIfIndexInvalid(int index)
        {
            // Arrange
            var attributes = new TagHelperAttributeList(new[]
            {
                new TagHelperAttribute("A", "AV"),
                new TagHelperAttribute("B", "BV")
            });

            // Act & Assert
            var exception = Assert.Throws<ArgumentOutOfRangeException>("index", () =>
            {
                attributes[index] = new TagHelperAttribute("C", "CV");
            });
        }
        public void Remove_ReturnsExpectedValueAndRemovesFirstAttribute(
            IEnumerable<TagHelperAttribute> initialAttributes,
            TagHelperAttribute attributeToRemove,
            IEnumerable<TagHelperAttribute> expectedAttributes,
            bool expectedResult)
        {
            // Arrange
            var attributes = new TagHelperAttributeList(initialAttributes);

            // Act
            var result = attributes.Remove(attributeToRemove);

            // Assert
            Assert.Equal(expectedResult, result);
            Assert.Equal(expectedAttributes, attributes, CaseSensitiveTagHelperAttributeComparer.Default);
        }
        public void CopyTo_CopiesAttributes(
            IEnumerable<TagHelperAttribute> initialAttributes,
            TagHelperAttribute[] attributesToCopy,
            int locationToCopy,
            IEnumerable<TagHelperAttribute> expectedAttributes)
        {
            // Arrange
            var attributes = new TagHelperAttributeList(initialAttributes);
            var attributeDestination = new TagHelperAttribute[expectedAttributes.Count()];
            attributes.ToArray().CopyTo(attributeDestination, 0);

            // Act
            attributesToCopy.CopyTo(attributeDestination, locationToCopy);

            // Assert
            Assert.Equal(expectedAttributes, attributeDestination, CaseSensitiveTagHelperAttributeComparer.Default);
        }
        public void Insert_InsertsAttributes(
            IEnumerable<TagHelperAttribute> initialAttributes,
            TagHelperAttribute attributeToAdd,
            int locationToInsert,
            IEnumerable<TagHelperAttribute> expectedAttributes)
        {
            // Arrange
            var attributes = new TagHelperAttributeList(initialAttributes);

            // Act
            attributes.Insert(locationToInsert, attributeToAdd);

            // Assert
            Assert.Equal(expectedAttributes, attributes, CaseSensitiveTagHelperAttributeComparer.Default);
        }
        public void Add_AppendsAttributes(
            IEnumerable<TagHelperAttribute> initialAttributes,
            TagHelperAttribute attributeToAdd,
            IEnumerable<TagHelperAttribute> expectedAttributes)
        {
            // Arrange
            var attributes = new TagHelperAttributeList(initialAttributes);

            // Act
            attributes.Add(attributeToAdd);

            // Assert
            Assert.Equal(expectedAttributes, attributes, CaseSensitiveTagHelperAttributeComparer.Default);
        }
        public void StringIndexer_Setter_ThrowsIfUnmatchingKey(
            TagHelperAttributeList attributes)
        {
            // Arrange
            var expectedMessage = $"Cannot add a {nameof(TagHelperAttribute)} with inconsistent names. The " +
                $"{nameof(TagHelperAttribute.Name)} property 'somethingelse' must match the location 'something'." +
                $"{Environment.NewLine}Parameter name: name";

            // Act & Assert
            var exception = Assert.Throws<ArgumentException>("name", () =>
            {
                attributes["something"] = new TagHelperAttribute("somethingelse", "value");
            });
            Assert.Equal(expectedMessage, exception.Message);
        }