Example #1
0
        public void DecorateCodeBuilder_DesignTimeRazorPathNormalizer_NormalizesChunkInheritanceUtilityPaths(
            string rootPrefix)
        {
            // Arrange
            var rootedAppPath = $"{rootPrefix}SomeComputer/Location/Project/";
            var rootedFilePath = $"{rootPrefix}SomeComputer/Location/Project/src/file.cshtml";
            var host = new MvcRazorHost(
                codeTreeCache: null,
                pathNormalizer: new DesignTimeRazorPathNormalizer(rootedAppPath));
            var chunkInheritanceUtility = new PathValidatingChunkInheritanceUtility(host);
            var codeBuilderContext = new CodeBuilderContext(
                new CodeGeneratorContext(
                    host,
                    host.DefaultClassName,
                    host.DefaultNamespace,
                    rootedFilePath,
                    shouldGenerateLinePragmas: true),
                new ParserErrorSink());
            var codeBuilder = new CSharpCodeBuilder(codeBuilderContext);
            host.ChunkInheritanceUtility = chunkInheritanceUtility;

            // Act
            host.DecorateCodeBuilder(codeBuilder, codeBuilderContext);

            // Assert
            Assert.Equal("src/file.cshtml", chunkInheritanceUtility.InheritedCodeTreePagePath, StringComparer.Ordinal);
        }
        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());
        }
        public void CodeTreeWithUsings()
        {
            var syntaxTreeNode = new Mock<Span>(new SpanBuilder());
            var language = new CSharpRazorCodeLanguage();
            var host = new CodeGenTestHost(language);
            var codeBuilderContext = new CodeBuilderContext(
                host,
                "TestClass",
                "TestNamespace",
                "Foo.cs",
                shouldGenerateLinePragmas: false,
                errorSink: new ErrorSink());
            codeBuilderContext.CodeTreeBuilder.AddUsingChunk("FakeNamespace1", syntaxTreeNode.Object);
            codeBuilderContext.CodeTreeBuilder.AddUsingChunk("FakeNamespace2.SubNamespace", syntaxTreeNode.Object);
            var codeBuilder = new CodeGenTestCodeBuilder(codeBuilderContext);

            // Act
            var result = codeBuilder.Build();

            BaselineWriter.WriteBaseline(
                @"test\Microsoft.AspNet.Razor.Test\TestFiles\CodeGenerator\CS\Output\CSharpCodeBuilder.cs",
                result.Code);

            var expectedOutput = TestFile.Create("TestFiles/CodeGenerator/CS/Output/CSharpCodeBuilder.cs").ReadAllText();

            // Assert
            Assert.Equal(expectedOutput, result.Code);
        }
            protected override CSharpCodeVisitor CreateCSharpCodeVisitor(
                CSharpCodeWriter writer,
                CodeBuilderContext context)
            {
                var bodyVisitor = base.CreateCSharpCodeVisitor(writer, context);

                bodyVisitor.TagHelperRenderer.AttributeValueCodeRenderer = new CustomTagHelperAttributeCodeRenderer();

                return bodyVisitor;
            }
Example #5
0
 public override CodeBuilder DecorateCodeBuilder(CodeBuilder incomingBuilder, CodeBuilderContext context)
 {
     if (incomingBuilder is CodeGenTestCodeBuilder)
     {
         return incomingBuilder;
     }
     else
     {
         return new CodeGenTestCodeBuilder(context);
     }
 }
Example #6
0
 private static Mock<ChunkVisitor<CodeWriter>> CreateVisitor()
 {
     var codeBuilderContext = new CodeBuilderContext(
         new RazorEngineHost(new CSharpRazorCodeLanguage()),
         "myclass",
         "myns",
         string.Empty,
         shouldGenerateLinePragmas: false,
         errorSink: new ParserErrorSink());
     var writer = Mock.Of<CodeWriter>();
     return new Mock<ChunkVisitor<CodeWriter>>(writer, codeBuilderContext);
 }
        public void CreateCodeBuilder_ReturnsNewCSharpCodeBuilder()
        {
            // Arrange
            var language = new CSharpRazorCodeLanguage();
            var host = new RazorEngineHost(language);
            var codeBuilderContext = new CodeBuilderContext(
                host,
                "myclass",
                "myns",
                string.Empty,
                shouldGenerateLinePragmas: false,
                errorSink: new ErrorSink());

            // Act
            var generator = language.CreateCodeBuilder(codeBuilderContext);

            // Assert
            Assert.IsType<CSharpCodeBuilder>(generator);
        }
Example #8
0
 public abstract CodeBuilder CreateCodeBuilder(CodeBuilderContext codeGeneratorContext);
            public TrackingUniqueIdsTagHelperCodeRenderer(
                IChunkVisitor bodyVisitor,
                CSharpCodeWriter writer,
                CodeBuilderContext context)
                : base(bodyVisitor, writer, context)
            {

            }
Example #10
0
 /// <summary>
 /// Gets an instance of the code builder and is provided an opportunity to decorate or replace it
 /// </summary>
 /// <param name="incomingBuilder">The code builder</param>
 /// <returns>Either the same code builder, after modifications, or a different code builder.</returns>
 public virtual CodeBuilder DecorateCodeBuilder(CodeBuilder incomingBuilder, CodeBuilderContext context)
 {
     if (incomingBuilder == null)
     {
         throw new ArgumentNullException("incomingBuilder");
     }
     return incomingBuilder;
 }
            public override void RenderAttributeValue(
                TagHelperAttributeDescriptor attributeInfo,
                CSharpCodeWriter writer,
                CodeBuilderContext context,
                Action<CSharpCodeWriter> renderAttributeValue,
                bool complexValue)
            {
                writer.Write("**From custom attribute code renderer**: ");

                base.RenderAttributeValue(attributeInfo, writer, context, renderAttributeValue, complexValue);
            }
Example #12
0
 public TestCSharpCodeBuilder(CodeBuilderContext context,
                              string defaultModel,
                              string activateAttribute,
                              GeneratedTagHelperAttributeContext tagHelperAttributeContext)
     : base(context, defaultModel, activateAttribute, tagHelperAttributeContext)
 {
     _tagHelperAttributeContext = tagHelperAttributeContext;
 }
Example #13
0
 protected internal virtual CodeBuilder CreateCodeBuilder(CodeBuilderContext context)
 {
     return Host.DecorateCodeBuilder(Host.CodeLanguage.CreateCodeBuilder(context),
                                     context);
 }
 public override CodeBuilder DecorateCodeBuilder(CodeBuilder incomingBuilder, CodeBuilderContext context)
 {
     return new AttributeCodeGeneratorReplacingCodeBuilder(context);
 }
Example #15
0
        protected internal virtual GeneratorResults GenerateCodeCore(ITextDocument input,
                                                                     string className,
                                                                     string rootNamespace,
                                                                     string sourceFileName,
                                                                     string checksum,
                                                                     CancellationToken? cancelToken)
        {
            className = (className ?? Host.DefaultClassName) ?? DefaultClassName;
            rootNamespace = (rootNamespace ?? Host.DefaultNamespace) ?? DefaultNamespace;

            // Run the parser
            var parser = CreateParser(sourceFileName);
            Debug.Assert(parser != null);
            var results = parser.Parse(input);

            // Generate code
            var generator = CreateCodeGenerator(className, rootNamespace, sourceFileName);
            generator.DesignTimeMode = Host.DesignTimeMode;
            generator.Visit(results);

            var codeBuilderContext = new CodeBuilderContext(generator.Context, results.ErrorSink);
            codeBuilderContext.Checksum = checksum;
            var builder = CreateCodeBuilder(codeBuilderContext);
            var builderResult = builder.Build();

            // Collect results and return
            return new GeneratorResults(results, builderResult, codeBuilderContext.CodeTreeBuilder.CodeTree);
        }
 public override CodeBuilder CreateCodeBuilder(CodeBuilderContext context)
 {
     return new CSharpCodeBuilder(context);
 }
Example #17
0
            public TestCSharpCodeBuilder(CodeBuilderContext context)
                : base(context)
            {

            }
Example #18
0
 protected internal override CodeBuilder CreateCodeBuilder(CodeBuilderContext context)
 {
     return Host.DecorateCodeBuilder(new TestCSharpCodeBuilder(context), context);
 }
Example #19
0
 protected override CSharpCodeVisitor CreateCSharpCodeVisitor(CSharpCodeWriter writer, CodeBuilderContext context)
 {
     var visitor = base.CreateCSharpCodeVisitor(writer, context);
     visitor.TagHelperRenderer = new NoUniqueIdsTagHelperCodeRenderer(visitor, writer, context);
     return visitor;
 }
 public AttributeCodeGeneratorReplacingCodeBuilder(CodeBuilderContext context)
     : base(context)
 {
 }
 public CodeGenTestCodeBuilder(CodeBuilderContext context)
     : base(context)
 {
 }
Example #22
0
 /// <summary>
 /// Gets an instance of the code builder and is provided an opportunity to decorate or replace it
 /// </summary>
 /// <param name="incomingBuilder">The code builder</param>
 /// <returns>Either the same code builder, after modifications, or a different code builder.</returns>
 public virtual CodeBuilder DecorateCodeBuilder(
     [NotNull] CodeBuilder incomingBuilder,
     CodeBuilderContext context)
 {
     return incomingBuilder;
 }
Example #23
0
            public override CodeBuilder DecorateCodeBuilder(CodeBuilder incomingBuilder, CodeBuilderContext context)
            {
                base.DecorateCodeBuilder(incomingBuilder, context);

                return new TestCSharpCodeBuilder(context,
                                                 DefaultModel,
                                                 ActivateAttribute,
                                                 new GeneratedTagHelperAttributeContext
                                                 {
                                                     ModelExpressionTypeName = ModelExpressionType,
                                                     CreateModelExpressionMethodName = CreateModelExpressionMethod
                                                 });
            }