How to use RandomRangedNumberCustomization in Autofixture to ensure that parameters are in a specific range?

I have a class that takes two parameters, as shown:

    public double X { get; private set; }

    public double Y { get; private set; }

    public Point(double x, double y)
    {
        if (x > 90 || x < -90)
            throw new ArgumentOutOfRangeException("latitude");

        if (y > 180 || y < -180)
            throw new ArgumentOutOfRangeException("longitude");

        X = x;
        Y = y;
    }

The corresponding properties are set in the constructor, so I need to tell AutoFixture to create a Point class with parameters in the range specified in the guard clauses. I managed to confuse the use of the RandomRangedNumberCustomization class a bit. I have done the following:

        var xRange = new RangedNumberRequest(typeof(double), -90.0, 90.0);
        var yRange = new RangedNumberRequest(typeof (double), -180.0, 180.0);
        var dummyContext = new DelegatingSpecimenContext();
        var generator = new RandomRangedNumberGenerator();
        var x = (double)generator.Create(latitudeRange, dummyContext);
        var y = (double) generator.Create(longitudeRange, dummyContext);

which will generate numbers in my range, so I could just create a point and feed in these generated numbers, but I am missing something in terms of customization. Any help and / or guidance would be greatly appreciated.

Thank!

+4
source share
2

, , . , x y Point :

public class PointCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Customizations.Add(new XBuilder());
        fixture.Customizations.Add(new YBuilder());
    }

    private class XBuilder : ISpecimenBuilder
    {
        public object Create(object request, ISpecimenContext context)
        {
            var pi = request as ParameterInfo;
            if (pi == null ||
                pi.Name != "x" ||
                pi.Member.DeclaringType != typeof(Point))
                return new NoSpecimen(request);

            return context.Resolve(
                new RangedNumberRequest(typeof(double), -90d, 90d));
        }
    }

    private class YBuilder : ISpecimenBuilder
    {
        public object Create(object request, ISpecimenContext context)
        {
            var pi = request as ParameterInfo;
            if (pi == null ||
                pi.Name != "y" ||
                pi.Member.DeclaringType != typeof(Point))
                return new NoSpecimen(request);

            return context.Resolve(
                new RangedNumberRequest(typeof(double), -180d, 180d));
        }
    }
}

( .)

PointCustomization, :

[Fact]
public void CreatePointDoesNotThrow()
{
    var fixture = new Fixture().Customize(new PointCustomization());
    var e = Record.Exception(() => fixture.Create<Point>());
    Assert.Null(e);
}
+4

AutoFixture. - :

public class Point
{
    public double X { get; private set; }
    public double Y { get; private set; }

    public Point(
        [Range( -90,  90)]double x,
        [Range(-180, 180)]double y)
    {
        if (x > 90 || x < -90)
            throw new ArgumentOutOfRangeException("latitude");

        if (y > 180 || y < -180)
            throw new ArgumentOutOfRangeException("longitude");

        this.X = x;
        this.Y = y;
    }
}

[Fact]
public void CreatePointDoesNotThrow()
{
    var fixture = new Fixture();
    Assert.DoesNotThrow(() => fixture.Create<Point>()); // Passes.
}

public class Point
{
    public double X { get; private set; }
    public double Y { get; private set; }

    public Point(double x, double y)
    {
        if (x > 90 || x < -90)
            throw new ArgumentOutOfRangeException("latitude");

        if (y > 180 || y < -180)
            throw new ArgumentOutOfRangeException("longitude");

        this.X = x;
        this.Y = y;
    }
}

[Fact]
public void CreatePointDoesNotThrow()
{
    var fixture = new Fixture();
    var x = new Generator<int>(fixture).First(pt => pt >  -90 && pt <  90);
    var y = new Generator<int>(fixture).First(pt => pt > -180 && pt < 180);
    fixture.Customize<Point>(c => c
        .FromFactory(() => new Point(x, y)));

    Assert.DoesNotThrow(() => fixture.Create<Point>()); // Passes.
}
+4

Source: https://habr.com/ru/post/1589012/


All Articles