コード例 #1
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);
    }
コード例 #2
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);
    }