Upgrade Roslyn dependency to 2.0.0.

- Added C# 7 unit and functional test to validate features work end-to-end.

#6149
This commit is contained in:
N. Taylor Mullen 2017-04-14 16:34:49 -07:00
parent 025870e8b9
commit 83faaebdb6
8 changed files with 116 additions and 6 deletions

View File

@ -8,7 +8,7 @@
<JsonNetBsonVersion>1.0.1</JsonNetBsonVersion> <JsonNetBsonVersion>1.0.1</JsonNetBsonVersion>
<MoqVersion>4.7.1</MoqVersion> <MoqVersion>4.7.1</MoqVersion>
<NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion> <NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>
<RoslynVersion>1.3.0</RoslynVersion> <RoslynVersion>2.0.0</RoslynVersion>
<RuntimeFrameworkVersion>2.0.0-*</RuntimeFrameworkVersion> <RuntimeFrameworkVersion>2.0.0-*</RuntimeFrameworkVersion>
<TestSdkVersion>15.0.0</TestSdkVersion> <TestSdkVersion>15.0.0</TestSdkVersion>
<WebApiClientVersion>5.2.2</WebApiClientVersion> <WebApiClientVersion>5.2.2</WebApiClientVersion>

View File

@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
/// </summary> /// </summary>
public class RazorViewEngineOptions public class RazorViewEngineOptions
{ {
private CSharpParseOptions _parseOptions = new CSharpParseOptions(LanguageVersion.CSharp6); private CSharpParseOptions _parseOptions = new CSharpParseOptions(LanguageVersion.CSharp7);
private CSharpCompilationOptions _compilationOptions = private CSharpCompilationOptions _compilationOptions =
new CSharpCompilationOptions(CodeAnalysis.OutputKind.DynamicallyLinkedLibrary); new CSharpCompilationOptions(CodeAnalysis.OutputKind.DynamicallyLinkedLibrary);
private Action<RoslynCompilationContext> _compilationCallback = c => { }; private Action<RoslynCompilationContext> _compilationCallback = c => { };

View File

@ -30,6 +30,30 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
public HttpClient Client { get; } public HttpClient Client { get; }
[Fact]
public async Task CanRender_CSharp7Views()
{
// Arrange
var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8");
var outputFile = "compiler/resources/BasicWebSite.Home.CSharp7View.html";
var expectedContent =
await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false);
// Act
var response = await Client.GetAsync("Home/CSharp7View");
var responseContent = await response.Content.ReadAsStringAsync();
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
#if GENERATE_BASELINES
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
#else
Assert.Equal(expectedContent, responseContent, ignoreLineEndingDifferences: true);
#endif
}
[Fact] [Fact]
public async Task CanRender_ViewComponentWithArgumentsFromController() public async Task CanRender_ViewComponentWithArgumentsFromController()
{ {

View File

@ -0,0 +1,19 @@
<p>
<strong>John Doe's</strong> favorite number is
<em>
(double) 6.02214085774747E&#x2B;23 </em>
</p>
<p>
<strong>John Smith's</strong> favorite number is
<em>
(long) 100000000000 </em>
</p>
<p>
<strong>Someone Nice's</strong> favorite number is
<em>
(decimal) 1.6180339887498948482045868344 </em>
</p>

View File

@ -2,9 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.IO;
using System.Reflection; using System.Reflection;
using System.Text;
using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Razor.Compilation; using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
@ -19,6 +17,38 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{ {
public class DefaultRoslynCompilationServiceTest public class DefaultRoslynCompilationServiceTest
{ {
[Fact]
public void Compile_SucceedsForCSharp7()
{
// Arrange
var content = @"
public class MyTestType
{
private string _name;
public string Name
{
get => _name;
set => _name = value ?? throw new System.ArgumentNullException(nameof(value));
}
}";
var compilationService = GetRoslynCompilationService();
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("Hello world", "test.cshtml"));
var csharpDocument = new RazorCSharpDocument()
{
GeneratedCode = content
};
// Act
var result = compilationService.Compile(codeDocument, csharpDocument);
// Assert
Assert.Equal("MyTestType", result.CompiledType.Name);
Assert.Null(result.CompilationFailures);
}
[Fact] [Fact]
public void Compile_ReturnsCompilationResult() public void Compile_ReturnsCompilationResult()
{ {
@ -219,7 +249,7 @@ public class MyNonCustomDefinedClass {}
RoslynCompilationContext usedCompilation = null; RoslynCompilationContext usedCompilation = null;
var options = GetOptions(c => usedCompilation = c); var options = GetOptions(c => usedCompilation = c);
var compilationService = GetRoslynCompilationService(options: options); var compilationService = GetRoslynCompilationService(options: options);
var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("Hello world", "some-relative-path")); var codeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("Hello world", "some-relative-path"));
var csharpDocument = new RazorCSharpDocument() var csharpDocument = new RazorCSharpDocument()

View File

@ -99,7 +99,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
}); });
Assert.Empty(parseOptions.PreprocessorSymbolNames); Assert.Empty(parseOptions.PreprocessorSymbolNames);
Assert.Equal(LanguageVersion.CSharp6, parseOptions.LanguageVersion); Assert.Equal(LanguageVersion.CSharp7, parseOptions.LanguageVersion);
} }
[Fact] [Fact]

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using BasicWebSite.Models; using BasicWebSite.Models;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@ -16,6 +17,18 @@ namespace BasicWebSite.Controllers
return View(); return View();
} }
public IActionResult CSharp7View()
{
var people = new List<(string FirstName, string LastName, object FavoriteNumber)>()
{
("John", "Doe", 6.022_140_857_747_474e23),
("John", "Smith", 100_000_000_000),
("Someone", "Nice", (decimal)1.618_033_988_749_894_848_204_586_834_365_638_117_720_309_179M),
};
return View(people);
}
// Keep the return type as object to ensure that we don't // Keep the return type as object to ensure that we don't
// wrap IActionResult instances into ObjectResults. // wrap IActionResult instances into ObjectResults.
public object PlainView() public object PlainView()

View File

@ -0,0 +1,24 @@
@model IEnumerable<(string FirstName, string LastName, object FavoriteNumber)>
@foreach (var person in Model)
{
<p>
<strong>@person.FirstName @person.LastName's</strong> favorite number is
<em>
@switch (person.FavoriteNumber)
{
case double doubleVal:
<text>(double) @doubleVal</text>
break;
case long longVal:
<text>(long) @longVal</text>
break;
case decimal longVal:
<text>(decimal) @longVal</text>
break;
}
</em>
</p>
}