Skip to content

Commit

Permalink
more nullable checks
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek-galezowski committed Mar 5, 2024
1 parent c861902 commit 51dd049
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AnySpecification.Fixtures;
using Core.NullableReferenceTypesExtensions;
using FluentAssertions;
using NUnit.Framework.Legacy;

Expand All @@ -14,8 +15,8 @@ public void ShouldFillPropertiesAndFieldsWhenCreatingDataStructures()

//THEN
ClassicAssert.NotNull(instance.Data);
ClassicAssert.NotNull(instance._field);
ClassicAssert.NotNull(instance.Data.Text);
ClassicAssert.NotNull(instance.Field);
ClassicAssert.NotNull(instance.Data.OrThrow().Text);
ClassicAssert.IsNotEmpty(instance.AnImmutableList);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace AnySpecification.Fixtures;

public class ConcreteDataStructure
{
public ConcreteDataStructure2 _field;
public ConcreteDataStructure2? Field;
public TimeSpan Span { get; set; }
public ConcreteDataStructure2 Data { get; set; }
public ConcreteDataStructure2? Data { get; set; }
public ImmutableList<int> AnImmutableList { get; set; } = ImmutableList<int>.Empty;
}
14 changes: 7 additions & 7 deletions src/netstandard2.0/AnySpecification/Fixtures/RecursiveClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

public class RecursiveClass
{
public RecursiveClass Same { get; set; }
public string Whatever { get; set; }
public RecursiveClass2 Other { get; set; }
public RecursiveClass2[] Others { get; set; }
public RecursiveClass? Same { get; set; }
public string? Whatever { get; set; }
public RecursiveClass2? Other { get; set; }
public RecursiveClass2[]? Others { get; set; }
}

public class ObjectWithIndirectRecursion
{
public ObjectWithIndirectRecursion2 Other2 { get; set; }
public ObjectWithIndirectRecursion2? Other2 { get; set; }
}

public class ObjectWithIndirectRecursion2
{
public ObjectWithIndirectRecursion Other { get; set; }
public ObjectWithIndirectRecursion? Other { get; set; }
}

public class RecursiveClassWithOnlyReferenceToItself
{
public RecursiveClassWithOnlyReferenceToItself Same { get; set; }
public RecursiveClassWithOnlyReferenceToItself? Same { get; set; }
}

public class RecursiveClassWithOnlyReferenceToItselfInConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AnySpecification.Fixtures;
using Core.NullableReferenceTypesExtensions;
using NUnit.Framework.Legacy;

namespace AnySpecification;
Expand All @@ -11,9 +12,9 @@ public void ShouldRespectRecursionLimitOf5ForSpecificType()
//GIVEN
var instance = Any.Instance<RecursiveClass>();

ClassicAssert.NotNull(instance.Same.Same.Same, "Last recursive element should be generated as uninitialized dummy");
ClassicAssert.Null(instance.Same.Same.Same.Same, "Semi-last is dummy");
ClassicAssert.NotNull(instance.Same.Same.Same.Whatever, "Whatever doesn't increase recursion count");
ClassicAssert.NotNull(instance.Same.OrThrow().Same.OrThrow().Same, "Last recursive element should be generated as uninitialized dummy");
ClassicAssert.Null(instance.Same.OrThrow().Same.OrThrow().Same.OrThrow().Same, "Semi-last is dummy");
ClassicAssert.NotNull(instance.Same.OrThrow().Same.OrThrow().Same.OrThrow().Whatever, "Whatever doesn't increase recursion count");
}

[Test, Parallelizable]
Expand All @@ -22,8 +23,8 @@ public void ShouldRespectRecursionLimitOf5ForSpecificType2()
//GIVEN
var instance = Any.Instance<RecursiveClassWithOnlyReferenceToItself>();

ClassicAssert.NotNull(instance.Same.Same.Same, "Fourth recursive element should be generated as uninitialized dummy");
ClassicAssert.Null(instance.Same.Same.Same.Same, "Semi-last is dummy");
ClassicAssert.NotNull(instance.Same.OrThrow().Same.OrThrow().Same, "Fourth recursive element should be generated as uninitialized dummy");
ClassicAssert.Null(instance.Same.OrThrow().Same.OrThrow().Same.OrThrow().Same, "Semi-last is dummy");
}

[Test, Parallelizable]
Expand All @@ -42,8 +43,11 @@ public void ShouldApplyPerTypeRecursionLimit()
//GIVEN
var instance = Any.Instance<ObjectWithIndirectRecursion>();

ClassicAssert.NotNull(instance.Other2.Other.Other2.Other.Other2.Other.Other2, "Dummy algorithm generates one last dummy");
ClassicAssert.Null(instance.Other2.Other.Other2.Other.Other2.Other.Other2.Other);
ClassicAssert.NotNull(
instance.Other2.OrThrow().Other.OrThrow().Other2.OrThrow().Other.OrThrow().Other2.OrThrow().Other.OrThrow()
.Other2, "Dummy algorithm generates one last dummy");
ClassicAssert.Null(instance.Other2.OrThrow().Other.OrThrow().Other2.OrThrow().Other.OrThrow().Other2.OrThrow().Other
.OrThrow().Other2.OrThrow().Other);
}

[Test, Parallelizable, Ignore("diminishing disabled for now for backwards compatibility reasons")]
Expand All @@ -53,7 +57,8 @@ public void ShouldRespectNestingLimit()
var instance = Any.Instance<RecursiveClass>();

//THEN
ClassicAssert.AreEqual(1, instance.Others[0].Other.Others[0].Other.Others.Length, "Dummy algorithm generates an empty collection");
ClassicAssert.AreEqual(1, instance.Others.OrThrow()[0].Other.Others.OrThrow()[0].Other.Others.OrThrow().Length,
"Dummy algorithm generates an empty collection");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static EnumerableConversion<T, ConcurrentBag<T>> AsConcurrentBag<T>(this
}

public static EnumerableConversion<KeyValuePair<TKey, TValue>, Dictionary<TKey, TValue>>
AsDictionary<TKey, TValue>(this InlineGenerator<IEnumerable<KeyValuePair<TKey, TValue>>> enumerableGenerator)
AsDictionary<TKey, TValue>(this InlineGenerator<IEnumerable<KeyValuePair<TKey, TValue>>> enumerableGenerator) where TKey : notnull
{
return Conversion(enumerableGenerator,
enumerable => enumerable.ToDictionary(x => x.Key, x => x.Value));
Expand Down Expand Up @@ -95,41 +95,41 @@ public static InlineGenerator<ImmutableStack<T>> AsImmutableStack<T>(this Inline
}

public static InlineGenerator<ImmutableDictionary<T1, T2>> AsImmutableDictionary<T1, T2>(
this InlineGenerator<IEnumerable<KeyValuePair<T1, T2>>> enumerableGenerator)
this InlineGenerator<IEnumerable<KeyValuePair<T1, T2>>> enumerableGenerator) where T1 : notnull
{
return Conversion(enumerableGenerator, ImmutableDictionary.ToImmutableDictionary);
}

public static InlineGenerator<ImmutableSortedDictionary<T1, T2>> AsImmutableSortedDictionary<T1, T2>(
this InlineGenerator<IEnumerable<KeyValuePair<T1, T2>>> enumerableGenerator)
this InlineGenerator<IEnumerable<KeyValuePair<T1, T2>>> enumerableGenerator) where T1 : notnull
{
return Conversion(enumerableGenerator, ImmutableSortedDictionary.ToImmutableSortedDictionary);
}

public static EnumerableConversion<KeyValuePair<TKey, TValue>, IReadOnlyDictionary<TKey, TValue>>
AsReadOnlyDictionary<TKey, TValue>(this InlineGenerator<IEnumerable<KeyValuePair<TKey, TValue>>> enumerableGenerator)
AsReadOnlyDictionary<TKey, TValue>(this InlineGenerator<IEnumerable<KeyValuePair<TKey, TValue>>> enumerableGenerator) where TKey : notnull
{
return Conversion(enumerableGenerator,
enumerable => (IReadOnlyDictionary<TKey, TValue>)(enumerable.ToDictionary(x => x.Key, x => x.Value)));
}

public static EnumerableConversion<KeyValuePair<TKey, TValue>, SortedDictionary<TKey, TValue>>
AsSortedDictionary<TKey, TValue>(this InlineGenerator<IEnumerable<KeyValuePair<TKey, TValue>>> enumerableGenerator)
AsSortedDictionary<TKey, TValue>(this InlineGenerator<IEnumerable<KeyValuePair<TKey, TValue>>> enumerableGenerator) where TKey : notnull
{
return Conversion(enumerableGenerator,
enumerable => new SortedDictionary<TKey, TValue>(
enumerable.ToDictionary(x => x.Key, x => x.Value)));
}

public static EnumerableConversion<KeyValuePair<TKey, TValue>, ConcurrentDictionary<TKey, TValue>>
AsConcurrentDictionary<TKey, TValue>(this InlineGenerator<IEnumerable<KeyValuePair<TKey, TValue>>> enumerableGenerator)
AsConcurrentDictionary<TKey, TValue>(this InlineGenerator<IEnumerable<KeyValuePair<TKey, TValue>>> enumerableGenerator) where TKey : notnull
{
return Conversion(enumerableGenerator,
enumerable => new ConcurrentDictionary<TKey, TValue>(enumerable));
}

public static EnumerableConversion<KeyValuePair<TKey, TValue>, SortedList<TKey, TValue>>
AsSortedList<TKey, TValue>(this InlineGenerator<IEnumerable<KeyValuePair<TKey, TValue>>> enumerableGenerator)
AsSortedList<TKey, TValue>(this InlineGenerator<IEnumerable<KeyValuePair<TKey, TValue>>> enumerableGenerator) where TKey : notnull
{
return Conversion(enumerableGenerator,
enumerable => new SortedList<TKey, TValue>(enumerable.ToDictionary(x => x.Key, x => x.Value)));
Expand Down

0 comments on commit 51dd049

Please sign in to comment.