Skip to content

Commit

Permalink
More nullability checks
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek-galezowski committed Mar 6, 2024
1 parent 51dd049 commit 3f9262d
Show file tree
Hide file tree
Showing 26 changed files with 104 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>

<PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/netstandard2.0/AnyGenerators/AnyGenerators.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.1</TargetFrameworks>
Expand Down
23 changes: 11 additions & 12 deletions src/netstandard2.0/AnyGenerators/Root/InlineGenerators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ public static InlineGenerator<List<T>> ListWith<T>(T[] included)
return InternalInlineGenerators.ListWith(included);
}

public static InlineGenerator<SortedList<TKey, TValue>> SortedList<TKey, TValue>(int length)
public static InlineGenerator<SortedList<TKey, TValue>> SortedList<TKey, TValue>(int length) where TKey : notnull
{
return InternalInlineGenerators.SortedList<TKey, TValue>(length);
}

public static InlineGenerator<SortedList<TKey, TValue>> SortedList<TKey, TValue>()
public static InlineGenerator<SortedList<TKey, TValue>> SortedList<TKey, TValue>() where TKey : notnull
{
return InternalInlineGenerators.SortedList<TKey, TValue>();
}
Expand Down Expand Up @@ -115,7 +115,7 @@ public static InlineGenerator<SortedSet<T>> SortedSet<T>()
return InternalInlineGenerators.SortedSet<T>();
}

public static InlineGenerator<Dictionary<TKey, TValue>> Dictionary<TKey, TValue>(int length)
public static InlineGenerator<Dictionary<TKey, TValue>> Dictionary<TKey, TValue>(int length) where TKey : notnull
{
return InternalInlineGenerators.Dictionary<TKey, TValue>(length);
}
Expand All @@ -136,21 +136,20 @@ public static InlineGenerator<IReadOnlyDictionary<TKey, TValue>> ReadOnlyDiction
// Used by reflection
// public API
// ReSharper disable once UnusedMember.Global
public static InlineGenerator<Dictionary<TKey, TValue>> Dictionary<TKey, TValue>()
public static InlineGenerator<Dictionary<TKey, TValue>> Dictionary<TKey, TValue>() where TKey : notnull
{
return InternalInlineGenerators.Dictionary<TKey, TValue>();
}


public static InlineGenerator<ConcurrentDictionary<TKey, TValue>> ConcurrentDictionary<TKey, TValue>(int length)
public static InlineGenerator<ConcurrentDictionary<TKey, TValue>> ConcurrentDictionary<TKey, TValue>(int length) where TKey : notnull
{
return InternalInlineGenerators.ConcurrentDictionary<TKey, TValue>(length);
}

// Used by reflection
// public API
// ReSharper disable once UnusedMember.Global
public static InlineGenerator<ConcurrentDictionary<TKey, TValue>> ConcurrentDictionary<TKey, TValue>()
public static InlineGenerator<ConcurrentDictionary<TKey, TValue>> ConcurrentDictionary<TKey, TValue>() where TKey : notnull
{
return InternalInlineGenerators.ConcurrentDictionary<TKey, TValue>();
}
Expand Down Expand Up @@ -181,15 +180,15 @@ public static InlineGenerator<ConcurrentQueue<T>> ConcurrentQueue<T>()
return InternalInlineGenerators.ConcurrentQueue<T>();
}

public static InlineGenerator<SortedDictionary<TKey, TValue>> SortedDictionary<TKey,TValue>(int length)
public static InlineGenerator<SortedDictionary<TKey, TValue>> SortedDictionary<TKey,TValue>(int length) where TKey : notnull
{
return InternalInlineGenerators.SortedDictionary<TKey, TValue>(length);
}

// Used by reflection
// public API
// ReSharper disable once UnusedMember.Global
public static InlineGenerator<SortedDictionary<TKey, TValue>> SortedDictionary<TKey,TValue>()
public static InlineGenerator<SortedDictionary<TKey, TValue>> SortedDictionary<TKey,TValue>() where TKey : notnull
{
return InternalInlineGenerators.SortedDictionary<TKey, TValue>();
}
Expand Down Expand Up @@ -224,7 +223,7 @@ public static InlineGenerator<T> From<T>(T[] possibleValues)
return InternalInlineGenerators.From(possibleValues);
}

public static InlineGenerator<Dictionary<TKey, TValue>> DictionaryWithKeys<TKey, TValue>(IEnumerable<TKey> keys)
public static InlineGenerator<Dictionary<TKey, TValue>> DictionaryWithKeys<TKey, TValue>(IEnumerable<TKey> keys) where TKey : notnull
{
return InternalInlineGenerators.DictionaryWithKeys<TKey, TValue>(keys);
}
Expand Down Expand Up @@ -564,15 +563,15 @@ public static InlineGenerator<ImmutableSortedSet<T>> ImmutableSortedSet<T>()
// Used by reflection
// public API
// ReSharper disable once UnusedMember.Global
public static InlineGenerator<ImmutableDictionary<T1, T2>> ImmutableDictionary<T1, T2>()
public static InlineGenerator<ImmutableDictionary<T1, T2>> ImmutableDictionary<T1, T2>() where T1 : notnull
{
return InternalInlineGenerators.ImmutableDictionary<T1, T2>();
}

// Used by reflection
// public API
// ReSharper disable once UnusedMember.Global
public static InlineGenerator<ImmutableSortedDictionary<T1, T2>> ImmutableSortedDictionary<T1, T2>()
public static InlineGenerator<ImmutableSortedDictionary<T1, T2>> ImmutableSortedDictionary<T1, T2>() where T1 : notnull
{
return InternalInlineGenerators.ImmutableSortedDictionary<T1, T2>();
}
Expand Down
2 changes: 1 addition & 1 deletion src/netstandard2.0/AnyRoot/AnyRoot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>

<PropertyGroup>
Expand Down
30 changes: 15 additions & 15 deletions src/netstandard2.0/AnyRoot/Collections/AnyCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ public static IReadOnlyList<T> ReadOnlyListWithout<T>(this BasicGenerator gen, p
return gen.InstanceOf(InlineGenerators.ListWithout(items));
}

public static SortedList<TKey, TValue> SortedList<TKey, TValue>(this BasicGenerator gen)
public static SortedList<TKey, TValue> SortedList<TKey, TValue>(this BasicGenerator gen) where TKey : notnull
{
return gen.InstanceOf(InlineGenerators.SortedList<TKey, TValue>());
}

public static SortedList<TKey, TValue> SortedList<TKey, TValue>(this BasicGenerator gen, int length)
public static SortedList<TKey, TValue> SortedList<TKey, TValue>(this BasicGenerator gen, int length) where TKey : notnull
{
return gen.InstanceOf(InlineGenerators.SortedList<TKey, TValue>(length));
}
Expand All @@ -151,17 +151,17 @@ public static ISet<T> SortedSet<T>(this BasicGenerator gen)
return gen.InstanceOf(InlineGenerators.SortedSet<T>());
}

public static Dictionary<TKey, TValue> Dictionary<TKey, TValue>(this BasicGenerator gen, int length)
public static Dictionary<TKey, TValue> Dictionary<TKey, TValue>(this BasicGenerator gen, int length) where TKey : notnull
{
return gen.InstanceOf(InlineGenerators.Dictionary<TKey, TValue>(length));
}

public static Dictionary<T, U> DictionaryWithKeys<T, U>(this BasicGenerator gen, IEnumerable<T> keys)
public static Dictionary<TKey, TValue> DictionaryWithKeys<TKey, TValue>(this BasicGenerator gen, IEnumerable<TKey> keys) where TKey : notnull
{
return gen.InstanceOf(InlineGenerators.DictionaryWithKeys<T, U>(keys));
return gen.InstanceOf(InlineGenerators.DictionaryWithKeys<TKey, TValue>(keys));
}

public static Dictionary<TKey, TValue> Dictionary<TKey, TValue>(this BasicGenerator gen)
public static Dictionary<TKey, TValue> Dictionary<TKey, TValue>(this BasicGenerator gen) where TKey : notnull
{
return gen.InstanceOf(InlineGenerators.Dictionary<TKey, TValue>());
}
Expand All @@ -171,7 +171,7 @@ public static IReadOnlyDictionary<TKey, TValue> ReadOnlyDictionary<TKey, TValue>
return gen.InstanceOf(InlineGenerators.ReadOnlyDictionary<TKey, TValue>(length));
}

public static IReadOnlyDictionary<TKey, TValue> ReadOnlyDictionaryWithKeys<TKey, TValue>(this BasicGenerator gen, IEnumerable<TKey> keys)
public static IReadOnlyDictionary<TKey, TValue> ReadOnlyDictionaryWithKeys<TKey, TValue>(this BasicGenerator gen, IEnumerable<TKey> keys) where TKey : notnull
{
return gen.InstanceOf(InlineGenerators.DictionaryWithKeys<TKey, TValue>(keys));
}
Expand All @@ -181,22 +181,22 @@ public static IReadOnlyDictionary<TKey, TValue> ReadOnlyDictionary<TKey, TValue>
return gen.InstanceOf(InlineGenerators.ReadOnlyDictionary<TKey, TValue>());
}

public static SortedDictionary<TKey, TValue> SortedDictionary<TKey, TValue>(this BasicGenerator gen, int length)
public static SortedDictionary<TKey, TValue> SortedDictionary<TKey, TValue>(this BasicGenerator gen, int length) where TKey : notnull
{
return gen.InstanceOf(InlineGenerators.SortedDictionary<TKey, TValue>(length));
}

public static SortedDictionary<TKey, TValue> SortedDictionary<TKey, TValue>(this BasicGenerator gen)
public static SortedDictionary<TKey, TValue> SortedDictionary<TKey, TValue>(this BasicGenerator gen) where TKey : notnull
{
return gen.InstanceOf(InlineGenerators.SortedDictionary<TKey, TValue>());
}

public static ConcurrentDictionary<TKey, TValue> ConcurrentDictionary<TKey, TValue>(this BasicGenerator gen, int length)
public static ConcurrentDictionary<TKey, TValue> ConcurrentDictionary<TKey, TValue>(this BasicGenerator gen, int length) where TKey : notnull
{
return gen.InstanceOf(InlineGenerators.ConcurrentDictionary<TKey, TValue>(length));
}

public static ConcurrentDictionary<TKey, TValue> ConcurrentDictionary<TKey, TValue>(this BasicGenerator gen)
public static ConcurrentDictionary<TKey, TValue> ConcurrentDictionary<TKey, TValue>(this BasicGenerator gen) where TKey : notnull
{
return gen.InstanceOf(InlineGenerators.ConcurrentDictionary<TKey, TValue>());
}
Expand Down Expand Up @@ -263,14 +263,14 @@ public static ImmutableSortedSet<T> ImmutableSortedSet<T>(this BasicGenerator ge
return gen.InstanceOf(InlineGenerators.ImmutableSortedSet<T>());
}

public static ImmutableDictionary<T1, T2> ImmutableDictionary<T1, T2>(this BasicGenerator gen)
public static ImmutableDictionary<TKey, TValue> ImmutableDictionary<TKey, TValue>(this BasicGenerator gen) where TKey : notnull
{
return gen.InstanceOf(InlineGenerators.ImmutableDictionary<T1, T2>());
return gen.InstanceOf(InlineGenerators.ImmutableDictionary<TKey, TValue>());
}

public static ImmutableSortedDictionary<T1, T2> ImmutableSortedDictionary<T1, T2>(this BasicGenerator gen)
public static ImmutableSortedDictionary<TKey, TValue> ImmutableSortedDictionary<TKey, TValue>(this BasicGenerator gen) where TKey : notnull
{
return gen.InstanceOf(InlineGenerators.ImmutableSortedDictionary<T1, T2>());
return gen.InstanceOf(InlineGenerators.ImmutableSortedDictionary<TKey, TValue>());
}

public static ImmutableQueue<T> ImmutableQueue<T>(this BasicGenerator gen)
Expand Down
9 changes: 5 additions & 4 deletions src/netstandard2.0/AnySpecification/AnySpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Threading.Tasks;
using AnySpecification.Fixtures;
using AnySpecification.GraphComparison;
using Core.NullableReferenceTypesExtensions;
using FluentAssertions;
using Functional.Maybe;
using Newtonsoft.Json;
Expand Down Expand Up @@ -382,7 +383,7 @@ public void ShouldDisallowSkippingTheSameValueTwiceWhenGeneratingAnyValueOtherTh
}

[Test, CancelAfter(2000)]
public async Task ShouldDisallowSkippingAllEnumMembers(CancellationToken ct)
public void ShouldDisallowSkippingAllEnumMembers()
{
Any.Invoking(a => a.OtherThan(
LolEnum.Value2,
Expand Down Expand Up @@ -992,8 +993,8 @@ public void ShouldAllowCustomizationsToReachInnerAutoFixture()
}));

anyConcrete.DummyString.Should().Be("CustomString");
anyConcrete.Inner.InnerDummyInt.Should().Be(123);
anyConcrete.Inner.InnerDummyString.Should().Be("InnerCustomString");
anyConcrete.Inner.OrThrow().InnerDummyInt.Should().Be(123);
anyConcrete.Inner.OrThrow().InnerDummyString.Should().Be("InnerCustomString");
}

[TestCase(LolEnum.Value1)]
Expand Down Expand Up @@ -1876,7 +1877,7 @@ private static void CallSomeMethodsOn(AbstractObjectWithInterfaceInConstructor x
RecursiveInterface x3)
{
// ReSharper disable once UnusedVariable
_ = new object[] {x1.AbstractInt, x2.GetSomething(), x3.NestedAsDictionary, x2.GetSomething2(), x3.Nested};
_ = new object[] {x1.AbstractInt, x2.GetSomething().OrThrow(), x3.NestedAsDictionary, x2.GetSomething2(), x3.Nested};
}

private static void SerializeAnyInstanceOf<T>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8603</WarningsAsErrors>
<WarningsAsErrors>nullable</WarningsAsErrors>
</PropertyGroup>

<PropertyGroup>
Expand Down
49 changes: 25 additions & 24 deletions src/netstandard2.0/AnySpecification/BuilderSpecification.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Core.NullableReferenceTypesExtensions;
using FluentAssertions;
using TddXt.AnyRoot.Builder;

Expand All @@ -9,28 +10,28 @@ class BuilderSpecification
[Test, Parallelizable]
public void ShouldAllowSettingNestedSettableAutoProperties()
{
Any.Instance<DataStructure>().WithProperty(d => d.Nested.SettableStringValue, "lol")
.Nested.SettableStringValue.Should().Be("lol");
Any.Instance<DataStructure>().WithProperty(d => d.Nested!.SettableStringValue, "lol")
.Nested.OrThrow().SettableStringValue.Should().Be("lol");
}

[Test, Parallelizable]
public void ShouldAllowSettingNestedNonSettableAutoProperties()
{
Any.Instance<DataStructure>().WithProperty(d => d.Nested.StringValue, "lol")
.Nested.StringValue.Should().Be("lol");
Any.Instance<DataStructure>().WithProperty(d => d.Nested!.StringValue, "lol")
.Nested.OrThrow().StringValue.Should().Be("lol");
}

[Test, Parallelizable]
public void ShouldThrowWhenSettingPropertyOfUninitializedProperty()
{
Any.Instance<DataStructure>().Invoking(d => d.WithProperty(d => d.NestedNotInitializedFromConstructor.StringValue, "lol"))
Any.Instance<DataStructure>().Invoking(d => d.WithProperty(d => d.NestedNotInitializedFromConstructor!.StringValue, "lol"))
.Should().Throw<Exception>();
}

[Test, Parallelizable]
public void ShouldThrowWhenInvokedOnNull()
{
new Action(() => (null as DataStructure)!.WithProperty(d => d!.NestedNotInitializedFromConstructor.StringValue, "lol"))
new Action(() => (null as DataStructure)!.WithProperty(d => d.NestedNotInitializedFromConstructor!.StringValue, "lol"))
.Should().Throw<Exception>();
}

Expand All @@ -44,7 +45,7 @@ public void ShouldThrowWhenSettingPropertyOfMethodReturnValue()
[Test, Parallelizable]
public void ShouldThrowWhenSettingNonAutoNonSettableProperty()
{
Any.Instance<DataStructure>().Invoking(d => d.WithProperty(d => d.Nested.NonAutoStringValue, "lol"))
Any.Instance<DataStructure>().Invoking(d => d.WithProperty(d => d.Nested!.NonAutoStringValue, "lol"))
.Should().Throw<Exception>();
}

Expand All @@ -53,31 +54,31 @@ public void ShouldThrowWhenSettingNonAutoNonSettableProperty()
[Test, Parallelizable]
public void ShouldAllowSettingPublicFieldThroughProperty()
{
Any.Instance<DataStructure>().WithProperty(d => d.Nested.StringField, "lol")
.Nested.StringField.Should().Be("lol");
Any.Instance<DataStructure>().WithProperty(d => d.Nested!.StringField, "lol")
.Nested.OrThrow().StringField.Should().Be("lol");
}

[Test, Parallelizable]
public void ShouldAllowSettingPublicFieldThroughField()
{
Any.Instance<DataStructure>().WithProperty(d => d.NestedField.StringField, "lol")
.NestedField.StringField.Should().Be("lol");
Any.Instance<DataStructure>().WithProperty(d => d.NestedField!.StringField, "lol")
.NestedField.OrThrow().StringField.Should().Be("lol");
}

[Test, Parallelizable]
public void ShouldAllowSettingPublicFieldThroughReadOnlyField()
{
var dataStructure = Any.Instance<DataStructure>();
dataStructure.WithProperty(d => d.NestedReadOnlyField.StringField, "lol")
.NestedReadOnlyField.StringField.Should().Be("lol");
dataStructure.WithProperty(d => d.NestedReadOnlyField!.StringField, "lol")
.NestedReadOnlyField.OrThrow().StringField.Should().Be("lol");
}

[Test, Parallelizable]
public void ShouldAllowSettingPropertyThroughPublicField()
{
var dataStructure = Any.Instance<DataStructure>();
dataStructure.WithProperty(d => d.NestedReadOnlyField.SettableStringValue, "lol")
.NestedReadOnlyField.SettableStringValue.Should().Be("lol");
dataStructure.WithProperty(d => d.NestedReadOnlyField!.SettableStringValue, "lol")
.NestedReadOnlyField.OrThrow().SettableStringValue.Should().Be("lol");
}
}

Expand All @@ -88,17 +89,17 @@ public DataStructure(NestedDataStructure nested)
Nested = nested;
}

public NestedDataStructure Nested { get; }
public NestedDataStructure NestedField;
public NestedDataStructure NestedReadOnlyField;
public NestedDataStructure NestedNotInitializedFromConstructor { get; }
public NestedDataStructure GetNested() => new NestedDataStructure();
public NestedDataStructure? Nested { get; }
public NestedDataStructure? NestedField;
public NestedDataStructure? NestedReadOnlyField;
public NestedDataStructure? NestedNotInitializedFromConstructor { get; }
public NestedDataStructure GetNested() => new();
}

public class NestedDataStructure
{
public string StringField;
public string StringValue { get; }
public string SettableStringValue { get; set; }
public string? StringField;
public string? StringValue { get; }
public string? SettableStringValue { get; set; }
public string NonAutoStringValue => "";
}
}
6 changes: 3 additions & 3 deletions src/netstandard2.0/AnySpecification/Fixtures/AreaEntity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace AnySpecification.Fixtures;
namespace AnySpecification.Fixtures;

public class AreaEntity
{
public Feature Feature { get; set; }
}
public Feature? Feature { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace AnySpecification.Fixtures;
namespace AnySpecification.Fixtures;

public class ConcreteDataStructure2
{
public string Text { get; set; }
}
public string? Text { get; set; }
}
Loading

0 comments on commit 3f9262d

Please sign in to comment.