Test.CSharp.ModelAnalyzer 0.0.10

There is a newer version of this package available.
See the version list below for details.
dotnet add package Test.CSharp.ModelAnalyzer --version 0.0.10
                    
NuGet\Install-Package Test.CSharp.ModelAnalyzer -Version 0.0.10
                    
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="Test.CSharp.ModelAnalyzer" Version="0.0.10">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Test.CSharp.ModelAnalyzer" Version="0.0.10" />
                    
Directory.Packages.props
<PackageReference Include="Test.CSharp.ModelAnalyzer">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Test.CSharp.ModelAnalyzer --version 0.0.10
                    
#r "nuget: Test.CSharp.ModelAnalyzer, 0.0.10"
                    
#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.
#:package Test.CSharp.ModelAnalyzer@0.0.10
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Test.CSharp.ModelAnalyzer&version=0.0.10
                    
Install as a Cake Addin
#tool nuget:?package=Test.CSharp.ModelAnalyzer&version=0.0.10
                    
Install as a Cake Tool

ModelAnalyzer

Roslyn-based analysis of class models with informal contracts.

Build status codecov CodeFactor NuGet

How to install

To install this analyzer, in Visual Studio:

  • Open menu ToolsNuGet Package ManagerManage NuGet Packages for Solution. The NuGet - Solution window appears.
  • In the top right corner, make sure Package Source is selected to be either nuget.org or All.
  • Click the Browse tab and in the search prompt type ModelAnalyzer.
  • A list of packages appears, one one them called CSharp.ModelAnalyzer.
  • Click to select this package and in the right pane check projects you want to be analyzed.
  • Click the Install button.

How to uninstall

To uninstall this analyzer, in Visual Studio:

  • Open menu ToolsNuGet Package ManagerManage NuGet Packages for Solution. The NuGet - Solution window appears.
  • Click to select the CSharp.ModelAnalyzer package and in the right pane uncheck projects you no longer want to be analyzed.
  • Click the Uninstall button.

Model Analysis

This analyzer looks for inconsistencies by creating a model of a class from the source code. The model is basically a big mathematical proof of consistency. The proof is then analyzed by a theorem solver, and if found true it proves the source is consistent (in some abstract sense).

Caveat

A big caveat to what might look like magic is that this analyzer only supports a very restricted subset of C# (see below for a list). Anything that is not supported prevents the analysis from starting and displays a warning.

Contracts

To help the analyzer, programmers can add contract clauses:

  • A require clause puts some requirement to the input of a method.
  • An ensure clause provides some guarantees when a method exits.
  • An invariant clause added to a class puts some requirement on the system after a class is initialized, and every time a method exits.

Features support

The analyzer supports:

  • Private fields of type int. Ex: private int X, Y;. Fields can only use default initialization (to zero).
  • Private or public methods that return either void or int and take zero or more int parameters.
    • Parameters are not allowed to have the same name as fields.
    • Parameters cannot be assigned, they are read-only.
  • Assignment of an expression to a field.
  • return, but at the end of a method only.
  • The if else statement.
  • A restricted subset of expressions:
    • The +, -, * and / binary operators.
    • The - unary operator.
    • Parenthesis.
    • The && and || logical operators.
    • The ==, !=, >, >=, < and <= comparison operators.
    • Integer constants (ex: 0), true and false.
    • Variables, either fields or parameters.

Everything else, attributes, preprocessor directives etc. is not supported.

Method contract

To add a require clause to a method, put a line starting with // Require: below the method declaration and before the opening brace. To add an ensure clause put a line starting with // Ensure: below the method's closing brace.

For example:

int N;

int Sum(int x, int y)
// Require: x >= 0 && x <= 256 
// Require: y >= 0 && y <= 256 
{
  N = x + y;
  return N;
}
// Ensure: N >= 0 && N <= 512

Assertions support the same subset of expressions as the code.

Class contract

To add an invariant, put a line starting with // Invariant: below the class' closing brace. For example:

public class Test
{
  int N;

  int Sum(int x, int y)
  // Require: x >= 16 && x <= 256 
  // Require: y >= 16 && y <= 256 
  {
    N = x + y;
    return N;
  }
  // Ensure: N >= 32 && N <= 512
}
// Invariant: N == 0 || (N >= 32 && N <= 512)

Suppressing warnings

If the code includes unsupported features, this is reported as one or more warnings. To remove these warnings and turn off analysis for a class, put a line starting with // No model before the class declaration. For example:

// No model
public class Test
{
  /* ... */
}

List of diagnostics

Code Diagnostic
MA0001 Invalid ensure clause
MA0002 Invalid expression
MA0003 Invalid invariant clause
MA0004 Invalid parameter
MA0005 Invalid require clause
MA0006 Invalid statement
MA0007 Error in class model
MA0008 Invariant violation
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

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
0.0.29 517 1/28/2023
0.0.28 500 1/28/2023
0.0.27 489 1/28/2023
0.0.26 543 1/2/2023
0.0.25 457 12/29/2022
0.0.24 409 12/20/2022
0.0.23 471 12/19/2022
0.0.22 420 12/19/2022
0.0.21 389 12/15/2022
0.0.20 402 12/15/2022
0.0.19 350 12/15/2022
0.0.18 377 12/14/2022
0.0.17 394 12/14/2022
0.0.16 387 12/14/2022
0.0.15 378 12/14/2022
0.0.14 373 12/14/2022
0.0.13 372 12/14/2022
0.0.12 367 12/14/2022
0.0.11 398 12/14/2022
0.0.10 368 12/14/2022
0.0.9 360 12/13/2022
0.0.8 365 12/13/2022
0.0.7 410 12/13/2022
0.0.6 410 12/13/2022
0.0.5 408 12/13/2022
0.0.4 368 12/13/2022

Initial package.