Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Changes for @Inject support
Browse files Browse the repository at this point in the history
Fixes #35
  • Loading branch information
pranavkm committed Jun 3, 2014
1 parent 47141b2 commit 4f255f8
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ public override CodeBuilderResult Build()
new CSharpTypeMemberVisitor(writer, Context).Accept(Tree.Chunks);
new CSharpDesignTimeHelpersVisitor(writer, Context).AcceptTree(Tree);

writer.WriteLineHiddenDirective();
using (writer.BuildConstructor(Context.ClassName))
{
// Any constructor based logic that we need to add?
};
BuildConstructor(writer);

// Add space inbetween constructor and method body
writer.WriteLine();
Expand All @@ -72,6 +68,15 @@ public override CodeBuilderResult Build()
return new CodeBuilderResult(writer.GenerateCode(), writer.LineMappingManager.Mappings);
}

protected virtual void BuildConstructor(CSharpCodeWriter writer)
{
writer.WriteLineHiddenDirective();
using (writer.BuildConstructor(Context.ClassName))
{
// Any constructor based logic that we need to add?
};
}

private void AddImports(CodeTree codeTree, CSharpCodeWriter writer, IEnumerable<string> defaultImports)
{
// Write out using directives
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Accept(IList<Chunk> chunks)
}
}

public void Accept(Chunk chunk)
public virtual void Accept(Chunk chunk)
{
if (chunk == null)
{
Expand Down Expand Up @@ -105,10 +105,6 @@ public void Accept(Chunk chunk)
{
Visit((SessionStateChunk)chunk);
}
else
{
throw new InvalidOperationException("Unknown chunk type " + chunk.GetType().Name);
}
}

protected abstract void Visit(LiteralChunk chunk);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private void UsingDeclaration()
}
}

private bool NamespaceOrTypeName()
protected bool NamespaceOrTypeName()
{
if (Optional(CSharpSymbolType.Identifier) || Optional(CSharpSymbolType.Keyword))
{
Expand Down
14 changes: 14 additions & 0 deletions src/Microsoft.AspNet.Razor/RazorEngineHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,19 @@ public virtual RazorCodeGenerator DecorateCodeGenerator(RazorCodeGenerator incom
}
return incomingCodeGenerator;
}

/// <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, CodeGeneratorContext context)
{
if (incomingBuilder == null)
{
throw new ArgumentNullException("incomingBuilder");
}
return incomingBuilder;
}
}
}
8 changes: 7 additions & 1 deletion src/Microsoft.AspNet.Razor/RazorTemplateEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected internal virtual GeneratorResults GenerateCodeCore(ITextDocument input
generator.DesignTimeMode = Host.DesignTimeMode;
generator.Visit(results);

var builder = Host.CodeLanguage.CreateCodeBuilder(generator.Context);
var builder = CreateCodeBuilder(generator.Context);
var builderResult = builder.Build();

// Collect results and return
Expand All @@ -191,5 +191,11 @@ protected internal virtual RazorParser CreateParser()
DesignTimeMode = Host.DesignTimeMode
};
}

protected internal virtual CodeBuilder CreateCodeBuilder(CodeGeneratorContext context)
{
return Host.DecorateCodeBuilder(Host.CodeLanguage.CreateCodeBuilder(context),
context);
}
}
}
21 changes: 20 additions & 1 deletion test/Microsoft.AspNet.Razor.Test/CSharpRazorCodeLanguageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNet.Razor.Generator;
using Microsoft.AspNet.Razor.Generator.Compiler.CSharp;
using Microsoft.AspNet.Razor.Parser;
using Microsoft.CSharp;
using Microsoft.TestCommon;

namespace Microsoft.AspNet.Razor.Test
Expand Down Expand Up @@ -42,5 +42,24 @@ public void CreateCodeGeneratorParserListenerReturnsNewCSharpCodeGeneratorParser
Assert.Equal("Baz", generator.SourceFileName);
Assert.Same(host, generator.Host);
}

[Fact]
public void CreateCodeBuilder_ReturnsNewCSharpCodeBuilder()
{
// Arrange
var language = new CSharpRazorCodeLanguage();
var host = new RazorEngineHost(language);
var context = CodeGeneratorContext.Create(host,
"myclass",
"myns",
string.Empty,
shouldGenerateLinePragmas: false);

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

// Assert
Assert.IsType<CSharpCodeBuilder>(generator);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Microsoft.AspNet.Razor.Generator;
using Microsoft.AspNet.Razor.Generator.Compiler;
using Microsoft.TestCommon;
using Moq;
using Moq.Protected;

namespace Microsoft.AspNet.Razor
{
public class ChunkVisitorTests
{
[Fact]
public void Accept_InvokesAppropriateOverload()
{
// Arrange
var chunks = new Chunk[] { new LiteralChunk(), new StatementChunk() };
var visitor = CreateVisitor();

// Act
visitor.Object.Accept(chunks);

// Assert
visitor.Protected().Verify("Visit", Times.AtMostOnce(), chunks[0]);
visitor.Protected().Verify("Visit", Times.AtMostOnce(), chunks[1]);
}

private static Mock<ChunkVisitor<CodeWriter>> CreateVisitor()
{
var context = CodeGeneratorContext.Create(new RazorEngineHost(new CSharpRazorCodeLanguage()),
"myclass",
"myns",
string.Empty,
shouldGenerateLinePragmas: false);
var writer = Mock.Of<CodeWriter>();
return new Mock<ChunkVisitor<CodeWriter>>(writer, context);
}

private class MyTestChunk : Chunk
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Compile Include="Framework\ParserTestBase.cs" />
<Compile Include="Framework\RawTextSymbol.cs" />
<Compile Include="Framework\TestSpanBuilder.cs" />
<Compile Include="Generator\CodeTree\ChunkVisitorTests.cs" />
<Compile Include="Generator\CodeTree\CodeTreeGenerationTest.cs" />
<Compile Include="Generator\CodeTree\CSharpCodeBuilderTests.cs" />
<Compile Include="Generator\CodeTree\CSharpPaddingBuilderTests.cs" />
Expand Down
24 changes: 24 additions & 0 deletions test/Microsoft.AspNet.Razor.Test/RazorTemplateEngineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Web.WebPages.TestUtils;
using Microsoft.AspNet.Razor.Generator;
using Microsoft.AspNet.Razor.Generator.Compiler.CSharp;
using Microsoft.AspNet.Razor.Parser;
using Microsoft.AspNet.Razor.Text;
using Microsoft.TestCommon;
Expand Down Expand Up @@ -104,6 +105,29 @@ public void CreateCodeGeneratorMethodPassesCodeGeneratorThroughDecorateMethodOnH
Assert.Equal(expected, actual);
}

[Fact]
public void CreateCodeBuilder_PassesCodeGeneratorThroughDecorateMethodOnHost()
{
// Arrange
var mockHost = new Mock<RazorEngineHost>(new CSharpRazorCodeLanguage()) { CallBase = true };
var context = CodeGeneratorContext.Create(mockHost.Object,
"different-class",
"different-ns",
string.Empty,
shouldGenerateLinePragmas: true);
var expected = new CSharpCodeBuilder(context);

mockHost.Setup(h => h.DecorateCodeBuilder(It.IsAny<CSharpCodeBuilder>(), context))
.Returns(expected);
var engine = new RazorTemplateEngine(mockHost.Object);

// Act
var actual = engine.CreateCodeBuilder(context);

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

[Fact]
public void ParseTemplateCopiesTextReaderContentToSeekableTextReaderAndPassesToParseTemplateCore()
{
Expand Down

0 comments on commit 4f255f8

Please sign in to comment.