Exemplo n.º 1
0
        public void ModelExpressionPass_ModelExpressionProperty_SimpleExpression()
        {
            // Arrange

            // Using \r\n here because we verify line mappings
            var codeDocument = CreateDocument(
                "@addTagHelper TestTagHelper, TestAssembly\r\n<p foo=\"Bar\">");

            var tagHelpers = new[]
            {
                TagHelperDescriptorBuilder.Create("TestTagHelper", "TestAssembly")
                .BoundAttributeDescriptor(attribute =>
                                          attribute
                                          .Name("Foo")
                                          .TypeName("Microsoft.AspNetCore.Mvc.ViewFeatures.ModelExpression"))
                .TagMatchingRuleDescriptor(rule =>
                                           rule.RequireTagName("p"))
                .Build()
            };

            var engine = CreateEngine(tagHelpers);
            var pass   = new ModelExpressionPass()
            {
                Engine = engine,
            };

            var irDocument = CreateIRDocument(engine, codeDocument);

            // Act
            pass.Execute(codeDocument, irDocument);

            // Assert
            var tagHelper   = FindTagHelperNode(irDocument);
            var setProperty = tagHelper.Children.OfType <TagHelperPropertyIntermediateNode>().Single();

            var expression = Assert.IsType <CSharpExpressionIntermediateNode>(Assert.Single(setProperty.Children));

            Assert.Equal("ModelExpressionProvider.CreateModelExpression(ViewData, __model => __model.Bar)", GetCSharpContent(expression));

            var originalNode = Assert.IsAssignableFrom <IntermediateToken>(expression.Children[2]);

            Assert.Equal(TokenKind.CSharp, originalNode.Kind);
            Assert.Equal("Bar", originalNode.Content);
            Assert.Equal(new SourceSpan("test.cshtml", 51, 1, 8, 3), originalNode.Source.Value);
        }
Exemplo n.º 2
0
        public void ModelExpressionPass_NonModelExpressionProperty_Ignored()
        {
            // Arrange
            var codeDocument = CreateDocument(@"
@addTagHelper TestTagHelper, TestAssembly
<p foo=""17"">");

            var tagHelpers = new[]
            {
                TagHelperDescriptorBuilder.Create("TestTagHelper", "TestAssembly")
                .BoundAttributeDescriptor(attribute =>
                                          attribute
                                          .Name("Foo")
                                          .TypeName("System.Int32"))
                .TagMatchingRuleDescriptor(rule =>
                                           rule.RequireTagName("p"))
                .Build()
            };

            var engine = CreateEngine(tagHelpers);
            var pass   = new ModelExpressionPass()
            {
                Engine = engine,
            };

            var irDocument = CreateIRDocument(engine, codeDocument);

            // Act
            pass.Execute(codeDocument, irDocument);

            // Assert
            var tagHelper   = FindTagHelperNode(irDocument);
            var setProperty = tagHelper.Children.OfType <TagHelperPropertyIntermediateNode>().Single();

            var token = Assert.IsAssignableFrom <IntermediateToken>(Assert.Single(setProperty.Children));

            Assert.True(token.IsCSharp);
            Assert.Equal("17", token.Content);
        }