How to do it...

  1. Open Roslyn.sln at the root of the Roslyn repo in VS2017.
  2. Open source file <%ROOT%>srcCompilersCSharpTestSemanticSemanticsImplicitlyTypedLocalsTests.cs.
  3. Add the following new unit test to the source file:
[Fact]
public void VarInferredTypeNotApparent()
{
var source = @"
class Class
{
void M(int x, string y)
{
var z = x + y;
}
}";

CreateCompilationWithMscorlib(source).VerifyDiagnostics();
}
  1. Build the test project and execute the unit test on a command-line console using the command line copied from the project's Debug property page, appending -method switch for the newly added unit test:
<%USERS_FOLDER%>.nugetpackagesxunit.runner.console2.2.0-beta4-build3444	oolsxunit.console.x86.exe "<%ROOT%>BinariesDebugUnitTestsCSharpCompilerSemanticTestRoslyn.Compilers.CSharp.Semantic.UnitTests.dll" -html "C:
oslynBinariesDebugUnitTestsCSharpCompilerSemanticTestxUnitResultsRoslyn.Compilers.CSharp.Semantic.UnitTests.html" -noshadow -method Microsoft.CodeAnalysis.CSharp.UnitTests.ImplicitlyTypedLocalTests.VarInferredTypeNotApparent
  1. Verify the unit test fails with the missing CS0823 diagnostic:
Expected:
Actual:
// (6,7): warning CS0823: Use an explicit type for declaration as the initializer type 'string' is not apparent due to conversions
// var z = x + y;
Diagnostic(ErrorCode.WRN_ImplicitlyTypedVariableNotRecommended, "z = x + y").WithArguments("string").WithLocation(6, 7)

Diff:
++> Diagnostic(ErrorCode.WRN_ImplicitlyTypedVariableNotRecommended, "z = x + y").WithArguments("string").WithLocation(6, 7)
  1. Add the missing diagnostic as an argument to the VerifyDiagnostics invocation in our unit test:
  1. Re-execute the unit test by repeating step 4 and verify that the test passes now.
If you get a DirectoryNotFoundException, ensure that the test results directory exists on the machine: <%ROOT%>BinariesDebugUnitTestsCSharpCompilerSemanticTestxUnitResults.
  1. Add another unit test to verify that the diagnostic does not fire for a case where the initializer binary expression has no implicit conversions:
[Fact]
public void VarInferredTypeApparent_NoDiagnostic()
{
var source = @"
class Class
{
void M(int x, string y)
{
var z = (string)(x + y);
}
}";

CreateCompilationWithMscorlib(source).VerifyDiagnostics();
}
  1. Execute the new unit test and verify that it passes.
You can also execute the unit tests inside Visual Studio using the Test Explorer window, but the test discovery for Roslyn.sln is quite slow due to thousands of unit tests across the solution. Hence, you might have to wait for a few minutes before you can execute the first unit test.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset