コード例 #1
0
        public async Task ProcessAsync_CallsIntoGenerateValidationMessageWithExpectedParameters()
        {
            // Arrange
            var validationMessageTagHelper = new ValidationMessageTagHelper
            {
                For = CreateModelExpression("Hello")
            };
            var output = new TagHelperOutput(
                "span",
                attributes: new Dictionary <string, string>(),
                content: "Content of validation message");
            var expectedViewContext = CreateViewContext();
            var generator           = new Mock <IHtmlGenerator>();

            generator
            .Setup(mock =>
                   mock.GenerateValidationMessage(expectedViewContext, "Hello", null, null, null))
            .Returns(new TagBuilder("span"))
            .Verifiable();
            validationMessageTagHelper.Generator   = generator.Object;
            validationMessageTagHelper.ViewContext = expectedViewContext;

            // Act & Assert
            await validationMessageTagHelper.ProcessAsync(context : null, output : output);

            generator.Verify();
            Assert.Equal("span", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal("Content of validation message", output.Content);
        }
コード例 #2
0
        public async Task ProcessAsync_DoesNothingIfNullFor()
        {
            // Arrange
            var generator = new Mock <IHtmlGenerator>(MockBehavior.Strict);
            var validationMessageTagHelper = new ValidationMessageTagHelper(generator.Object);
            var expectedPreContent         = "original pre-content";
            var expectedContent            = "original content";
            var expectedPostContent        = "original post-content";
            var output = new TagHelperOutput(
                "span",
                attributes: new TagHelperAttributeList());

            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);

            var viewContext = CreateViewContext();

            validationMessageTagHelper.ViewContext = viewContext;

            // Act
            await validationMessageTagHelper.ProcessAsync(context : null, output : output);

            // Assert
            Assert.Equal("span", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
        }
コード例 #3
0
        public async Task ProcessAsync_DoesNotOverrideOutputContent(
            string childContent, string outputContent, string expectedOutputContent)
        {
            // Arrange
            var tagBuilder = new TagBuilder("span2");

            tagBuilder.InnerHtml.SetContentEncoded("New HTML");
            tagBuilder.Attributes.Add("data-foo", "bar");
            tagBuilder.Attributes.Add("data-hello", "world");

            var generator = new Mock <IHtmlGenerator>(MockBehavior.Strict);
            var setup     = generator
                            .Setup(mock => mock.GenerateValidationMessage(
                                       It.IsAny <ViewContext>(),
                                       It.IsAny <string>(),
                                       It.IsAny <string>(),
                                       It.IsAny <string>(),
                                       It.IsAny <object>()))
                            .Returns(tagBuilder);

            var validationMessageTagHelper = new ValidationMessageTagHelper(generator.Object)
            {
                For = CreateModelExpression("Hello")
            };
            var output = new TagHelperOutput(
                "span",
                attributes: new TagHelperAttributeList());

            output.Content.AppendEncoded(outputContent);

            var context = new TagHelperContext(
                allAttributes: new ReadOnlyTagHelperAttributeList <IReadOnlyTagHelperAttribute>(
                    Enumerable.Empty <IReadOnlyTagHelperAttribute>()),
                items: new Dictionary <object, object>(),
                uniqueId: "test",
                getChildContentAsync: useCachedResult =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.AppendEncoded(childContent);
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var viewContext = CreateViewContext();

            validationMessageTagHelper.ViewContext = viewContext;

            // Act
            await validationMessageTagHelper.ProcessAsync(context, output : output);

            // Assert
            Assert.Equal("span", output.TagName);
            Assert.Equal(2, output.Attributes.Count);
            var attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-foo"));

            Assert.Equal("bar", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-hello"));
            Assert.Equal("world", attribute.Value);
            Assert.Equal(expectedOutputContent, output.Content.GetContent());
        }
コード例 #4
0
        public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationMessage(
            string childContent, string expectedOutputContent)
        {
            // Arrange
            var validationMessageTagHelper = new ValidationMessageTagHelper
            {
                For = CreateModelExpression("Hello")
            };
            var output = new TagHelperOutput(
                "span",
                attributes: new Dictionary <string, object>());

            var context = new TagHelperContext(
                allAttributes: new Dictionary <string, object>(),
                items: new Dictionary <object, object>(),
                uniqueId: "test",
                getChildContentAsync: () =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent(childContent);
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });
            var tagBuilder = new TagBuilder("span2", new HtmlEncoder())
            {
                InnerHtml = "New HTML"
            };

            tagBuilder.Attributes.Add("data-foo", "bar");
            tagBuilder.Attributes.Add("data-hello", "world");

            var generator = new Mock <IHtmlGenerator>(MockBehavior.Strict);
            var setup     = generator
                            .Setup(mock => mock.GenerateValidationMessage(
                                       It.IsAny <ViewContext>(),
                                       It.IsAny <string>(),
                                       It.IsAny <string>(),
                                       It.IsAny <string>(),
                                       It.IsAny <object>()))
                            .Returns(tagBuilder);
            var viewContext = CreateViewContext();

            validationMessageTagHelper.ViewContext = viewContext;
            validationMessageTagHelper.Generator   = generator.Object;

            // Act
            await validationMessageTagHelper.ProcessAsync(context, output : output);

            // Assert
            Assert.Equal("span", output.TagName);
            Assert.Equal(2, output.Attributes.Count);
            var attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("data-foo"));

            Assert.Equal("bar", attribute.Value);
            attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("data-hello"));
            Assert.Equal("world", attribute.Value);
            Assert.Equal(expectedOutputContent, output.Content.GetContent());
        }
コード例 #5
0
        public async Task ProcessAsync_CallsIntoGenerateValidationMessageWithExpectedParameters()
        {
            // Arrange
            var expectedViewContext = CreateViewContext();
            var generator           = new Mock <IHtmlGenerator>();

            generator
            .Setup(mock =>
                   mock.GenerateValidationMessage(expectedViewContext, "Hello", null, null, null))
            .Returns(new TagBuilder("span"))
            .Verifiable();

            var validationMessageTagHelper = new ValidationMessageTagHelper(generator.Object)
            {
                For = CreateModelExpression("Hello")
            };
            var expectedPreContent  = "original pre-content";
            var expectedContent     = "original content";
            var expectedPostContent = "original post-content";
            var context             = new TagHelperContext(
                allAttributes: new ReadOnlyTagHelperAttributeList <IReadOnlyTagHelperAttribute>(
                    Enumerable.Empty <IReadOnlyTagHelperAttribute>()),
                items: new Dictionary <object, object>(),
                uniqueId: "test",
                getChildContentAsync: useCachedResult =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("Something");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });
            var output = new TagHelperOutput(
                "span",
                attributes: new TagHelperAttributeList());

            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);

            validationMessageTagHelper.ViewContext = expectedViewContext;

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

            generator.Verify();
            Assert.Equal("span", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
        }
コード例 #6
0
        public async Task ProcessAsync_GeneratesExpectedOutput()
        {
            // Arrange
            var expectedTagName            = "not-span";
            var metadataProvider           = new DataAnnotationsModelMetadataProvider();
            var modelExpression            = CreateModelExpression("Name");
            var validationMessageTagHelper = new ValidationMessageTagHelper
            {
                For = modelExpression
            };

            var tagHelperContext = new TagHelperContext(
                allAttributes: new Dictionary <string, object>
            {
                { "id", "myvalidationmessage" },
                { "for", modelExpression },
            });
            var output = new TagHelperOutput(
                expectedTagName,
                attributes: new Dictionary <string, string>
            {
                { "id", "myvalidationmessage" }
            },
                content: "Something");
            var htmlGenerator = new TestableHtmlGenerator(metadataProvider);
            var viewContext   = TestableHtmlGenerator.GetViewContext(model: null,
                                                                     htmlGenerator: htmlGenerator,
                                                                     metadataProvider: metadataProvider);

            validationMessageTagHelper.ViewContext = viewContext;
            validationMessageTagHelper.Generator   = htmlGenerator;

            // Act
            await validationMessageTagHelper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal(4, output.Attributes.Count);
            var attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("id"));

            Assert.Equal("myvalidationmessage", attribute.Value);
            attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("class"));
            Assert.Equal("field-validation-valid", attribute.Value);
            attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("data-valmsg-for"));
            Assert.Equal("Name", attribute.Value);
            attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("data-valmsg-replace"));
            Assert.Equal("true", attribute.Value);
            Assert.Equal("Something", output.Content);
            Assert.Equal(expectedTagName, output.TagName);
        }
コード例 #7
0
        public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationMessage(
            string outputContent, string expectedOutputContent)
        {
            // Arrange
            var validationMessageTagHelper = new ValidationMessageTagHelper
            {
                For = CreateModelExpression("Hello")
            };
            var output = new TagHelperOutput(
                "span",
                attributes: new Dictionary <string, string>(),
                content: outputContent);
            var tagBuilder = new TagBuilder("span2")
            {
                InnerHtml = "New HTML"
            };

            tagBuilder.Attributes.Add("data-foo", "bar");
            tagBuilder.Attributes.Add("data-hello", "world");

            var generator = new Mock <IHtmlGenerator>(MockBehavior.Strict);
            var setup     = generator
                            .Setup(mock => mock.GenerateValidationMessage(
                                       It.IsAny <ViewContext>(),
                                       It.IsAny <string>(),
                                       It.IsAny <string>(),
                                       It.IsAny <string>(),
                                       It.IsAny <object>()))
                            .Returns(tagBuilder);
            var viewContext = CreateViewContext();

            validationMessageTagHelper.ViewContext = viewContext;
            validationMessageTagHelper.Generator   = generator.Object;

            // Act
            await validationMessageTagHelper.ProcessAsync(context : null, output : output);

            // Assert
            Assert.Equal(output.TagName, "span");
            Assert.Equal(2, output.Attributes.Count);
            var attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("data-foo"));

            Assert.Equal("bar", attribute.Value);
            attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("data-hello"));
            Assert.Equal("world", attribute.Value);
            Assert.Equal(expectedOutputContent, output.Content);
        }
コード例 #8
0
        public async Task ProcessAsync_CallsIntoGenerateValidationMessageWithExpectedParameters()
        {
            // Arrange
            var validationMessageTagHelper = new ValidationMessageTagHelper
            {
                For = CreateModelExpression("Hello")
            };
            var expectedPreContent  = "original pre-content";
            var expectedContent     = "original content";
            var expectedPostContent = "original post-content";
            var context             = new TagHelperContext(
                allAttributes: new Dictionary <string, object>(),
                uniqueId: "test",
                getChildContentAsync: () => Task.FromResult("Something"));
            var output = new TagHelperOutput(
                "span",
                attributes: new Dictionary <string, string>())
            {
                PreContent  = expectedPreContent,
                Content     = expectedContent,
                PostContent = expectedPostContent,
            };
            var expectedViewContext = CreateViewContext();
            var generator           = new Mock <IHtmlGenerator>();

            generator
            .Setup(mock =>
                   mock.GenerateValidationMessage(expectedViewContext, "Hello", null, null, null))
            .Returns(new TagBuilder("span"))
            .Verifiable();
            validationMessageTagHelper.Generator   = generator.Object;
            validationMessageTagHelper.ViewContext = expectedViewContext;

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

            generator.Verify();
            Assert.Equal("span", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent);
            Assert.Equal(expectedContent, output.Content);
            Assert.Equal(expectedPostContent, output.PostContent);
        }
コード例 #9
0
        public async Task ProcessAsync_DoesNothingIfNullFor()
        {
            // Arrange
            var validationMessageTagHelper = new ValidationMessageTagHelper();
            var output = new TagHelperOutput(
                "span",
                attributes: new Dictionary <string, string>(),
                content: "Content of validation message");
            var viewContext = CreateViewContext();
            var generator   = new Mock <IHtmlGenerator>(MockBehavior.Strict);

            validationMessageTagHelper.ViewContext = viewContext;
            validationMessageTagHelper.Generator   = generator.Object;

            // Act
            await validationMessageTagHelper.ProcessAsync(context : null, output : output);

            // Assert
            Assert.Equal("span", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal("Content of validation message", output.Content);
        }
コード例 #10
0
        public async Task ProcessAsync_DoesNothingIfNullFor()
        {
            // Arrange
            var generator = new Mock <IHtmlGenerator>(MockBehavior.Strict);
            var validationMessageTagHelper = new ValidationMessageTagHelper(generator.Object);
            var expectedPreContent         = "original pre-content";
            var expectedContent            = "original content";
            var expectedPostContent        = "original post-content";
            var output = new TagHelperOutput(
                tagName: "span",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (_) => Task.FromResult <TagHelperContent>(new DefaultTagHelperContent()));

            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);

            var context = new TagHelperContext(
                allAttributes: new ReadOnlyTagHelperAttributeList <IReadOnlyTagHelperAttribute>(
                    Enumerable.Empty <IReadOnlyTagHelperAttribute>()),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var viewContext = CreateViewContext();

            validationMessageTagHelper.ViewContext = viewContext;

            // Act
            await validationMessageTagHelper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("span", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
        }
コード例 #11
0
        public async Task ProcessAsync_CallsIntoGenerateValidationMessageWithExpectedParameters()
        {
            // Arrange
            var expectedViewContext = CreateViewContext();
            var generator = new Mock<IHtmlGenerator>();
            generator
                .Setup(mock =>
                    mock.GenerateValidationMessage(expectedViewContext, "Hello", null, null, null))
                .Returns(new TagBuilder("span", new CommonTestEncoder()))
                .Verifiable();

            var validationMessageTagHelper = new ValidationMessageTagHelper(generator.Object)
            {
                For = CreateModelExpression("Hello")
            };
            var expectedPreContent = "original pre-content";
            var expectedContent = "original content";
            var expectedPostContent = "original post-content";
            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 output = new TagHelperOutput(
                "span",
                attributes: new TagHelperAttributeList());
            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);

            validationMessageTagHelper.ViewContext = expectedViewContext;

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

            generator.Verify();
            Assert.Equal("span", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
        }
コード例 #12
0
        public async Task ProcessAsync_DoesNothingIfNullFor()
        {
            // Arrange
            var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
            var validationMessageTagHelper = new ValidationMessageTagHelper(generator.Object);
            var expectedPreContent = "original pre-content";
            var expectedContent = "original content";
            var expectedPostContent = "original post-content";
            var output = new TagHelperOutput(
                "span",
                attributes: new TagHelperAttributeList());
            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);

            var viewContext = CreateViewContext();
            validationMessageTagHelper.ViewContext = viewContext;

            // Act
            await validationMessageTagHelper.ProcessAsync(context: null, output: output);

            // Assert
            Assert.Equal("span", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
        }
コード例 #13
0
        public async Task ProcessAsync_GeneratesExpectedOutput()
        {
            // Arrange
            var expectedTagName = "not-span";
            var metadataProvider = new TestModelMetadataProvider();
            var modelExpression = CreateModelExpression("Name");
            var htmlGenerator = new TestableHtmlGenerator(metadataProvider);

            var validationMessageTagHelper = new ValidationMessageTagHelper(htmlGenerator)
            {
                For = modelExpression
            };

            var expectedPreContent = "original pre-content";
            var expectedContent = "original content";
            var expectedPostContent = "original post-content";

            var tagHelperContext = new TagHelperContext(
                allAttributes: new TagHelperAttributeList
                {
                    { "id", "myvalidationmessage" },
                    { "for", modelExpression },
                },
                items: new Dictionary<object, object>(),
                uniqueId: "test",
                getChildContentAsync: () =>
                {
                    var tagHelperContent = new DefaultTagHelperContent();
                    tagHelperContent.SetContent("Something");
                    return Task.FromResult<TagHelperContent>(tagHelperContent);
                });
            var output = new TagHelperOutput(
                expectedTagName,
                attributes: new TagHelperAttributeList
                {
                    { "id", "myvalidationmessage" }
                });
            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);
            
            var viewContext = TestableHtmlGenerator.GetViewContext(model: null,
                                                                   htmlGenerator: htmlGenerator,
                                                                   metadataProvider: metadataProvider);
            validationMessageTagHelper.ViewContext = viewContext;

            // Act
            await validationMessageTagHelper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal(4, output.Attributes.Count);
            var attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("id"));
            Assert.Equal("myvalidationmessage", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("class"));
            Assert.Equal("field-validation-valid", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-valmsg-for"));
            Assert.Equal("Name", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-valmsg-replace"));
            Assert.Equal("true", attribute.Value);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
            Assert.Equal(expectedTagName, output.TagName);
        }
コード例 #14
0
        public async Task ProcessAsync_MergesTagBuilderFromGenerateValidationMessage(
            string childContent, string expectedOutputContent)
        {
            // Arrange
            var tagBuilder = new TagBuilder("span2", new CommonTestEncoder())
            {
                InnerHtml = "New HTML"
            };
            tagBuilder.Attributes.Add("data-foo", "bar");
            tagBuilder.Attributes.Add("data-hello", "world");

            var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
            var setup = generator
                .Setup(mock => mock.GenerateValidationMessage(
                    It.IsAny<ViewContext>(),
                    It.IsAny<string>(),
                    It.IsAny<string>(),
                    It.IsAny<string>(),
                    It.IsAny<object>()))
                .Returns(tagBuilder);

            var validationMessageTagHelper = new ValidationMessageTagHelper(generator.Object)
            {
                For = CreateModelExpression("Hello")
            };
            var output = new TagHelperOutput(
                "span",
                attributes: new TagHelperAttributeList());

            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(childContent);
                    return Task.FromResult<TagHelperContent>(tagHelperContent);
                });

            var viewContext = CreateViewContext();
            validationMessageTagHelper.ViewContext = viewContext;

            // Act
            await validationMessageTagHelper.ProcessAsync(context, output: output);

            // Assert
            Assert.Equal("span", output.TagName);
            Assert.Equal(2, output.Attributes.Count);
            var attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-foo"));
            Assert.Equal("bar", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-hello"));
            Assert.Equal("world", attribute.Value);
            Assert.Equal(expectedOutputContent, output.Content.GetContent());
        }
コード例 #15
0
        public async Task ProcessAsync_GeneratesExpectedOutput()
        {
            // Arrange
            var expectedTagName  = "not-span";
            var metadataProvider = new TestModelMetadataProvider();
            var modelExpression  = CreateModelExpression("Name");
            var htmlGenerator    = new TestableHtmlGenerator(metadataProvider);

            var validationMessageTagHelper = new ValidationMessageTagHelper(htmlGenerator)
            {
                For = modelExpression
            };

            var expectedPreContent  = "original pre-content";
            var expectedContent     = "original content";
            var expectedPostContent = "original post-content";

            var tagHelperContext = new TagHelperContext(
                allAttributes: new TagHelperAttributeList
            {
                { "id", "myvalidationmessage" },
                { "for", modelExpression },
            },
                items: new Dictionary <object, object>(),
                uniqueId: "test",
                getChildContentAsync: useCachedResult =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("Something");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });
            var output = new TagHelperOutput(
                expectedTagName,
                attributes: new TagHelperAttributeList
            {
                { "id", "myvalidationmessage" }
            });

            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);

            var viewContext = TestableHtmlGenerator.GetViewContext(model: null,
                                                                   htmlGenerator: htmlGenerator,
                                                                   metadataProvider: metadataProvider);

            validationMessageTagHelper.ViewContext = viewContext;

            // Act
            await validationMessageTagHelper.ProcessAsync(tagHelperContext, output);

            // Assert
            Assert.Equal(4, output.Attributes.Count);
            var attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("id"));

            Assert.Equal("myvalidationmessage", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("class"));
            Assert.Equal("field-validation-valid", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-valmsg-for"));
            Assert.Equal("Name", attribute.Value);
            attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-valmsg-replace"));
            Assert.Equal("true", attribute.Value);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
            Assert.Equal(expectedTagName, output.TagName);
        }
コード例 #16
0
        public async Task ProcessAsync_DoesNothingIfNullFor()
        {
            // Arrange
            var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
            var validationMessageTagHelper = new ValidationMessageTagHelper(generator.Object);
            var expectedPreContent = "original pre-content";
            var expectedContent = "original content";
            var expectedPostContent = "original post-content";
            var output = new TagHelperOutput(
                tagName: "span",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
            output.PreContent.SetContent(expectedPreContent);
            output.Content.SetContent(expectedContent);
            output.PostContent.SetContent(expectedPostContent);

            var context = new TagHelperContext(
                allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
                    Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
                items: new Dictionary<object, object>(),
                uniqueId: "test");

            var viewContext = CreateViewContext();
            validationMessageTagHelper.ViewContext = viewContext;

            // Act
            await validationMessageTagHelper.ProcessAsync(context, output);

            // Assert
            Assert.Equal("span", output.TagName);
            Assert.Empty(output.Attributes);
            Assert.Equal(expectedPreContent, output.PreContent.GetContent());
            Assert.Equal(expectedContent, output.Content.GetContent());
            Assert.Equal(expectedPostContent, output.PostContent.GetContent());
        }