Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions Dummies.UnitTests/AnyCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public void ListOfUnconstrained() {
[Fact(DisplayName = "ListOf: the count family fixes, floors, caps and ranges the size.")]
public void ListOfCountFamily() {
for (int i = 0; i < SampleCount; i++) {
Check.That(((List<int>)Any.ListOf(Any.Int32()).WithCount(5)).Count).IsEqualTo(5);
Check.That(((List<int>)Any.ListOf(Any.Int32()).Empty()).Count).IsEqualTo(0);
Check.That(((List<int>)Any.ListOf(Any.Int32()).NonEmpty()).Count).IsStrictlyGreaterThan(0);
Check.That(((List<int>)Any.ListOf(Any.Int32()).WithMinCount(3)).Count).IsGreaterOrEqualThan(3);
Check.That(((List<int>)Any.ListOf(Any.Int32()).WithMaxCount(2)).Count).IsLessOrEqualThan(2);
Check.That(Any.ListOf(Any.Int32()).WithCount(5).Generate().Count).IsEqualTo(5);
Check.That(Any.ListOf(Any.Int32()).Empty().Generate().Count).IsEqualTo(0);
Check.That(Any.ListOf(Any.Int32()).NonEmpty().Generate().Count).IsStrictlyGreaterThan(0);
Check.That(Any.ListOf(Any.Int32()).WithMinCount(3).Generate().Count).IsGreaterOrEqualThan(3);
Check.That(Any.ListOf(Any.Int32()).WithMaxCount(2).Generate().Count).IsLessOrEqualThan(2);

int ranged = ((List<int>)Any.ListOf(Any.Int32()).WithCountBetween(4, 6)).Count;
int ranged = Any.ListOf(Any.Int32()).WithCountBetween(4, 6).Generate().Count;
Check.That(ranged is >= 4 and <= 6).IsTrue();
}
}
Expand All @@ -71,7 +71,7 @@ public void ListOfCountValidation() {
[Fact(DisplayName = "Distinct: a wide-domain distinct list holds only distinct elements.")]
public void DistinctOverAWideDomain() {
for (int i = 0; i < SampleCount; i++) {
List<int> list = Any.ListOf(Any.Int32().Between(1, 1000)).WithCount(20).Distinct();
List<int> list = Any.ListOf(Any.Int32().Between(1, 1000)).WithCount(20).Distinct().Generate();
Check.That(list.Count).IsEqualTo(20);
Check.That(new HashSet<int>(list).Count).IsEqualTo(20);
}
Expand Down Expand Up @@ -102,7 +102,7 @@ public void DistinctFallbackThrowsAtGeneration() {
[Fact(DisplayName = "SetOf: elements are always distinct and drawn from the item generator.")]
public void SetOfIsDistinct() {
for (int i = 0; i < SampleCount; i++) {
HashSet<int> set = Any.SetOf(Any.Int32().Between(1, 500)).WithCount(10);
HashSet<int> set = Any.SetOf(Any.Int32().Between(1, 500)).WithCount(10).Generate();
Check.That(set.Count).IsEqualTo(10);
Check.That(set).ContainsOnlyElementsThatMatch(value => value is >= 1 and <= 500);
}
Expand All @@ -113,7 +113,7 @@ public void SetOfHonoursAComparer() {
IEqualityComparer<int> modTen = new ModuloComparer(10);

for (int i = 0; i < SampleCount; i++) {
HashSet<int> set = Any.SetOf(Any.Int32().Between(0, 999), modTen).WithCount(5);
HashSet<int> set = Any.SetOf(Any.Int32().Between(0, 999), modTen).WithCount(5).Generate();
Check.That(set.Count).IsEqualTo(5);
List<int> classes = set.Select(value => value % 10).ToList();
Check.That(classes.Count).IsEqualTo(new HashSet<int>(classes).Count);
Expand All @@ -127,7 +127,7 @@ public void SetOfHonoursAComparer() {
[Fact(DisplayName = "Containing: a required value is present, and a distinct duplicate requirement conflicts.")]
public void ContainingPlacesValues() {
for (int i = 0; i < SampleCount; i++) {
List<int> list = Any.ListOf(Any.Int32().Between(1, 9)).WithCount(5).Containing(777);
List<int> list = Any.ListOf(Any.Int32().Between(1, 9)).WithCount(5).Containing(777).Generate();
Check.That(list).Contains(777);
Check.That(list.Count).IsEqualTo(5);
}
Expand All @@ -142,15 +142,15 @@ public void ContainingPlacesValues() {
[Fact(DisplayName = "Containing: a value drawn from a generator is forced into the collection.")]
public void ContainingFromAGenerator() {
for (int i = 0; i < SampleCount; i++) {
List<int> list = Any.ListOf(Any.Int32().Between(1, 9)).NonEmpty().ContainingAny(Any.Int32().OneOf(4242));
List<int> list = Any.ListOf(Any.Int32().Between(1, 9)).NonEmpty().ContainingAny(Any.Int32().OneOf(4242)).Generate();
Check.That(list).Contains(4242);
}
}

[Fact(DisplayName = "ArrayOf: produces an array of the requested size, distinct when asked.")]
public void ArrayOfProducesArrays() {
for (int i = 0; i < SampleCount; i++) {
int[] array = Any.ArrayOf(Any.Int32().Between(1, 100)).WithCount(6).Distinct();
int[] array = Any.ArrayOf(Any.Int32().Between(1, 100)).WithCount(6).Distinct().Generate();
Check.That(array.Length).IsEqualTo(6);
Check.That(new HashSet<int>(array).Count).IsEqualTo(6);
}
Expand All @@ -169,7 +169,7 @@ public void SequenceOfIsMaterialized() {
[Fact(DisplayName = "DictionaryOf: builds unique-keyed dictionaries and gates the count by the key domain.")]
public void DictionaryOfBehaves() {
for (int i = 0; i < SampleCount; i++) {
Dictionary<int, string> dictionary = Any.DictionaryOf(Any.Int32().Between(1, 1000), Any.String().NonEmpty()).WithCount(8);
Dictionary<int, string> dictionary = Any.DictionaryOf(Any.Int32().Between(1, 1000), Any.String().NonEmpty()).WithCount(8).Generate();
Check.That(dictionary.Count).IsEqualTo(8);
Check.That(dictionary.Values).ContainsOnlyElementsThatMatch(value => value.Length > 0);
}
Expand All @@ -194,13 +194,13 @@ public void PairAndTriple() {

[Fact(DisplayName = "Collections are reproducible when their element generator draws from a seeded context.")]
public void CollectionsAreReproducible() {
HashSet<int> first = Any.SetOf(Any.WithSeed(4242).Int32()).WithCount(6);
HashSet<int> second = Any.SetOf(Any.WithSeed(4242).Int32()).WithCount(6);
HashSet<int> first = Any.SetOf(Any.WithSeed(4242).Int32()).WithCount(6).Generate();
HashSet<int> second = Any.SetOf(Any.WithSeed(4242).Int32()).WithCount(6).Generate();

Check.That(second.OrderBy(value => value)).ContainsExactly(first.OrderBy(value => value));

List<int> listOne = Any.ListOf(Any.WithSeed(7).Int32().Between(0, 99)).WithCount(5);
List<int> listTwo = Any.ListOf(Any.WithSeed(7).Int32().Between(0, 99)).WithCount(5);
List<int> listOne = Any.ListOf(Any.WithSeed(7).Int32().Between(0, 99)).WithCount(5).Generate();
List<int> listTwo = Any.ListOf(Any.WithSeed(7).Int32().Between(0, 99)).WithCount(5).Generate();
Check.That(listTwo).ContainsExactly(listOne);
}

Expand Down
6 changes: 3 additions & 3 deletions Dummies.UnitTests/AnyContinuousTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ public void DecimalBehaves() {

[Fact(DisplayName = "Continuous generators convert implicitly to their value type.")]
public void ImplicitConversions() {
double d = Any.Double().Between(1d, 2d);
float f = Any.Single().Between(1f, 2f);
decimal m = Any.Decimal().Between(1m, 2m);
double d = Any.Double().Between(1d, 2d).Generate();
float f = Any.Single().Between(1f, 2f).Generate();
decimal m = Any.Decimal().Between(1m, 2m).Generate();

Check.That(d).IsGreaterOrEqualThan(1d);
Check.That(f).IsGreaterOrEqualThan(1f);
Expand Down
2 changes: 1 addition & 1 deletion Dummies.UnitTests/AnySetTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void BoolBehaves() {
Check.That(conflict.Message).Contains("False()");
Check.That(conflict.Message).Contains("True()");

bool value = Any.Bool().True();
bool value = Any.Bool().True().Generate();
Check.That(value).IsTrue();
}

Expand Down
6 changes: 3 additions & 3 deletions Dummies.UnitTests/AnySignedIntegerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public void Int64ExtremesAndArguments() {

[Fact(DisplayName = "Signed integers convert implicitly to their value type.")]
public void ImplicitConversions() {
sbyte small = Any.SByte().Positive();
short mid = Any.Int16().Negative();
long wide = Any.Int64().Between(1L, 10L);
sbyte small = Any.SByte().Positive().Generate();
short mid = Any.Int16().Negative().Generate();
long wide = Any.Int64().Between(1L, 10L).Generate();

Check.That((int)small).IsStrictlyGreaterThan(0);
Check.That((int)mid).IsStrictlyLessThan(0);
Expand Down
8 changes: 4 additions & 4 deletions Dummies.UnitTests/AnyUnsignedIntegerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public void UInt64ExtremesAndSets() {

[Fact(DisplayName = "Unsigned integers convert implicitly to their value type.")]
public void ImplicitConversions() {
byte tiny = Any.Byte().Between(1, 10);
ushort mid = Any.UInt16().NonZero();
uint wide = Any.UInt32().Between(1u, 10u);
ulong huge = Any.UInt64().Between(1UL, 10UL);
byte tiny = Any.Byte().Between(1, 10).Generate();
ushort mid = Any.UInt16().NonZero().Generate();
uint wide = Any.UInt32().Between(1u, 10u).Generate();
ulong huge = Any.UInt64().Between(1UL, 10UL).Generate();

Check.That((int)tiny).IsGreaterOrEqualThan(1);
Check.That((int)mid).IsStrictlyGreaterThan(0);
Expand Down
52 changes: 0 additions & 52 deletions Dummies.UnitTests/ImplicitConversionTests.cs

This file was deleted.

68 changes: 68 additions & 0 deletions Dummies.UnitTests/MaterializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#region Usings declarations

using JetBrains.Annotations;

using NFluent;

#endregion

namespace Dummies.UnitTests;

[TestSubject(typeof(Any))]
public sealed class MaterializationTests {

#region Statics members declarations

private static T Materialize<T>(IAny<T> generator) {
return generator.Generate();
}

#endregion

[Fact(DisplayName = "Generate materializes a valid string value.")]
public void GenerateMaterializesAString() {
string value = Any.String().NonEmpty().Generate();

Check.That(value).IsNotEmpty();
}

[Fact(DisplayName = "Generate materializes a valid int value.")]
public void GenerateMaterializesAnInt() {
int value = Any.Int32().Positive().Generate();

Check.That(value).IsStrictlyGreaterThan(0);
}

[Fact(DisplayName = "A materialized value flows into a method expecting the generated type.")]
public void GeneratedValueFlowsIntoACallSite() {
static int Measure(string text) {
return text.Length;
}

int length = Measure(Any.String().WithLength(9).Generate());

Check.That(length).IsEqualTo(9);
}

[Fact(DisplayName = "Each Generate call draws a fresh value.")]
public void EachGenerateDrawsAFreshValue() {
AnyInt32 generator = Any.Int32().Between(0, int.MaxValue);

HashSet<int> seen = new();
for (int i = 0; i < 20; i++) {
seen.Add(generator.Generate());
}

Check.That(seen.Count).IsStrictlyGreaterThan(1);
}

[Fact(DisplayName = "Generic inference flows through IAny<T>, materializing without any implicit conversion.")]
public void GenericInferenceMaterializesThroughIAny() {
string text = Materialize(Any.String().NonEmpty());
int value = Materialize(Any.Int32().Positive());

Check.That(text).IsNotEmpty();
Check.That(value).IsStrictlyGreaterThan(0);
}

}
44 changes: 22 additions & 22 deletions Dummies.UnitTests/SeedReproducibilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ public sealed class SeedReproducibilityTests {
private static string Batch() {
// Explicitly typed locals: string.Join(params object[]) would otherwise box the generators and
// call their ToString() instead of triggering the implicit conversions.
int full = Any.Int32();
int bounded = Any.Int32().Between(1, 1000);
string free = Any.String();
string capped = Any.String().NonEmpty().WithMaxLength(50);
string shaped = Any.String().StartingWith("ORD-").WithLength(12);
long wide = Any.Int64();
ulong unsigned = Any.UInt64();
double real = Any.Double().Between(0d, 1000d);
decimal exact = Any.Decimal().Between(0m, 1000m);
bool flag = Any.Bool();
Guid id = Any.Guid();
char letter = Any.Char();
TimeSpan span = Any.TimeSpan();
DateTime instant = Any.DateTime();
Int128 huge = Any.Int128();
Half tiny = Any.Half();
List<int> list = Any.ListOf(Any.Int32().Between(0, 9)).WithCount(4);
HashSet<int> set = Any.SetOf(Any.Int32().Between(0, 99)).WithCount(3);
int full = Any.Int32().Generate();
int bounded = Any.Int32().Between(1, 1000).Generate();
string free = Any.String().Generate();
string capped = Any.String().NonEmpty().WithMaxLength(50).Generate();
string shaped = Any.String().StartingWith("ORD-").WithLength(12).Generate();
long wide = Any.Int64().Generate();
ulong unsigned = Any.UInt64().Generate();
double real = Any.Double().Between(0d, 1000d).Generate();
decimal exact = Any.Decimal().Between(0m, 1000m).Generate();
bool flag = Any.Bool().Generate();
Guid id = Any.Guid().Generate();
char letter = Any.Char().Generate();
TimeSpan span = Any.TimeSpan().Generate();
DateTime instant = Any.DateTime().Generate();
Int128 huge = Any.Int128().Generate();
Half tiny = Any.Half().Generate();
List<int> list = Any.ListOf(Any.Int32().Between(0, 9)).WithCount(4).Generate();
HashSet<int> set = Any.SetOf(Any.Int32().Between(0, 99)).WithCount(3).Generate();
int? maybe = Any.Int32().Between(0, 9).OrNull().Generate();

return string.Join("|", full, bounded, free, capped, shaped,
Expand All @@ -50,8 +50,8 @@ public void SameSeedContextsAgree() {
AnyContext any1 = Any.WithSeed(12345);
AnyContext any2 = Any.WithSeed(12345);

string value1 = any1.String().NonEmpty();
string value2 = any2.String().NonEmpty();
string value1 = any1.String().NonEmpty().Generate();
string value2 = any2.String().NonEmpty().Generate();

Check.That(value2).IsEqualTo(value1);
}
Expand All @@ -78,12 +78,12 @@ public void DifferentSeedContextsDiverge() {
[Fact(DisplayName = "A context is isolated from the ambient source: interleaved ambient draws do not shift it.")]
public void ContextIsIsolatedFromAmbientDraws() {
AnyContext quiet = Any.WithSeed(31415);
string undisturbed = quiet.String().WithLength(10);
string undisturbed = quiet.String().WithLength(10).Generate();

AnyContext interleaved = Any.WithSeed(31415);
Any.String().Generate();
Any.Int32().Generate();
string disturbed = interleaved.String().WithLength(10);
string disturbed = interleaved.String().WithLength(10).Generate();

Check.That(disturbed).IsEqualTo(undisturbed);
}
Expand Down
2 changes: 1 addition & 1 deletion Dummies/Any.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/// <example>
/// <code>
/// // The reference format is the invariant; the exact value is irrelevant — so it is Any.
/// string reference = Any.String().StartingWith("ORD-").WithLength(12);
/// string reference = Any.String().StartingWith("ORD-").WithLength(12).Generate();
///
/// // Turn a constrained primitive into a value object, without reflection:
/// OrderReference order = Any.String().StartingWith("ORD-").WithLength(12)
Expand Down Expand Up @@ -413,7 +413,7 @@
/// <typeparam name="TResult">The type of the composed value.</typeparam>
/// <returns>A generator of the composed value.</returns>
/// <exception cref="ArgumentNullException">Thrown when any argument is <c>null</c>.</exception>
public static IAny<TResult> Combine<T1, T2, T3, TResult>(IAny<T1> first, IAny<T2> second, IAny<T3> third, Func<T1, T2, T3, TResult> compose) {

Check warning on line 416 in Dummies/Any.cs

View workflow job for this annotation

GitHub Actions / SonarQube Cloud analysis

Reduce the number of generic parameters in the 'Any.Combine' method to no more than the 3 authorized.
if (first is null) { throw new ArgumentNullException(nameof(first)); }
if (second is null) { throw new ArgumentNullException(nameof(second)); }
if (third is null) { throw new ArgumentNullException(nameof(third)); }
Expand Down Expand Up @@ -445,7 +445,7 @@
/// <typeparam name="TResult">The type of the composed value.</typeparam>
/// <returns>A generator of the composed value.</returns>
/// <exception cref="ArgumentNullException">Thrown when any argument is <c>null</c>.</exception>
public static IAny<TResult> Combine<T1, T2, T3, T4, TResult>(IAny<T1> first, IAny<T2> second, IAny<T3> third, IAny<T4> fourth, Func<T1, T2, T3, T4, TResult> compose) {

Check warning on line 448 in Dummies/Any.cs

View workflow job for this annotation

GitHub Actions / SonarQube Cloud analysis

Reduce the number of generic parameters in the 'Any.Combine' method to no more than the 3 authorized.
if (first is null) { throw new ArgumentNullException(nameof(first)); }
if (second is null) { throw new ArgumentNullException(nameof(second)); }
if (third is null) { throw new ArgumentNullException(nameof(third)); }
Expand Down Expand Up @@ -481,7 +481,7 @@
/// <typeparam name="TResult">The type of the composed value.</typeparam>
/// <returns>A generator of the composed value.</returns>
/// <exception cref="ArgumentNullException">Thrown when any argument is <c>null</c>.</exception>
public static IAny<TResult> Combine<T1, T2, T3, T4, T5, TResult>(IAny<T1> first, IAny<T2> second, IAny<T3> third, IAny<T4> fourth, IAny<T5> fifth, Func<T1, T2, T3, T4, T5, TResult> compose) {

Check warning on line 484 in Dummies/Any.cs

View workflow job for this annotation

GitHub Actions / SonarQube Cloud analysis

Reduce the number of generic parameters in the 'Any.Combine' method to no more than the 3 authorized.
if (first is null) { throw new ArgumentNullException(nameof(first)); }
if (second is null) { throw new ArgumentNullException(nameof(second)); }
if (third is null) { throw new ArgumentNullException(nameof(third)); }
Expand Down
14 changes: 0 additions & 14 deletions Dummies/AnyArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@ namespace Dummies;
/// <typeparam name="T">The element type.</typeparam>
public sealed class AnyArray<T> : AnyCollection<T, T[], AnyArray<T>> {

#region Statics members declarations

/// <summary>
/// Generates the value — an <see cref="AnyArray{T}" /> can be used wherever a <c>T[]</c> is expected. Each
/// conversion draws a fresh array.
/// </summary>
/// <param name="generator">The generator to draw from.</param>
/// <returns>An arbitrary array satisfying the generator's constraints.</returns>
public static implicit operator T[](AnyArray<T> generator) {
return generator.Generate();
}

#endregion

internal AnyArray(RandomSource? source, CollectionState<T> state) : base(source, state) { }

/// <summary>Requires the elements to be pairwise distinct (default equality).</summary>
Expand Down
10 changes: 0 additions & 10 deletions Dummies/AnyBool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ public sealed class AnyBool : IAny<bool>, IHasRandomSource, ICardinalityHint {

#region Statics members declarations

/// <summary>
/// Generates the value — an <see cref="AnyBool" /> can be used wherever a <see cref="bool" /> is expected.
/// Each conversion draws a fresh value.
/// </summary>
/// <param name="generator">The generator to draw from.</param>
/// <returns>An arbitrary value satisfying the generator's constraints.</returns>
public static implicit operator bool(AnyBool generator) {
return generator.Generate();
}

internal static AnyBool Create(RandomSource source) {
return new AnyBool(source, null, null);
}
Expand Down
Loading