public void TagHelperDescriptorCache_TypeNameAffectsHash() { // Arrange var expectedPropertyName = "PropertyName"; var intTagHelperBuilder = new DefaultTagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "TestTagHelper", "Test"); _ = intTagHelperBuilder.TypeName("TestTagHelper"); intTagHelperBuilder.BoundAttributeDescriptor(intBuilder => intBuilder .Name("test") .PropertyName(expectedPropertyName) .TypeName(typeof(int).FullName) ); var intTagHelper = intTagHelperBuilder.Build(); var stringTagHelperBuilder = new DefaultTagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "TestTagHelper", "Test"); _ = stringTagHelperBuilder.TypeName("TestTagHelper"); stringTagHelperBuilder.BoundAttributeDescriptor(stringBuilder => stringBuilder .Name("test") .PropertyName(expectedPropertyName) .TypeName(typeof(string).FullName) ); var stringTagHelper = stringTagHelperBuilder.Build(); // Act TagHelperDescriptorCache.Set(intTagHelper.GetHashCode(), intTagHelper); // Assert Assert.False(TagHelperDescriptorCache.TryGetDescriptor(stringTagHelper.GetHashCode(), out var descriptor)); }
public void BoundAttributeDescriptor_HashChangesWithType() { var expectedPropertyName = "PropertyName"; var tagHelperBuilder = new DefaultTagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "TestTagHelper", "Test"); _ = tagHelperBuilder.TypeName("TestTagHelper"); var intBuilder = new DefaultBoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind); _ = intBuilder .Name("test") .PropertyName(expectedPropertyName) .TypeName(typeof(int).FullName); var intDescriptor = intBuilder.Build(); var stringBuilder = new DefaultBoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind); _ = stringBuilder .Name("test") .PropertyName(expectedPropertyName) .TypeName(typeof(string).FullName); var stringDescriptor = stringBuilder.Build(); Assert.NotEqual(intDescriptor.GetHashCode(), stringDescriptor.GetHashCode()); }
public void WriteSetPreallocatedTagHelperProperty_IndexerAttribute_MultipleValues() { // Arrange var extension = new PreallocatedAttributeTargetExtension(); var context = TestCodeRenderingContext.CreateRuntime(); var tagHelperBuilder = new DefaultTagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "FooTagHelper", "Test"); tagHelperBuilder.TypeName("FooTagHelper"); var builder = new DefaultBoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind); builder .Name("Foo") .TypeName("System.Collections.Generic.Dictionary<System.String, System.String>") .AsDictionaryAttribute("pre-", "System.String") .PropertyName("FooProp"); var boundAttribute = builder.Build(); var tagHelper = tagHelperBuilder.Build(); var tagHelperNode = new TagHelperIntermediateNode(); var node1 = new PreallocatedTagHelperPropertyIntermediateNode() { AttributeName = "pre-Bar", FieldName = "__FooTagHelper", VariableName = "_tagHelper0s", BoundAttribute = boundAttribute, IsIndexerNameMatch = true, PropertyName = "FooProp", TagHelper = tagHelper, }; var node2 = new PreallocatedTagHelperPropertyIntermediateNode() { AttributeName = "pre-Foo", FieldName = "__FooTagHelper", VariableName = "_tagHelper1", BoundAttribute = boundAttribute, IsIndexerNameMatch = true, PropertyName = "FooProp", TagHelper = tagHelper, }; tagHelperNode.Children.Add(node1); tagHelperNode.Children.Add(node2); Push(context, tagHelperNode); // Act extension.WriteTagHelperProperty(context, node2); // Assert var csharp = context.CodeWriter.GenerateCode(); Assert.Equal( @"__FooTagHelper.FooProp[""Foo""] = (string)_tagHelper1.Value; __tagHelperExecutionContext.AddTagHelperAttribute(_tagHelper1); ", csharp, ignoreLineEndingDifferences: true); }
public void WriteTagHelperProperty_RendersCorrectly() { // Arrange var extension = new PreallocatedAttributeTargetExtension(); var context = TestCodeRenderingContext.CreateRuntime(); var tagHelperBuilder = new DefaultTagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "FooTagHelper", "Test"); tagHelperBuilder.TypeName("FooTagHelper"); var builder = new DefaultBoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind); builder .Name("Foo") .TypeName("System.String") .PropertyName("FooProp"); var descriptor = builder.Build(); var tagHelperNode = new TagHelperIntermediateNode(); var node = new PreallocatedTagHelperPropertyIntermediateNode() { AttributeName = descriptor.Name, BoundAttribute = descriptor, FieldName = "__FooTagHelper", PropertyName = "FooProp", VariableName = "_tagHelper1", }; tagHelperNode.Children.Add(node); Push(context, tagHelperNode); // Act extension.WriteTagHelperProperty(context, node); // Assert var csharp = context.CodeWriter.GenerateCode(); Assert.Equal( @"__FooTagHelper.FooProp = (string)_tagHelper1.Value; __tagHelperExecutionContext.AddTagHelperAttribute(_tagHelper1); ", csharp, ignoreLineEndingDifferences: true); }
public void DisplayName_DefaultsToPropertyLookingDisplayName() { // Arrange var tagHelperBuilder = new DefaultTagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "TestTagHelper", "Test"); tagHelperBuilder.TypeName("TestTagHelper"); var builder = new DefaultBoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind); builder .TypeName(typeof(int).FullName) .PropertyName("SomeProperty"); // Act var descriptor = builder.Build(); // Assert Assert.Equal("int TestTagHelper.SomeProperty", descriptor.DisplayName); }
public void GetPropertyName_ReturnsNullIfNoPropertyName() { // Arrange var tagHelperBuilder = new DefaultTagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "TestTagHelper", "Test"); tagHelperBuilder.TypeName("TestTagHelper"); var builder = new DefaultBoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind); builder .Name("test") .TypeName(typeof(int).FullName); var descriptor = builder.Build(); // Act var propertyName = descriptor.GetPropertyName(); // Assert Assert.Null(propertyName); }
public void IsDefaultKind_ReturnsFalse_IfKindIsNotDefault() { // Arrange var tagHelperBuilder = new DefaultTagHelperDescriptorBuilder("other-kind", "TestTagHelper", "Test"); tagHelperBuilder.TypeName("TestTagHelper"); var builder = new DefaultBoundAttributeDescriptorBuilder(tagHelperBuilder, "other-kind"); builder .Name("test") .PropertyName("IntProperty") .TypeName(typeof(int).FullName); var descriptor = builder.Build(); // Act var isDefault = descriptor.IsDefaultKind(); // Assert Assert.False(isDefault); }
public void IsDefaultKind_ReturnsTrue_IfKindIsDefault() { // Arrange var tagHelperBuilder = new DefaultTagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "TestTagHelper", "Test"); tagHelperBuilder.TypeName("TestTagHelper"); var builder = new DefaultBoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind); builder .Name("test") .PropertyName("IntProperty") .TypeName(typeof(int).FullName); var descriptor = builder.Build(); // Act var isDefault = descriptor.IsDefaultKind(); // Assert Assert.True(isDefault); }
public void ExpectsBooleanValue_ReturnsFalse_ForNonBooleanProperty() { // Arrange var tagHelperBuilder = new DefaultTagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "TestTagHelper", "Test"); tagHelperBuilder.TypeName("TestTagHelper"); var builder = new DefaultBoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind); builder .Name("test") .PropertyName("BoundProp") .TypeName(typeof(int).FullName); var descriptor = builder.Build(); // Act var result = descriptor.ExpectsBooleanValue("test"); // Assert Assert.False(result); }
public void ExpectsBooleanValue_ReturnsFalse_BooleanIndexerAndNameMismatch() { // Arrange var tagHelperBuilder = new DefaultTagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "TestTagHelper", "Test"); tagHelperBuilder.TypeName("TestTagHelper"); var builder = new DefaultBoundAttributeDescriptorBuilder(tagHelperBuilder, TagHelperConventions.DefaultKind); builder .Name("test") .PropertyName("BoundProp") .TypeName("System.Collection.Generic.IDictionary<string, bool>") .AsDictionary("prefix-test-", typeof(bool).FullName); var descriptor = builder.Build(); // Act var result = descriptor.ExpectsBooleanValue("test"); // Assert Assert.False(result); }