A lightweight framework for writing unit tests for Roslyn diagnostic analyzers, code fixes and refactorings using NUnit,
Перейти к файлу
Svetlozar Angelov 615fa1f46c Add TestMissingCodeRefactoring method
We want tests to verify that specific refactroing is not available in some
test cases.
2015-11-13 17:11:31 +02:00
Source Add TestMissingCodeRefactoring method 2015-11-13 17:11:31 +02:00
Tools Add NuGet.exe and RoslynNUnitLight.nuspec 2015-04-23 10:04:57 -07:00
.gitattributes 🎊 Added .gitattributes & .gitignore files 2015-04-23 08:20:06 -07:00
.gitignore 🎊 Added .gitattributes & .gitignore files 2015-04-23 08:20:06 -07:00
BuildAndPackage.ps1 Update PowerShell script to specify correct portable intersection 2015-04-23 11:51:40 -07:00
Common.targets Add MSBuild targets 2015-04-23 08:25:38 -07:00
License.txt Add license.txt 2015-04-23 08:28:19 -07:00
README.md Update readme.md with quick start and unit test examples 2015-04-23 12:37:52 -07:00
RoslynNUnitLight.nuspec Update to Roslyn 1.1.0-rc1 2015-11-05 11:05:02 -08:00
RoslynNUnitLight.sln Add NuGet.exe and RoslynNUnitLight.nuspec 2015-04-23 10:04:57 -07:00

README.md

RoslynNUnitLight

A lightweight framework for writing unit tests for Roslyn diagnostic analyzers, code fixes and refactorings using NUnit.

Quick Start

  1. Install the RoslynNUnitLight package from NuGet into your project.

  2. Create a new class that inherits from one of the provided *TestFixture classes that matches what are going to test.

  3. Override the LanguageName property and return the appropriate value from Microsoft.CodeAnalysis.LanguageNames, depending on what language your tests will target.

  4. Override the CreateAnalyzer or CreateProvider method and return an instance of your analyzer or provider.

  5. Write tests!

Writing Unit Tests

RoslynNUnitLight accepts strings that are marked up with [| and |] to identify a particular span. This could represent the span of an expected diagnostic or the text selection before a refactoring is applied.

Example: Test presence of a diagnostic

[Test]
public void AutoPropDeclaredAndUsedInConstructor()
{
    const string code = @"
class C
{
	public bool MyProperty { get; [|private set;|] }
	public C(bool f)
	{
		MyProperty = f;
	}
}";

    HasDiagnostic(code, DiagnosticIds.UseGetterOnlyAutoProperty);
}

Example: Test absence of a diagnostic

[Test]
public void AutoPropAlreadyReadonly()
{
    const string code = @"
class C
{
    public bool MyProperty { get; }
    public C(bool f)
    {
        MyProperty = f;
    }
}";

    NoDiagnostic(code, DiagnosticIds.UseGetterOnlyAutoProperty);
}

Example: Test code fix behavior

[Test]
public void TestSimpleProperty()
{
    const string markupCode = @"
class C
{
    public bool P1 { get; [|private set;|] }
}";

    const string expected = @"
class C
{
    public bool P1 { get; }
}";

    TestCodeFix(markupCode, expected, DiagnosticDescriptors.UseGetterOnlyAutoProperty);
}

Example: Test code refactoring behavior

[Test]
public void SimpleTest()
{
    const string markupCode = @"
class C
{
    void M()
    {
        var s = [|string.Format(""{0}"", 42)|];
    }
}";

    const string expected = @"
class C
{
    void M()
    {
        var s = $""{42}"";
    }
}";

    TestCodeRefactoring(markupCode, expected);
}