Skip to content

0.5.0

Compare
Choose a tag to compare
@nth-commit nth-commit released this 18 Nov 19:15

BREAKING: Fix Gen.Byte().GreaterThan() types

This method was erroneously accepting an int, rather than a byte. Fixed now.

Add Gen.Nullable<T>()

Added a higher-order generator which takes a generator of any reference type, and returns a generator that produces values from the source generator, or produces nulls.

Gen.Nullable(Gen.String()); // "a", "aa", "a ", null, "ab" ...
Gen.String().OrNull(); // alternative syntax 

Gen.Create() now handles IEnumerables

IEnumerable<T> is now wired up to the reflection-based generator, Gen.Create<T>(). At the moment, it produces values that are lists of a limited size, but in the future, we may decide to leverage Gen.Infinite<T> - the generator which produces IEnumerable<T>s of infinite size.

That means that we can generate structures like this:

public class House
{
    public Address Address { get; set; }

    public IEnumerable<Room> Rooms { get; set; } // Previously this property was unsupported
}

Gen.Create<House>();

It also means IEnumerables are now supported in injected properties:

[Property]
public void SortIdempotency(IEnumerable<int> xs)
{
    Assert.Equal(xs.Sort(), xs.Sort().Sort());
}

GreaterThan()/LessThan() integer-generating builder methods now error on numeric overflow

Previously, a generator defined as Gen.Int32().GreaterThan(int.MaxValue) would silently cause a numeric overflow, and create a generator equivalent to Gen.Int32().GreaterThanEqual(int.MinValue).

Now, GalaxyCheck detects overflow in these methods, and will produce an error instead.

This is currently supported for the generators Gen.Byte(), Gen.Int16(), Gen.Int32(), Gen.Int64(). In the future, other (less likely to overflow) methods will have similar checks, such as Gen.String().WithCountGreaterThan().

Protect the inferred maximum count of the List<T> generator from overflowing

When you provide the list generator a really big minimum, and an unconstrained maximum e.g. Gen.List().WithCountGreaterThanEqual(int.MaxValue), it would previously return the error Error while running generator ListGen: 'maxCount' cannot be negative. This is because, if unspecified, internally in the generator, we infer the maxCount to be minCount + 20, and we weren't checking for integer overflow.

Now, we clamp the maxCount to int.MaxValue to prevent overflow. Out-of-the-box, this will result in a much clearer error message:

GalaxyCheck.Exceptions+GenErrorException : Error while running generator ListGen: Count limit exceeded. This is a built-in safety mechanism to prevent hanging tests. If generating a list with over 1000 elements was intended, relax this constraint by calling IListGen.DisableCountLimitUnsafe().

Generating a list with int.MaxValue items is probably not intended, so the error now gives a better hint towards what has gone wrong.