From 9869ee9545cfeab1c9f6fbef4d88d9de1b9204f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20Ga=C5=82=C4=99zowski?= Date: Thu, 7 Mar 2024 23:25:42 +0100 Subject: [PATCH] wip --- .../AnyExtensibility/BasicGenerator.cs | 4 ++-- .../AnyGenerators/Root/InlineGenerators.cs | 4 ++-- .../Collections/AnyCollectionExtensions.cs | 4 ++-- .../BuildScript/BuildScript.csproj | 1 + .../TypeResolution/AllGenerator.cs | 2 +- .../FallbackDummyObjectResolution.cs | 5 ++--- .../DictionaryWithKeysGenerator.cs | 12 +++------- .../DefaultGenerationRequest.cs | 6 ++--- .../InternalInlineGenerators.cs | 22 +++++++++---------- .../Numbers/IntegerSequence.cs | 4 ++-- .../FakeChainElements/ResolutionOfArrays.cs | 3 ++- 11 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/netstandard2.0/AnyExtensibility/BasicGenerator.cs b/src/netstandard2.0/AnyExtensibility/BasicGenerator.cs index 3ff71cf..2b80727 100644 --- a/src/netstandard2.0/AnyExtensibility/BasicGenerator.cs +++ b/src/netstandard2.0/AnyExtensibility/BasicGenerator.cs @@ -7,9 +7,9 @@ public interface BasicGenerator { [Obsolete("Do not use this method. It does not generate strings")] [EditorBrowsable(EditorBrowsableState.Never)] - string ToString(); + string? ToString(); T Instance(); T Instance(params GenerationCustomization[] customizations); T InstanceOf(InlineGenerator gen); -} \ No newline at end of file +} diff --git a/src/netstandard2.0/AnyGenerators/Root/InlineGenerators.cs b/src/netstandard2.0/AnyGenerators/Root/InlineGenerators.cs index 7e57468..2657fed 100644 --- a/src/netstandard2.0/AnyGenerators/Root/InlineGenerators.cs +++ b/src/netstandard2.0/AnyGenerators/Root/InlineGenerators.cs @@ -123,12 +123,12 @@ public static InlineGenerator> Dictionary // Used by reflection // public API // ReSharper disable once UnusedMember.Global - public static InlineGenerator> ReadOnlyDictionary() + public static InlineGenerator> ReadOnlyDictionary() where TKey : notnull { return InternalInlineGenerators.ReadOnlyDictionary(); } - public static InlineGenerator> ReadOnlyDictionary(int length) + public static InlineGenerator> ReadOnlyDictionary(int length) where TKey : notnull { return InternalInlineGenerators.ReadOnlyDictionary(length); } diff --git a/src/netstandard2.0/AnyRoot/Collections/AnyCollectionExtensions.cs b/src/netstandard2.0/AnyRoot/Collections/AnyCollectionExtensions.cs index 34be563..d0b4b7d 100644 --- a/src/netstandard2.0/AnyRoot/Collections/AnyCollectionExtensions.cs +++ b/src/netstandard2.0/AnyRoot/Collections/AnyCollectionExtensions.cs @@ -166,7 +166,7 @@ public static Dictionary Dictionary(this BasicGenera return gen.InstanceOf(InlineGenerators.Dictionary()); } - public static IReadOnlyDictionary ReadOnlyDictionary(this BasicGenerator gen, int length) + public static IReadOnlyDictionary ReadOnlyDictionary(this BasicGenerator gen, int length) where TKey : notnull { return gen.InstanceOf(InlineGenerators.ReadOnlyDictionary(length)); } @@ -176,7 +176,7 @@ public static IReadOnlyDictionary ReadOnlyDictionaryWithKeys(keys)); } - public static IReadOnlyDictionary ReadOnlyDictionary(this BasicGenerator gen) + public static IReadOnlyDictionary ReadOnlyDictionary(this BasicGenerator gen) where TKey : notnull { return gen.InstanceOf(InlineGenerators.ReadOnlyDictionary()); } diff --git a/src/netstandard2.0/BuildScript/BuildScript.csproj b/src/netstandard2.0/BuildScript/BuildScript.csproj index 2801c0a..ce24a2c 100644 --- a/src/netstandard2.0/BuildScript/BuildScript.csproj +++ b/src/netstandard2.0/BuildScript/BuildScript.csproj @@ -5,6 +5,7 @@ net8.0 latest enable + nullable diff --git a/src/netstandard2.0/TypeResolution/AllGenerator.cs b/src/netstandard2.0/TypeResolution/AllGenerator.cs index 2354b9a..840f088 100644 --- a/src/netstandard2.0/TypeResolution/AllGenerator.cs +++ b/src/netstandard2.0/TypeResolution/AllGenerator.cs @@ -85,7 +85,7 @@ public T Dummy(GenerationRequest request) public T OtherThan(params T[]? omittedValues) { - return (T)OtherThan(typeof(T), omittedValues?.Cast()?.ToArray(), DefaultGenerationRequest.WithDefaultLimits()); + return (T)OtherThan(typeof(T), omittedValues?.Cast()?.ToArray() ?? [], DefaultGenerationRequest.WithDefaultLimits()); } public object OtherThan(Type type, object[] skippedValues, GenerationRequest request) diff --git a/src/netstandard2.0/TypeResolution/FakeChainElements/DummyChainElements/FallbackDummyObjectResolution.cs b/src/netstandard2.0/TypeResolution/FakeChainElements/DummyChainElements/FallbackDummyObjectResolution.cs index 03e1c82..e72dc00 100644 --- a/src/netstandard2.0/TypeResolution/FakeChainElements/DummyChainElements/FallbackDummyObjectResolution.cs +++ b/src/netstandard2.0/TypeResolution/FakeChainElements/DummyChainElements/FallbackDummyObjectResolution.cs @@ -8,17 +8,16 @@ namespace TddXt.TypeResolution.FakeChainElements.DummyChainElements; public class FallbackDummyObjectResolution : IResolution { - public object? Apply(InstanceGenerator allGenerator, GenerationRequest generationRequest, Type type) + public object Apply(InstanceGenerator allGenerator, GenerationRequest generationRequest, Type type) { object result; try { result = SmartType.For(type).GetPublicParameterlessConstructor().Value().Invoke(Enumerable.Empty()); } - catch (Exception e) + catch { result = FormatterServices.GetUninitializedObject(type); - } generationRequest.CustomizeCreatedValue(result, allGenerator); return result; diff --git a/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/Collections/DictionaryWithKeysGenerator.cs b/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/Collections/DictionaryWithKeysGenerator.cs index f855798..1836771 100644 --- a/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/Collections/DictionaryWithKeysGenerator.cs +++ b/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/Collections/DictionaryWithKeysGenerator.cs @@ -1,20 +1,14 @@ using System.Collections.Generic; +using System.Linq; using TddXt.AnyExtensibility; namespace TddXt.TypeResolution.FakeChainElements.InlineGeneratorTypes.Collections; public class DictionaryWithKeysGenerator(IEnumerable keys) - : InlineGenerator> + : InlineGenerator> where TKey : notnull { public Dictionary GenerateInstance(InstanceGenerator instanceGenerator, GenerationRequest request) { - var dict = new Dictionary(); - - foreach (var key in keys) - { - dict.Add(key, instanceGenerator.Instance(request)); - } - - return dict; + return keys.ToDictionary(key => key, key => instanceGenerator.Instance(request)); } } diff --git a/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/Generic/ImplementationDetails/DefaultGenerationRequest.cs b/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/Generic/ImplementationDetails/DefaultGenerationRequest.cs index 91ade64..e34a285 100644 --- a/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/Generic/ImplementationDetails/DefaultGenerationRequest.cs +++ b/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/Generic/ImplementationDetails/DefaultGenerationRequest.cs @@ -100,15 +100,15 @@ public object ResolveNextNestingLevel( Trace.RecursionLimitReachedTryingDummy(); return instanceGenerator.Dummy(nestedRequest, type)!; } - catch (TargetInvocationException e) + catch (TargetInvocationException) { return default!; } - catch (MemberAccessException e) + catch (MemberAccessException) { return default!; } - catch (ArgumentException e) + catch (ArgumentException) { return default!; } diff --git a/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/InternalInlineGenerators.cs b/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/InternalInlineGenerators.cs index 8aaf2fd..cc3dced 100644 --- a/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/InternalInlineGenerators.cs +++ b/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/InternalInlineGenerators.cs @@ -149,12 +149,12 @@ public static InlineGenerator> ListWith(T[] included) return new InclusiveEnumerableGenerator(included).AsList(); } - public static InlineGenerator> SortedList(int length) + public static InlineGenerator> SortedList(int length) where TKey : notnull { return new EnumerableGenerator>(ManyStrategies.FromConstant(length)).AsSortedList(); } - public static InlineGenerator> SortedList() + public static InlineGenerator> SortedList() where TKey : notnull { return new EnumerableGenerator>( ManyStrategies.FromRequest()).AsSortedList(); @@ -186,7 +186,7 @@ public static InlineGenerator> SortedSet() return new EnumerableGenerator(ManyStrategies.FromRequest()).AsSortedSet(); } - public static InlineGenerator> Dictionary(int length) + public static InlineGenerator> Dictionary(int length) where TKey : notnull { return new EnumerableGenerator>(ManyStrategies.FromConstant(length)).AsDictionary(); } @@ -194,12 +194,12 @@ public static InlineGenerator> Dictionary // Used by reflection // public API // ReSharper disable once UnusedMember.Global - public static InlineGenerator> ReadOnlyDictionary() + public static InlineGenerator> ReadOnlyDictionary() where TKey : notnull { return new EnumerableGenerator>(ManyStrategies.FromRequest()).AsReadOnlyDictionary(); } - public static InlineGenerator> ReadOnlyDictionary(int length) + public static InlineGenerator> ReadOnlyDictionary(int length) where TKey : notnull { return new EnumerableGenerator>(ManyStrategies.FromConstant(length)).AsReadOnlyDictionary(); } @@ -207,12 +207,12 @@ public static InlineGenerator> ReadOnlyDiction // Used by reflection // public API // ReSharper disable once UnusedMember.Global - public static InlineGenerator> Dictionary() + public static InlineGenerator> Dictionary() where TKey : notnull { return new EnumerableGenerator>(ManyStrategies.FromRequest()).AsDictionary(); } - public static InlineGenerator> ConcurrentDictionary(int length) + public static InlineGenerator> ConcurrentDictionary(int length) where TKey : notnull { return new EnumerableGenerator>(ManyStrategies.FromConstant(length)) .AsConcurrentDictionary(); @@ -221,7 +221,7 @@ public static InlineGenerator> ConcurrentDict // Used by reflection // public API // ReSharper disable once UnusedMember.Global - public static InlineGenerator> ConcurrentDictionary() + public static InlineGenerator> ConcurrentDictionary() where TKey : notnull { return new EnumerableGenerator>(ManyStrategies.FromRequest()) .AsConcurrentDictionary(); @@ -253,7 +253,7 @@ public static InlineGenerator> ConcurrentQueue() return new EnumerableGenerator(ManyStrategies.FromRequest()).AsConcurrentQueue(); } - public static InlineGenerator> SortedDictionary(int length) + public static InlineGenerator> SortedDictionary(int length) where TKey : notnull { return new EnumerableGenerator>(ManyStrategies.FromConstant(length)) .AsSortedDictionary(); @@ -262,7 +262,7 @@ public static InlineGenerator> SortedDictionary> SortedDictionary() + public static InlineGenerator> SortedDictionary() where TKey : notnull { return new EnumerableGenerator>(ManyStrategies.FromRequest()) .AsSortedDictionary(); @@ -298,7 +298,7 @@ public static InlineGenerator From(T[] possibleValues) return new GeneratorByPickingFromSpecifiedSetOfValues(possibleValues); } - public static InlineGenerator> DictionaryWithKeys(IEnumerable keys) + public static InlineGenerator> DictionaryWithKeys(IEnumerable keys) where TKey : notnull { return new DictionaryWithKeysGenerator(keys); } diff --git a/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/Numbers/IntegerSequence.cs b/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/Numbers/IntegerSequence.cs index fbf2f48..f8a087d 100644 --- a/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/Numbers/IntegerSequence.cs +++ b/src/netstandard2.0/TypeResolution/FakeChainElements/InlineGeneratorTypes/Numbers/IntegerSequence.cs @@ -24,14 +24,14 @@ public IntegerSequence(int startingValue, int step, int initialStepsCount) } - public bool Equals(IntegerSequence other) + public bool Equals(IntegerSequence? other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; return _startingValue == other._startingValue && _step == other._step; } - public override bool Equals(object obj) + public override bool Equals(object? obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; diff --git a/src/netstandard2.0/TypeResolution/FakeChainElements/ResolutionOfArrays.cs b/src/netstandard2.0/TypeResolution/FakeChainElements/ResolutionOfArrays.cs index 0f7d051..c7d303a 100644 --- a/src/netstandard2.0/TypeResolution/FakeChainElements/ResolutionOfArrays.cs +++ b/src/netstandard2.0/TypeResolution/FakeChainElements/ResolutionOfArrays.cs @@ -1,4 +1,5 @@ using System; +using Core.NullableReferenceTypesExtensions; using TddXt.AnyExtensibility; using TddXt.TypeResolution.FakeChainElements.InlineGeneratorTypes; @@ -13,7 +14,7 @@ public bool AppliesTo(Type type) public object Apply(InstanceGenerator instanceGenerator, GenerationRequest request, Type type) { - var elementType = type.GetElementType(); + var elementType = type.GetElementType().OrThrow(); var array = InternalInlineGenerators.GetByNameAndType(nameof(InternalInlineGenerators.Array), elementType) .GenerateInstance(instanceGenerator, request); return array;