ALCTestKit 1.0.0

dotnet add package ALCTestKit --version 1.0.0                
NuGet\Install-Package ALCTestKit -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ALCTestKit" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ALCTestKit --version 1.0.0                
#r "nuget: ALCTestKit, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ALCTestKit as a Cake Addin
#addin nuget:?package=ALCTestKit&version=1.0.0

// Install ALCTestKit as a Cake Tool
#tool nuget:?package=ALCTestKit&version=1.0.0                

ALCTestKit

A lightweight framework for writing unit tests for AL diagnostic analyzers, code fixes, refactorings and completion providers. This is fork of RoslynTestKit which is a port of RoslynNUnitLight.NetStandard.

Quick Start

  1. Install the ALCTestKit package from NuGet into your project.
  2. Create appropriate test fixture using RoslynFixtureFactory
  3. Fix the fixture to perform assertion!

Code location markers

ALCTestKit 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. Instead of the markers you can also provide line number to locate the place of expected diagnostic.


The now following text is not updated for ALTestKit yet. Feel free to create contributions to improve this document.


Framework dependencies

By default RoslynTestKit adds to the compilation the following references:

  • mscorlib.dll (and its dependencies)
  • System.Private.CoreLib.dll
  • System.Linq.dll
  • System.Linq.Expression.dll

You can change that behavior by overriding CreateFrameworkMetadataReferences() method. You can also take full control of how the workspace/compilation is created by overriding :

Document CreateDocumentFromCode(string code, string languageName, IReadOnlyCollection<MetadataReference> extraReferences)

External dependencies

Every *TestFixture can be configured to import external dependencies required by the test case code/markup. There is also a couple of helper methods in ReferenceSource class that allow to easily define these dependencies. A sample setup for analyzer test with external dependencies can looks as follows:

public class SampleTests
{

    [Test]
    public void should_verify_analyzer()
    {
        var fixture = RoslynFixtureFactory.Create<SampleAnalyzer>(new () {
            References = new []{
                ReferenceSource.FromType<ReaderWriterLock>() 
            };
        });

        //TODO: test your analyzer
    }
}
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;
	}
}";

    var fixture = RoslynFixtureFactory.Create<UseGetterOnlyAutoPropertyAnalyzer>();

    fixture.HasDiagnostic(code, DiagnosticIds.UseGetterOnlyAutoProperty);
}
Example: Override test project and test document names
[Test]
public void AutoPropDeclaredAndUsedInConstructor()
{
    const string markup = @"
class C
{
	public bool MyProperty { get; [|private set;|] }
	public C(bool f)
	{
		MyProperty = f;
	}
}";

    var fixture = RoslynFixtureFactory.Create<UseGetterOnlyAutoPropertyAnalyzer>();

    var document = this.CreateDocumentFromMarkup(markup, "MySampleProject", "MySampleDocument");
    var diagnosticLocation = this.GetMarkerLocation(markup);
    fixture.HasDiagnostic(document, DiagnosticIds.UseGetterOnlyAutoProperty, diagnosticLocation);
}
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;
    }
}";

    var fixture = RoslynFixtureFactory.Create<UseGetterOnlyAutoPropertyAnalyzer>();
    fixture.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; }
}";

    var fixture = RoslynFixtureFactory.Create<UseGetterOnlyAutoPropertyCodeFix>();
    fixture.TestCodeFix(markupCode, expected, DiagnosticDescriptors.UseGetterOnlyAutoProperty);
}

Instead of the diagnostic descriptor, you can also use Diagnostic Id (error code) to identify the issue which should be fixed by tested code fix. This allows testing code fixes which respond to standard C# compiler errors such as CS0736.

Example: Test code fix that fixes issue reported by provided DiagnosticAnalyzer

public class SampleTest
{   

    [Test]
    public void should_be_able_fix_issue_reported_by_analyzer()
    {

        var fixture = RoslynFixtureFactory.Create<UseGetterOnlyAutoPropertyCodeFix>(new ()
        {
            AdditionalAnalyzers = new [] {
                new[] { new UseGetterOnlyAutoPropertyAnalyzer()
            }
        });

        fixture.TestCodeFix(/*Here comes code with issue */, /*Here comes fixed code*/, /*Diagnostic Id*/);
    }
}
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}"";
    }
}";

    var fixture = RoslynFixtureFactory.Create<SampleCodeRefactoringProvider>();
    fixture.TestCodeRefactoring(markupCode, expected);
}
Example: Test completion provider based on expected suggestions
[Test]
public void SimpleTest()
{
    const string markupCode = @"
class C
{
    void M()
    {
        var s = string.Format([||], 42);
    }
}";

    var fixture = RoslynFixtureFactory.Create<SampleCompletionProvider>();

    fixture.TestCompletion(markupCode, new []
    {
        "first expected suggestion",
        "second expected suggestion"
    });
}
Example: Test completion provider based on custom checks
[Test]
public void SimpleTest()
{
    const string markupCode = @"
class C
{
    void M()
    {
        var s = string.Format([||], 42);
    }
}";

    var fixture = RoslynFixtureFactory.Create<SampleCompletionProvider>();

    fixture.TestCompletion(markupCode, (ImmutableArray<CompletionItem> suggestions) =>
    {
        //TODO: Custom assertions
    });
}

Code comparison

In case of discrepancy between the expected code and the generated one, when testing CodeFixes and CodeRefactorings, the TransformedCodeDifferentThanExpectedException is thrown. The Text difference is presented in the console using inline diff format, which looks as follows:

RoslynTestKit.TransformedCodeDifferentThanExpectedException : Transformed code is different than expected:
===========================
From line 25:
- ················ZipCode·=·src.MainAddress.ZipCode,␍␊
===========================
From line 29:
- ············dst.Addresses·=·src.Addresses.ConvertAll(srcAddress·=>·new·AddressDTO()␍␊
- ············{␍␊
- ················City·=·srcAddress.City,␍␊
- ················ZipCode·=·srcAddress.ZipCode,␍␊
- ················Street·=·srcAddress.Street,␍␊
- ················FlatNo·=·srcAddress.FlatNo,␍␊
- ················BuildingNo·=·srcAddress.BuildingNo␍␊
- ············}).AsReadOnly();␍␊
- ············dst.UnitId·=·src.Unit.Id;␍␊
===========================
From line 71:
- ········public·string·ZipCode·{·get;·set;·}␍␊
+ ········public·string·ZipCode·{·get;·}␍␊
===========================
From line 94:
- ········public·List<AddressEntity>·Addresses·{·get;·set;·}␍␊
- ········public·UnitEntity·Unit·{·get;·set;·}␍␊
===========================
From line 124:
- ········public·string·BankName·{·get;·set;·}␍␊
+ ············public·string·BankName·{·get;·set;·}␍␊
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 130 7/15/2024