LiteSpecs 0.1.0

Install-Package LiteSpecs -Version 0.1.0
dotnet add package LiteSpecs --version 0.1.0
<PackageReference Include="LiteSpecs" Version="0.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LiteSpecs --version 0.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: LiteSpecs, 0.1.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install LiteSpecs as a Cake Addin
#addin nuget:?package=LiteSpecs&version=0.1.0

// Install LiteSpecs as a Cake Tool
#tool nuget:?package=LiteSpecs&version=0.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Sample

Define some specifications. Either generic or typed:

static class TestSpecs
{
    static readonly ISpecification<int> AllNumbers = Specification<int>.All;

    static readonly ISpecification<int> OddNumbers = Specification.Generic<int>(
        i => i % 2 != 0,
        "Not an odd integer.");

    static readonly ISpecification<int> Ones = Specification.Generic<int>(
        i => i == 1,
        "Not a 1.");

    static readonly ISpecification<int> Threes = Specification.Generic<int>(
        i => i == 3,
        "Not a 3.");

    static readonly ISpecification<VehicleObservation> CanBeObservationOfMotorcycle
        = new CanBeObservationOfMotorcycle();
    
    static readonly ISpecification<VehicleObservation> CanBeObservationOfCar
        = new CanBeObservationOfCar();
    
    static readonly ISpecification<VehicleObservation> CanBeObservationOfCar2
        = Specification.Generic<VehicleObservation>(i =>
        {
            if (!i.HasEngine)
                return SpecificationIs.NotSatisfied("A car must have an engine.");

            if (i.WheelCount != 4)
                return SpecificationIs.NotSatisfied("A car must have 4 wheels.");

            return SpecificationIs.Satisfied;
        });
}

public class CanBeObservationOfCar : Specification<VehicleObservation>
{
    public CanBeObservationOfCar() : base(o =>
    {
        if (!o.HasEngine)
            return SpecificationIs.NotSatisfied("A car must have an engine.");

        if (o.WheelCount != 4)
            return SpecificationIs.NotSatisfied("A car must have 4 wheels.");

        return SpecificationIs.Satisfied;
    })
    { }
}

public class CanBeObservationOfMotorcycle : Specification<VehicleObservation>
{
    public CanBeObservationOfMotorcycle() : base(
        o => o.HasEngine && o.WheelCount == 2,
        "It can not be a motorcycle.")
    { }
}

Use them:

[Fact]
public void Can_combine_specs_to_filter_enumerables()
{
    var combined = TestSpecs
        .AllNumbers
        .AndAlso(TestSpecs.OddNumbers)
        .And(TestSpecs.Ones.Or(TestSpecs.Threes));

    var onesAndThrees = new[] { 1, 2, 3, 4 }.Matching(combined).ToArray();

    onesAndThrees.Should().BeEquivalentTo(new[] { 1, 3 });
}

[Theory]
[InlineData(2)]
[InlineData(4)]
public void Or_Should_provide_a_reason_When_not_satisfied(int value)
{
    var result = TestSpecs.Ones.Or(TestSpecs.Threes).Eval(value);

    result.IsSatisfied.Should().BeFalse();
    result.Reasons.Should().BeEquivalentTo("Not a 1.", "Not a 3.");
}

[Fact]
public void Can_handle_non_satisfying_evaluations()
{
    var r = TestSpecs.CanBeObservationOfCar.Eval(_fourWheelerWithoutEngine);
    r.IsSatisfied.Should().BeFalse();
    r.Reasons.Should().BeEquivalentTo("A car must have an engine.");
}
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETStandard 2.0

    • 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.1.0 652 11/22/2017