RoslynCompilationService should set the generated code when compilation is successful

Fixes #895
This commit is contained in:
Pranav K 2014-08-13 12:16:27 -07:00
parent 64d797a489
commit 764b1e64b8
4 changed files with 109 additions and 6 deletions

View File

@ -15,7 +15,10 @@ namespace Microsoft.AspNet.Mvc.Razor
{
private Type _type;
private CompilationResult()
/// <summary>
/// Creates a new instance of <see cref="CompilationResult"/>.
/// </summary>
protected CompilationResult()
{
}
@ -40,12 +43,12 @@ namespace Microsoft.AspNet.Mvc.Razor
public IEnumerable<CompilationMessage> Messages { get; private set; }
/// <summary>
/// Gets the generated C# content that was compiled.
/// Gets (or sets in derived types) the generated C# content that was compiled.
/// </summary>
public string CompiledContent { get; private set; }
public string CompiledContent { get; protected set; }
/// <summary>
/// Gets the type produced as a result of compilation.
/// Gets (or sets in derived types) the type produced as a result of compilation.
/// </summary>
/// <exception cref="CompilationFailedException">An error occured during compilation.</exception>
public Type CompiledType
@ -59,6 +62,10 @@ namespace Microsoft.AspNet.Mvc.Razor
return _type;
}
protected set
{
_type = value;
}
}
private IFileInfo File { get; set; }
@ -91,7 +98,7 @@ namespace Microsoft.AspNet.Mvc.Razor
{
return new CompilationResult
{
_type = type
CompiledType = type
};
}

View File

@ -106,7 +106,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
var type = assembly.GetExportedTypes()
.First();
return CompilationResult.Successful(type);
return UncachedCompilationResult.Successful(type, compilationContent);
}
}
}

View File

@ -0,0 +1,34 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
namespace Microsoft.AspNet.Mvc.Razor
{
/// <summary>
/// Represents the result of compilation that does not come from the <see cref="CompilerCache" />.
/// </summary>
public class UncachedCompilationResult : CompilationResult
{
private UncachedCompilationResult()
{
}
/// <summary>
/// Creates a <see cref="UncachedCompilationResult"/> that represents a success in compilation.
/// </summary>
/// <param name="type">The compiled type.</param>
/// <param name="compiledContent">The generated C# content that was compiled.</param>
/// <returns>An <see cref="UncachedCompilationResult"/> instance that indicates a successful
/// compilation.</returns>
public static UncachedCompilationResult Successful([NotNull] Type type,
[NotNull] string compiledContent)
{
return new UncachedCompilationResult
{
CompiledType = type,
CompiledContent = compiledContent,
};
}
}
}

View File

@ -0,0 +1,62 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.FileSystems;
using Moq;
using Xunit;
namespace Microsoft.AspNet.Mvc.Razor.Test
{
public class CompilerCacheTest
{
[Fact]
public void GetOrAdd_ReturnsCompilationResultFromFactory()
{
// Arrange
var cache = new CompilerCache();
var fileInfo = Mock.Of<IFileInfo>();
var type = GetType();
var expected = UncachedCompilationResult.Successful(type, "hello world");
// Act
var actual = cache.GetOrAdd(fileInfo, () => expected);
// Assert
Assert.Same(expected, actual);
Assert.Equal("hello world", actual.CompiledContent);
Assert.Same(type, actual.CompiledType);
}
[Fact]
public void GetOrAdd_DoesNotCacheCompiledContent_OnCallsAfterInitial()
{
// Arrange
var lastModified = DateTime.UtcNow;
var cache = new CompilerCache();
var fileInfo = new Mock<IFileInfo>();
fileInfo.SetupGet(f => f.PhysicalPath)
.Returns("test");
fileInfo.SetupGet(f => f.LastModified)
.Returns(lastModified);
var type = GetType();
var uncachedResult = UncachedCompilationResult.Successful(type, "hello world");
// Act
cache.GetOrAdd(fileInfo.Object, () => uncachedResult);
var actual1 = cache.GetOrAdd(fileInfo.Object, () => uncachedResult);
var actual2 = cache.GetOrAdd(fileInfo.Object, () => uncachedResult);
// Assert
Assert.NotSame(uncachedResult, actual1);
Assert.NotSame(uncachedResult, actual2);
var result = Assert.IsType<CompilationResult>(actual1);
Assert.Null(actual1.CompiledContent);
Assert.Same(type, actual1.CompiledType);
result = Assert.IsType<CompilationResult>(actual2);
Assert.Null(actual2.CompiledContent);
Assert.Same(type, actual2.CompiledType);
}
}
}