- Roslyn Cookbook
- Manish Vasani
- 387字
- 2021-07-15 17:07:35
How to do it...
- Open UnitTests.cs in the CSharpAnalyzers.Test project in the Solution Explorer solution to view the default unit tests created for the default symbol analyzer (type names should not contain lowercase letters) for the template analyzer project.

- Navigate to Test | Windows | Test Window to open the Test Explorer window to view the unit tests in the project. Default analyzer project has two unit tests:
- TestMethod1: This tests the scenario where analyzer diagnostic does not fire on the test code and
- TestMethod2: This tests the scenario where analyzer diagnostic does fire on the test code.

Note that the unit test project contains unit tests for both the DiagnosticAnalyzer and CodeFixProvider. This chapter deals with analyzer testing only. We will expand on the unit tests for the CodeFixProvider later in this book.
- Run all the unit tests for the project by right-clicking Not Run tests node in the Test Explorer, executing the Run selected tests context menu command, and verify that the tests pass.
- Edit TestMethod1 so that the test code now has a type with lower-case letters:
[TestMethod]
public void TestMethod1()
{
var test = @"class Class1 { }";
VerifyCSharpDiagnostic(test);
}
- Right-click on TestMethod1 in the editor, execute the Run tests context menu command, and verify that the test now fails with the diagnostic mismatch assert - expected "0" actual "1":

- Edit TestMethod1 to now add an excepted diagnostic for the new test code:
var expected = new DiagnosticResult
{
Id = "CSharpAnalyzers",
Message = String.Format("Type name '{0}' contains lowercase letters", "Class1"),
Severity = DiagnosticSeverity.Warning,
Locations = new[] {
new DiagnosticResultLocation("Test0.cs", 11, 15)
}
};
VerifyCSharpDiagnostic(test, expected);
- Run the unit test again and note that the test still fails, but now it fails due to a difference in the location (column number) at which the diagnostic was reported.

- Edit the diagnostic location to use the correct expected column number and rerun the test - verify that the test passes now.
new DiagnosticResultLocation("Test0.cs", 11, 7)
- Edit TestMethod1 and change the test code to rename Class1 to CLASS1:
var test = @"class CLASS1 { }";
- Run the unit test again and verify that the test fails now due to a diagnostic mismatch assert - expected "1" actual "0".

- Edit TestMethod1 to remove the expected diagnostic and verify the test passes:
var test = @"class CLASS1 { }";
VerifyCSharpDiagnostic(test);
推薦閱讀
- DevOps with Kubernetes
- 程序員面試白皮書
- Beginning C++ Game Programming
- Java Web應(yīng)用開發(fā)技術(shù)與案例教程(第2版)
- Symfony2 Essentials
- 焊接機(jī)器人系統(tǒng)操作、編程與維護(hù)
- 微服務(wù)架構(gòu)深度解析:原理、實(shí)踐與進(jìn)階
- Android傳感器開發(fā)與智能設(shè)備案例實(shí)戰(zhàn)
- 區(qū)塊鏈項(xiàng)目開發(fā)指南
- OpenCV Android開發(fā)實(shí)戰(zhàn)
- SEO教程:搜索引擎優(yōu)化入門與進(jìn)階(第3版)
- Java程序設(shè)計(jì)實(shí)用教程(第2版)
- SQL Server實(shí)例教程(2008版)
- 游戲設(shè)計(jì)的底層邏輯
- HikariCP數(shù)據(jù)庫連接池實(shí)戰(zhàn)