From cf1355245f010193c4b77d5fd9a8345be56b6513 Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Fri, 14 Aug 2015 23:52:50 -0700 Subject: [PATCH] Add `RestrictChildrenAttribute` specific `TagHelperDescriptorFactory` tests. - We covered invalid tag names in the attribute but not the full end-to-end descriptor factory resolution piece. #255 --- .../TagHelperDescriptorFactoryTest.cs | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/TagHelperDescriptorFactoryTest.cs b/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/TagHelperDescriptorFactoryTest.cs index b73db12361..044650919b 100644 --- a/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/TagHelperDescriptorFactoryTest.cs +++ b/test/Microsoft.AspNet.Razor.Runtime.Test/TagHelpers/TagHelperDescriptorFactoryTest.cs @@ -17,6 +17,101 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers private static readonly string AssemblyName = typeof(TagHelperDescriptorFactoryTest).GetTypeInfo().Assembly.GetName().Name; + public static TheoryData RestrictChildrenData + { + get + { + // tagHelperType, expectedDescriptors + return new TheoryData + { + { + typeof(RestrictChildrenTagHelper), + new[] + { + new TagHelperDescriptor( + prefix: string.Empty, + tagName: "restrict-children", + typeName: typeof(RestrictChildrenTagHelper).FullName, + assemblyName: AssemblyName, + attributes: Enumerable.Empty(), + requiredAttributes: Enumerable.Empty(), + allowedChildren: new[] { "p" }, + tagStructure: TagStructure.Unspecified, + designTimeDescriptor: null) + } + }, + { + typeof(DoubleRestrictChildrenTagHelper), + new[] + { + new TagHelperDescriptor( + prefix: string.Empty, + tagName: "double-restrict-children", + typeName: typeof(DoubleRestrictChildrenTagHelper).FullName, + assemblyName: AssemblyName, + attributes: Enumerable.Empty(), + requiredAttributes: Enumerable.Empty(), + allowedChildren: new[] { "p", "strong" }, + tagStructure: TagStructure.Unspecified, + designTimeDescriptor: null) + } + }, + { + typeof(MultiTargetRestrictChildrenTagHelper), + new[] + { + new TagHelperDescriptor( + prefix: string.Empty, + tagName: "div", + typeName: typeof(MultiTargetRestrictChildrenTagHelper).FullName, + assemblyName: AssemblyName, + attributes: Enumerable.Empty(), + requiredAttributes: Enumerable.Empty(), + allowedChildren: new[] { "p", "strong" }, + tagStructure: TagStructure.Unspecified, + designTimeDescriptor: null), + new TagHelperDescriptor( + prefix: string.Empty, + tagName: "p", + typeName: typeof(MultiTargetRestrictChildrenTagHelper).FullName, + assemblyName: AssemblyName, + attributes: Enumerable.Empty(), + requiredAttributes: Enumerable.Empty(), + allowedChildren: new[] { "p", "strong" }, + tagStructure: TagStructure.Unspecified, + designTimeDescriptor: null), + } + }, + }; + } + } + + [Theory] + [MemberData(nameof(RestrictChildrenData))] + public void CreateDescriptors_CreatesDescriptorsWithAllowedChildren( + Type tagHelperType, + TagHelperDescriptor[] expectedDescriptors) + { + // Arrange + var errorSink = new ErrorSink(); + + // Act + var descriptors = TagHelperDescriptorFactory.CreateDescriptors( + AssemblyName, + tagHelperType, + designTime: false, + errorSink: errorSink); + + // Assert + Assert.Empty(errorSink.Errors); + + // We don't care about order. Mono returns reflected attributes differently so we need to ensure order + // doesn't matter by sorting. + descriptors = descriptors.OrderBy(descriptor => descriptor.TagName); + + Assert.Equal(expectedDescriptors, descriptors, CaseSensitiveTagHelperDescriptorComparer.Default); + } + public static TheoryData TagStructureData { get @@ -2034,6 +2129,23 @@ namespace Microsoft.AspNet.Razor.Runtime.TagHelpers designTimeDescriptor: null); } + [RestrictChildren("p")] + private class RestrictChildrenTagHelper + { + } + + [RestrictChildren("p", "strong")] + private class DoubleRestrictChildrenTagHelper + { + } + + [TargetElement("p")] + [TargetElement("div")] + [RestrictChildren("p", "strong")] + private class MultiTargetRestrictChildrenTagHelper + { + } + [TargetElement("input", TagStructure = TagStructure.WithoutEndTag)] private class TagStructureTagHelper : TagHelper {