forked from aspnet/Mvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RoslynCompilationService should set the generated code when compilati…
…on is successful Fixes aspnet#895
- Loading branch information
Showing
4 changed files
with
109 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/Microsoft.AspNet.Mvc.Razor/Compilation/UncachedCompilationResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
test/Microsoft.AspNet.Mvc.Razor.Test/Compilation/CompilerCacheTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |