示例#1
0
        public void Visit_WithDesignTimeHost_GeneratesBaseClass_ForModelChunks()
        {
            // Arrange
            var expected =
                "MyBase<" + Environment.NewLine +
                "#line 1 \"\"" + Environment.NewLine +
                "My_Generic.After.Periods" + Environment.NewLine +
                Environment.NewLine +
                "#line default" + Environment.NewLine +
                "#line hidden" + Environment.NewLine +
                ">";
            var writer  = new CSharpCodeWriter();
            var context = CreateContext();

            context.Host.DesignTimeMode = true;

            var visitor = new ModelChunkVisitor(writer, context);
            var factory = SpanFactory.CreateCsHtml();
            var node    = (Span)factory.Code("Some code")
                          .As(new ModelCodeGenerator("MyType", "MyPropertyName"));

            // Act
            visitor.Accept(new Chunk[]
            {
                new LiteralChunk(),
                new ModelChunk("MyBase", "My_Generic.After.Periods")
                {
                    Association = node
                }
            });
            var code = writer.GenerateCode();

            // Assert
            Assert.Equal(expected, code);
        }
示例#2
0
        public void Visit_GeneratesBaseClass_ForModelChunks()
        {
            // Arrange
            var expected =
"MyBase<" + Environment.NewLine +
"#line 1 \"\"" + Environment.NewLine +
"My_Generic.After.Periods" + Environment.NewLine +
Environment.NewLine +
"#line default" + Environment.NewLine +
"#line hidden" + Environment.NewLine +
">";
            var writer = new CSharpCodeWriter();
            var context = CreateContext();

            var visitor = new ModelChunkVisitor(writer, context);
            var factory = SpanFactory.CreateCsHtml();
            var node = (Span)factory.Code("Some code")
                                    .As(new ModelChunkGenerator("MyBase", "MyGeneric"));

            // Act
            visitor.Accept(new Chunk[]
            {
                new LiteralChunk(),
                new ModelChunk("MyBase", "My_Generic.After.Periods") { Association = node }
            });
            var code = writer.GenerateCode();

            // Assert
            Assert.Equal(expected, code);
        }
示例#3
0
        public void Visit_GeneratesProperties_ForInjectChunks()
        {
            // Arrange
            var expected =
                @"[ActivateAttribute]
public MyType1 MyPropertyName1 { get; private set; }
[ActivateAttribute]
public MyType2 @MyPropertyName2 { get; private set; }
";
            var writer  = new CSharpCodeWriter();
            var context = CreateContext();

            var visitor = new InjectChunkVisitor(writer, context, "ActivateAttribute");
            var factory = SpanFactory.CreateCsHtml();
            var node    = (Span)factory.Code("Some code")
                          .As(new InjectParameterGenerator("MyType", "MyPropertyName"));

            // Act
            visitor.Accept(new Chunk[]
            {
                new LiteralChunk(),
                new InjectChunk("MyType1", "MyPropertyName1")
                {
                    Association = node
                },
                new InjectChunk("MyType2", "@MyPropertyName2")
                {
                    Association = node
                }
            });
            var code = writer.GenerateCode();

            // Assert
            Assert.Equal(expected, code);
        }
示例#4
0
        public void RenderAttributeValue_RendersModelExpressionsCorrectly(string modelExpressionType,
                                                                          string propertyType,
                                                                          string expectedValue)
        {
            // Arrange
            var renderer = new MvcTagHelperAttributeValueCodeRenderer(
                new GeneratedTagHelperAttributeContext
            {
                ModelExpressionTypeName         = modelExpressionType,
                CreateModelExpressionMethodName = "SomeMethod"
            });
            var attributeDescriptor = new TagHelperAttributeDescriptor("MyAttribute", "SomeProperty", propertyType);
            var writer           = new CSharpCodeWriter();
            var generatorContext = new CodeGeneratorContext(host: null,
                                                            className: string.Empty,
                                                            rootNamespace: string.Empty,
                                                            sourceFile: string.Empty,
                                                            shouldGenerateLinePragmas: true);
            var context = new CodeBuilderContext(generatorContext);

            // Act
            renderer.RenderAttributeValue(attributeDescriptor, writer, context,
                                          (codeWriter) =>
            {
                codeWriter.Write("MyValue");
            });

            // Assert
            Assert.Equal(expectedValue, writer.GenerateCode());
        }
        public void Accept_CorrectlyGeneratesCode(IList <Chunk> chunks)
        {
            // Arrange
            var writer       = new CSharpCodeWriter();
            var context      = ChunkVisitorTestFactory.CreateCodeGeneratorContext();
            var chunkVisitor = new ViewComponentTagHelperChunkVisitor(writer, context);

            var assembly       = typeof(ViewComponentTagHelperChunkVisitorTest).GetTypeInfo().Assembly;
            var path           = "TestFiles/Output/Runtime/GeneratedViewComponentTagHelperClasses.cs";
            var expectedOutput = ResourceFile.ReadResource(assembly, path, sourceFile: true);

            // Act
            chunkVisitor.Accept(chunks);
            var resultOutput = writer.GenerateCode();

#if GENERATE_BASELINES
            if (!string.Equals(expectedOutput, resultOutput, System.StringComparison.Ordinal))
            {
                ResourceFile.UpdateFile(assembly, path, expectedOutput, resultOutput);
            }
#else
            // Assert
            Assert.Equal(expectedOutput, resultOutput, ignoreLineEndingDifferences: true);
#endif
        }
示例#6
0
        public void Visit_GeneratesProperties_ForInjectChunks()
        {
            // Arrange
            var expected =
@"[ActivateAttribute]
public MyType1 MyPropertyName1 { get; private set; }
[ActivateAttribute]
public MyType2 @MyPropertyName2 { get; private set; }
";
            var writer = new CSharpCodeWriter();
            var context = CreateContext();

            var visitor = new InjectChunkVisitor(writer, context, "ActivateAttribute");
            var factory = SpanFactory.CreateCsHtml();
            var node = (Span)factory.Code("Some code")
                                    .As(new InjectParameterGenerator("MyType", "MyPropertyName"));

            // Act
            visitor.Accept(new Chunk[]
            {
                new LiteralChunk(),
                new InjectChunk("MyType1", "MyPropertyName1") { Association = node },
                new InjectChunk("MyType2", "@MyPropertyName2") { Association = node }
            });
            var code = writer.GenerateCode();

            // Assert
            Assert.Equal(expected, code);
        }
        public void RenderAttributeValue_RendersModelExpressionsCorrectly(string modelExpressionType,
                                                                          string propertyType,
                                                                          string expectedValue)
        {
            // Arrange
            var renderer = new MvcTagHelperAttributeValueCodeRenderer(
                new GeneratedTagHelperAttributeContext
                {
                    ModelExpressionTypeName = modelExpressionType,
                    CreateModelExpressionMethodName = "SomeMethod"
                });
            var attributeDescriptor = new TagHelperAttributeDescriptor("MyAttribute", "SomeProperty", propertyType);
            var writer = new CSharpCodeWriter();
            var generatorContext = new CodeGeneratorContext(host: null,
                                                            className: string.Empty,
                                                            rootNamespace: string.Empty,
                                                            sourceFile: string.Empty,
                                                            shouldGenerateLinePragmas: true);
            var errorSink = new ParserErrorSink();
            var context = new CodeBuilderContext(generatorContext, errorSink);

            // Act
            renderer.RenderAttributeValue(attributeDescriptor, writer, context,
            (codeWriter) =>
            {
                codeWriter.Write("MyValue");
            },
            complexValue: false);

            // Assert
            Assert.Equal(expectedValue, writer.GenerateCode());
        }
示例#8
0
        public void Visit_WithDesignTimeHost_GeneratesPropertiesAndLinePragmas_ForInjectChunks()
        {
            // Arrange
            var expected = string.Join(Environment.NewLine,
                                       @"[Microsoft.AspNet.Mvc.ActivateAttribute]",
                                       @"public",
                                       @"#line 1 """"",
                                       @"MyType1 MyPropertyName1",
                                       "",
                                       @"#line default",
                                       @"#line hidden",
                                       @"{ get; private set; }",
                                       @"[Microsoft.AspNet.Mvc.ActivateAttribute]",
                                       @"public",
                                       @"#line 1 """"",
                                       @"MyType2 @MyPropertyName2",
                                       "",
                                       @"#line default",
                                       @"#line hidden",
                                       @"{ get; private set; }",
                                       "");
            var writer  = new CSharpCodeWriter();
            var context = CreateContext();

            context.Host.DesignTimeMode = true;

            var visitor = new InjectChunkVisitor(writer, context, "Microsoft.AspNet.Mvc.ActivateAttribute");
            var factory = SpanFactory.CreateCsHtml();
            var node    = (Span)factory.Code("Some code")
                          .As(new InjectParameterGenerator("MyType", "MyPropertyName"));

            // Act
            visitor.Accept(new Chunk[]
            {
                new LiteralChunk(),
                new InjectChunk("MyType1", "MyPropertyName1")
                {
                    Association = node
                },
                new InjectChunk("MyType2", "@MyPropertyName2")
                {
                    Association = node
                }
            });
            var code = writer.GenerateCode();

            // Assert
            Assert.Equal(expected, code);
        }
示例#9
0
        public void Visit_IgnoresNonModelChunks()
        {
            // Arrange
            var writer  = new CSharpCodeWriter();
            var context = CreateContext();

            var visitor = new ModelChunkVisitor(writer, context);

            // Act
            visitor.Accept(new Chunk[]
            {
                new LiteralChunk(),
                new CodeAttributeChunk()
            });
            var code = writer.GenerateCode();

            // Assert
            Assert.Empty(code);
        }
示例#10
0
        public void Visit_IgnoresNonModelChunks()
        {
            // Arrange
            var writer = new CSharpCodeWriter();
            var context = CreateContext();

            var visitor = new ModelChunkVisitor(writer, context);

            // Act
            visitor.Accept(new Chunk[]
            {
                new LiteralChunk(),
                new CodeAttributeChunk()
            });
            var code = writer.GenerateCode();

            // Assert
            Assert.Empty(code);
        }
示例#11
0
        public void RenderAttributeValue_RendersModelExpressionsCorrectly(
            string modelExpressionType,
            string propertyType,
            string expectedValue)
        {
            // Arrange
            var renderer = new MvcTagHelperAttributeValueCodeRenderer(
                new GeneratedTagHelperAttributeContext
            {
                ModelExpressionTypeName             = modelExpressionType,
                CreateModelExpressionMethodName     = "SomeMethod",
                ModelExpressionProviderPropertyName = "Provider",
                ViewDataPropertyName = "ViewData"
            });
            var attributeDescriptor = new TagHelperAttributeDescriptor
            {
                Name         = "MyAttribute",
                PropertyName = "SomeProperty",
                TypeName     = propertyType,
            };
            var writer           = new CSharpCodeWriter();
            var generatorContext = new ChunkGeneratorContext(
                host: null,
                className: string.Empty,
                rootNamespace: string.Empty,
                sourceFile: string.Empty,
                shouldGenerateLinePragmas: true);
            var errorSink = new ErrorSink();
            var context   = new CodeGeneratorContext(generatorContext, errorSink);

            // Act
            renderer.RenderAttributeValue(attributeDescriptor, writer, context,
                                          (codeWriter) =>
            {
                codeWriter.Write("MyValue");
            },
                                          complexValue: false);

            // Assert
            Assert.Equal(expectedValue, writer.GenerateCode());
        }
示例#12
0
        public void Visit_WithDesignTimeHost_GeneratesPropertiesAndLinePragmas_ForPartialInjectChunks()
        {
            // Arrange
            var expected = @"[Microsoft.AspNet.Mvc.Razor.Internal.RazorInjectAttribute]
public
#line 1 """"
MyType1

#line default
#line hidden
{ get; private set; }
";
            var writer   = new CSharpCodeWriter();
            var context  = CreateContext();

            context.Host.DesignTimeMode = true;

            var visitor = new InjectChunkVisitor(writer, context, "Microsoft.AspNet.Mvc.Razor.Internal.RazorInjectAttribute");
            var factory = SpanFactory.CreateCsHtml();
            var node    = (Span)factory.Code("Some code")
                          .As(new InjectParameterGenerator("MyType", "MyPropertyName"));

            // Act
            visitor.Accept(new Chunk[]
            {
                new LiteralChunk(),
                new InjectChunk("MyType1", string.Empty)
                {
                    Association = node
                },
            });
            var code = writer.GenerateCode();

            // Assert
            Assert.Equal(expected, code);
        }
示例#13
0
        public void Visit_WithDesignTimeHost_GeneratesPropertiesAndLinePragmas_ForInjectChunks()
        {
            // Arrange
            var expected = string.Join(Environment.NewLine,
@"[Microsoft.AspNet.Mvc.Razor.Internal.RazorInjectAttribute]",
@"public",
@"#line 1 """"",
@"MyType1 MyPropertyName1",
"",
@"#line default",
@"#line hidden",
@"{ get; private set; }",
@"[Microsoft.AspNet.Mvc.Razor.Internal.RazorInjectAttribute]",
@"public",
@"#line 1 """"",
@"MyType2 @MyPropertyName2",
"",
@"#line default",
@"#line hidden",
@"{ get; private set; }",
"");
            var writer = new CSharpCodeWriter();
            var context = CreateContext();
            context.Host.DesignTimeMode = true;

            var visitor = new InjectChunkVisitor(writer, context, "Microsoft.AspNet.Mvc.Razor.Internal.RazorInjectAttribute");
            var factory = SpanFactory.CreateCsHtml();
            var node = (Span)factory.Code("Some code")
                                    .As(new InjectParameterGenerator("MyType", "MyPropertyName"));

            // Act
            visitor.Accept(new Chunk[]
            {
                new LiteralChunk(),
                new InjectChunk("MyType1", "MyPropertyName1") { Association = node },
                new InjectChunk("MyType2", "@MyPropertyName2") { Association = node }
            });
            var code = writer.GenerateCode();

            // Assert
            Assert.Equal(expected, code);
        }
        public void Visit_WithDesignTimeHost_GeneratesPropertiesAndLinePragmas_ForPartialInjectChunks()
        {
            // Arrange
            var expected = @"[Microsoft.AspNet.Mvc.ActivateAttribute]
public
#line 1 """"
MyType1

#line default
#line hidden
{ get; private set; }
";
            var writer = new CSharpCodeWriter();
            var context = CreateContext();
            context.Host.DesignTimeMode = true;

            var visitor = new InjectChunkVisitor(writer, context, "Microsoft.AspNet.Mvc.ActivateAttribute");
            var factory = SpanFactory.CreateCsHtml();
            var node = (Span)factory.Code("Some code")
                                    .As(new InjectParameterGenerator("MyType", "MyPropertyName"));

            // Act
            visitor.Accept(new Chunk[]
            {
                new LiteralChunk(),
                new InjectChunk("MyType1", string.Empty) { Association = node },
            });
            var code = writer.GenerateCode();

            // Assert
            Assert.Equal(expected, code);
        }