Skip to content

Andrei-/net-Assert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Assert

Little helper on assertion.

Usage

I got tired of writing the same boilerplate code for checking unexpected values over and over again, such as:

public Tile[] PaintTiles(Color color, Tile[] tiles)
{
    if (color == default(Color))
        throw new ArgumentException("color shouldn't be the default one");
    
    if (!Enum.IsDefined(typeof(Color), value))
        throw new ArgumentException("invalid color");

    if (tiles == null)
        throw new ArgumentNullException("tiles");

    if (!tiles.Any())
        throw new ArgumentException("tiles wasn't supposed to be empty");

    foreach (var tile in tiles)
    {
        // Paint the tile
    }
}

So now I do:

using AssertLibrary;

public Tile[] PaintTiles(Color color, Tile[] tiles)
{
    Assert.IsNotDefault(color);
    Assert.IsInEnum<Color>(color);

    Assert.IsNotNull(tiles);
    Assert.HasElements(tiles);

    foreach (var tile in tiles)
    {
        // Paint the tile
    }
}

The helper can check unexpected nulls, places reach, types of values, enum values, default values, property existence, true/false, equal/not equal, less/more than, positive/negative/not zero numbers, and empty/small/large/single/not single/exactly collections.

Call Assert.UseDebug() to make the library use Debug.Assert, Assert.UseTrace() to use Trace.Assert and Assert.UseException() to make it throw an AssertException.

About

Little helper on assertion.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages