GithubHelp home page GithubHelp logo

isabella232 / roslynnunitlight Goto Github PK

View Code? Open in Web Editor NEW

This project forked from telerik-boneyard/roslynnunitlight

0.0 0.0 0.0 712 KB

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

License: Other

C# 89.57% PowerShell 10.43%

roslynnunitlight's Introduction

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);
}

roslynnunitlight's People

Contributors

dustincampbell avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.