Exemplo n.º 1
0
        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());
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
    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);
    }
Exemplo n.º 5
0
    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);
    }
Exemplo n.º 6
0
    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);
    }
Exemplo n.º 7
0
    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);
    }
Exemplo n.º 8
0
    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);
    }