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); }
protected override CSharpCodeWritingScope BuildClassDeclaration(CSharpCodeWriter writer) { // Grab the last model chunk so it gets intellisense. var modelChunk = ChunkHelper.GetModelChunk(Context.ChunkTreeBuilder.ChunkTree); Model = modelChunk != null ? modelChunk.ModelType : _defaultModel; // If there were any model chunks then we need to modify the class declaration signature. if (modelChunk != null) { writer.Write(string.Format(CultureInfo.InvariantCulture, "public class {0} : ", Context.ClassName)); var modelVisitor = new ModelChunkVisitor(writer, Context); // This generates the base class signature modelVisitor.Accept(modelChunk); writer.WriteLine(); return(new CSharpCodeWritingScope(writer)); } else { return(base.BuildClassDeclaration(writer)); } }
protected override CSharpCodeWritingScope BuildClassDeclaration(CSharpCodeWriter writer) { // Grab the last model chunk so it gets intellisense. var modelChunk = ChunkHelper.GetModelChunk(Context.ChunkTreeBuilder.ChunkTree); Model = modelChunk != null ? modelChunk.ModelType : _defaultModel; // If there were any model chunks then we need to modify the class declaration signature. if (modelChunk != null) { writer.Write(string.Format(CultureInfo.InvariantCulture, "public class {0} : ", Context.ClassName)); var modelVisitor = new ModelChunkVisitor(writer, Context); // This generates the base class signature modelVisitor.Accept(modelChunk); writer.WriteLine(); return new CSharpCodeWritingScope(writer); } else { return base.BuildClassDeclaration(writer); } }
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); }
protected override CSharpCodeWritingScope BuildClassDeclaration(CSharpCodeWriter writer) { // Grab the last model chunk so it gets intellisense. // NOTE: If there's more than 1 model chunk there will be a Razor error BUT we want intellisense to // show up on the current model chunk that the user is typing. var modelChunk = Context.CodeTreeBuilder.CodeTree.Chunks.OfType <ModelChunk>() .LastOrDefault(); Model = modelChunk != null ? modelChunk.ModelType : _hostOptions.DefaultModel; // If there were any model chunks then we need to modify the class declaration signature. if (modelChunk != null) { writer.Write(string.Format(CultureInfo.InvariantCulture, "public class {0} : ", Context.ClassName)); var modelVisitor = new ModelChunkVisitor(writer, Context); // This generates the base class signature modelVisitor.Accept(modelChunk); writer.WriteLine(); return(new CSharpCodeWritingScope(writer)); } else { return(base.BuildClassDeclaration(writer)); } }
protected override CSharpCodeWritingScope BuildClassDeclaration(CSharpCodeWriter writer) { // Grab the last model chunk so it gets intellisense. // NOTE: If there's more than 1 model chunk there will be a Razor error BUT we want intellisense to // show up on the current model chunk that the user is typing. var modelChunk = Context.CodeTreeBuilder.CodeTree.Chunks.OfType<ModelChunk>() .LastOrDefault(); Model = modelChunk != null ? modelChunk.ModelType : _hostOptions.DefaultModel; // If there were any model chunks then we need to modify the class declaration signature. if (modelChunk != null) { writer.Write(string.Format(CultureInfo.InvariantCulture, "public class {0} : ", Context.ClassName)); var modelVisitor = new ModelChunkVisitor(writer, Context); // This generates the base class signature modelVisitor.Accept(modelChunk); writer.WriteLine(); return new CSharpCodeWritingScope(writer); } else { return base.BuildClassDeclaration(writer); } }
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); }