From c7bf6541169072fb273a4fc366ee8f1f5321e38a Mon Sep 17 00:00:00 2001 From: Layomi Akinrinade Date: Fri, 1 May 2020 10:21:21 -0700 Subject: [PATCH] Move collection tests to dedicated class + more clean up --- ...ithParameterizedConstructorConverter.Small.cs | 4 ++-- ...bjectWithParameterizedConstructorConverter.cs | 4 ++-- .../Json/Serialization/JsonConverterFactory.cs | 13 +++++++------ .../Json/Serialization/JsonPropertyInfoOfT.cs | 2 ++ .../JsonSerializer.Read.HandleMetadata.cs | 16 +++++++++------- .../CollectionTests.Concurrent.Write.cs | 2 +- .../CollectionTests.Concurrent.cs | 2 +- .../CollectionTests.Generic.Read.cs | 2 +- .../CollectionTests.Generic.Write.cs | 2 +- .../CollectionTests.Immutable.Read.cs | 3 +-- .../CollectionTests.Immutable.Write.cs | 3 +-- .../CollectionTests.NonGeneric.Read.cs | 2 +- .../CollectionTests.NonGeneric.Write.cs | 2 +- .../CollectionTests.ObjectModel.Read.cs | 2 +- .../CollectionTests.ObjectModel.Write.cs | 2 +- .../CollectionTests.Specialized.Read.cs | 2 +- .../CollectionTests.Specialized.Write.cs | 2 +- .../TestClasses.ImmutableCollections.cs | 1 - .../tests/Serialization/Value.ReadTests.cs | 1 + 19 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.Small.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.Small.cs index c9dcd97118d..391a5710204 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.Small.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.Small.cs @@ -58,7 +58,7 @@ namespace System.Text.Json.Serialization.Converters } break; default: - Debug.Fail("We shouldn't be here if there are more than 4 parameters."); + Debug.Fail("More than 4 params: we should be in override for LargeObjectWithParameterizedConstructorConverter."); throw new InvalidOperationException(); } @@ -98,7 +98,7 @@ namespace System.Text.Json.Serialization.Converters arguments.Arg3 = ((JsonParameterInfo)parameterInfo).TypedDefaultValue!; break; default: - Debug.Fail("We shouldn't be here if there are more than 4 parameters."); + Debug.Fail("More than 4 params: we should be in override for LargeObjectWithParameterizedConstructorConverter."); throw new InvalidOperationException(); } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.cs index 9634310ebf1..7842a0b8b7b 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectWithParameterizedConstructorConverter.cs @@ -26,7 +26,7 @@ namespace System.Text.Json.Serialization.Converters if (!state.SupportContinuation && !shouldReadPreservedReferences) { - // Fast path that avoids maintaining state variables and dealing with preserved references. + // Fast path that avoids maintaining state variables. ReadOnlySpan originalSpan = reader.OriginalSpan; @@ -72,7 +72,7 @@ namespace System.Text.Json.Serialization.Converters } else { - // Slower path that supports continuation and preserved references. + // Slower path that supports continuation. if (state.Current.ObjectState == StackFrameObjectState.None) { diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterFactory.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterFactory.cs index a485c3539d9..4a26cdaab2d 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterFactory.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterFactory.cs @@ -40,11 +40,15 @@ namespace System.Text.Json.Serialization internal override JsonPropertyInfo CreateJsonPropertyInfo() { + Debug.Fail("We should never get here."); + throw new InvalidOperationException(); } internal override JsonParameterInfo CreateJsonParameterInfo() { + Debug.Fail("We should never get here."); + throw new InvalidOperationException(); } @@ -68,8 +72,7 @@ namespace System.Text.Json.Serialization JsonSerializerOptions options, ref ReadStack state) { - // We should never get here. - Debug.Assert(false); + Debug.Fail("We should never get here."); throw new InvalidOperationException(); } @@ -80,8 +83,7 @@ namespace System.Text.Json.Serialization JsonSerializerOptions options, ref WriteStack state) { - // We should never get here. - Debug.Assert(false); + Debug.Fail("We should never get here."); throw new InvalidOperationException(); } @@ -94,8 +96,7 @@ namespace System.Text.Json.Serialization JsonSerializerOptions options, ref WriteStack state) { - // We should never get here. - Debug.Assert(false); + Debug.Fail("We should never get here."); throw new InvalidOperationException(); } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoOfT.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoOfT.cs index 7ced542e17f..863590f3b5f 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoOfT.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoOfT.cs @@ -11,6 +11,8 @@ namespace System.Text.Json /// /// Represents a strongly-typed property to prevent boxing and to create a direct delegate to the getter\setter. /// + /// is the for either the property's converter, + /// or a type's converter, if the current instance is a . internal sealed class JsonPropertyInfo : JsonPropertyInfo { public Func? Get { get; private set; } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleMetadata.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleMetadata.cs index d41ebadcf7b..edb6aebf97b 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleMetadata.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleMetadata.cs @@ -22,7 +22,7 @@ namespace System.Text.Json if (state.Current.ObjectState < StackFrameObjectState.ReadAheadNameOrEndObject) { // Read the first metadata property name. - if (!ReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadNameOrEndObject)) + if (!TryReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadNameOrEndObject)) { return false; } @@ -100,14 +100,14 @@ namespace System.Text.Json if (state.Current.ObjectState == StackFrameObjectState.ReadAheadRefValue) { - if (!ReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadRefValue)) + if (!TryReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadRefValue)) { return false; } } else if (state.Current.ObjectState == StackFrameObjectState.ReadAheadIdValue) { - if (!ReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadIdValue)) + if (!TryReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadIdValue)) { return false; } @@ -153,7 +153,7 @@ namespace System.Text.Json if (state.Current.ObjectState == StackFrameObjectState.ReadAheadRefEndObject) { - if (!ReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadRefEndObject)) + if (!TryReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadRefEndObject)) { return false; } @@ -174,7 +174,7 @@ namespace System.Text.Json if (state.Current.ObjectState == StackFrameObjectState.ReadAheadValuesName) { - if (!ReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadValuesName)) + if (!TryReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadValuesName)) { return false; } @@ -202,7 +202,7 @@ namespace System.Text.Json if (state.Current.ObjectState == StackFrameObjectState.ReadAheadValuesStartArray) { - if (!ReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadValuesStartArray)) + if (!TryReadAheadMetadataAndSetState(ref reader, ref state, StackFrameObjectState.ReadValuesStartArray)) { return false; } @@ -222,8 +222,10 @@ namespace System.Text.Json } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static bool ReadAheadMetadataAndSetState(ref Utf8JsonReader reader, ref ReadStack state, StackFrameObjectState nextState) + private static bool TryReadAheadMetadataAndSetState(ref Utf8JsonReader reader, ref ReadStack state, StackFrameObjectState nextState) { + // If we can't read here, the read will be completed at the root API by asking the stream for more data. + // Set the state so we know where to resume on re-entry. state.Current.ObjectState = nextState; return reader.Read(); } diff --git a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Concurrent.Write.cs b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Concurrent.Write.cs index cb78774b831..2e42fc67419 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Concurrent.Write.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Concurrent.Write.cs @@ -7,7 +7,7 @@ using Xunit; namespace System.Text.Json.Serialization.Tests { - public static partial class ValueTests + public static partial class CollectionTests { [Fact] public static void Write_ConcurrentCollection() diff --git a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Concurrent.cs b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Concurrent.cs index cb7947ae640..b4976ba357d 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Concurrent.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Concurrent.cs @@ -7,7 +7,7 @@ using Xunit; namespace System.Text.Json.Serialization.Tests { - public static partial class ValueTests + public static partial class CollectionTests { [Fact] public static void Read_ConcurrentCollection() diff --git a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Generic.Read.cs b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Generic.Read.cs index 0d44ddf9550..2d72329f494 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Generic.Read.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Generic.Read.cs @@ -8,7 +8,7 @@ using Xunit; namespace System.Text.Json.Serialization.Tests { - public static partial class ValueTests + public static partial class CollectionTests { [Fact] public static void ReadListOfList() diff --git a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Generic.Write.cs b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Generic.Write.cs index 0da0392f5dd..e408f09b0e7 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Generic.Write.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Generic.Write.cs @@ -8,7 +8,7 @@ using Xunit; namespace System.Text.Json.Serialization.Tests { - public static partial class ValueTests + public static partial class CollectionTests { [Fact] public static void WriteListOfList() diff --git a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Immutable.Read.cs b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Immutable.Read.cs index b02c1e0b995..6330c1dede6 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Immutable.Read.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Immutable.Read.cs @@ -5,12 +5,11 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; -using System.Text.Json.Tests; using Xunit; namespace System.Text.Json.Serialization.Tests { - public static partial class ValueTests + public static partial class CollectionTests { [Fact] public static void ReadImmutableArrayOfImmutableArray() diff --git a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Immutable.Write.cs b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Immutable.Write.cs index 3a7be82e8bd..8fa60e5aeb2 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Immutable.Write.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Immutable.Write.cs @@ -4,12 +4,11 @@ using System.Collections.Generic; using System.Collections.Immutable; -using System.Text.Json.Tests; using Xunit; namespace System.Text.Json.Serialization.Tests { - public static partial class ValueTests + public static partial class CollectionTests { [Fact] public static void WriteImmutableArrayOfImmutableArray() diff --git a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.NonGeneric.Read.cs b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.NonGeneric.Read.cs index 4d758ad69ec..3455a3e38f3 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.NonGeneric.Read.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.NonGeneric.Read.cs @@ -8,7 +8,7 @@ using Xunit; namespace System.Text.Json.Serialization.Tests { - public static partial class ValueTests + public static partial class CollectionTests { [Fact] public static void ReadGenericIEnumerableOfIEnumerable() diff --git a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.NonGeneric.Write.cs b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.NonGeneric.Write.cs index 32f75c4a160..b21ae5d177f 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.NonGeneric.Write.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.NonGeneric.Write.cs @@ -8,7 +8,7 @@ using Xunit; namespace System.Text.Json.Serialization.Tests { - public static partial class ValueTests + public static partial class CollectionTests { [Fact] public static void WriteIEnumerableOfIEnumerable() diff --git a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.ObjectModel.Read.cs b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.ObjectModel.Read.cs index 722b5f27e95..b37c12d1724 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.ObjectModel.Read.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.ObjectModel.Read.cs @@ -7,7 +7,7 @@ using Xunit; namespace System.Text.Json.Serialization.Tests { - public static partial class ValueTests + public static partial class CollectionTests { [Fact] public static void Read_ObjectModelCollection() diff --git a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.ObjectModel.Write.cs b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.ObjectModel.Write.cs index d1559ef1f9a..e8916e4744e 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.ObjectModel.Write.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.ObjectModel.Write.cs @@ -8,7 +8,7 @@ using Xunit; namespace System.Text.Json.Serialization.Tests { - public static partial class ValueTests + public static partial class CollectionTests { [Fact] public static void Write_ObjectModelCollection() diff --git a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Specialized.Read.cs b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Specialized.Read.cs index 57c69024710..28625c90984 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Specialized.Read.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Specialized.Read.cs @@ -7,7 +7,7 @@ using Xunit; namespace System.Text.Json.Serialization.Tests { - public static partial class ValueTests + public static partial class CollectionTests { [Fact] public static void Read_SpecializedCollection() diff --git a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Specialized.Write.cs b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Specialized.Write.cs index 2b88bbbff7c..74240e7541d 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Specialized.Write.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/CollectionTests/CollectionTests.Specialized.Write.cs @@ -7,7 +7,7 @@ using Xunit; namespace System.Text.Json.Serialization.Tests { - public static partial class ValueTests + public static partial class CollectionTests { [Fact] public static void Write_SpecializedCollection() diff --git a/src/libraries/System.Text.Json/tests/Serialization/TestClasses/TestClasses.ImmutableCollections.cs b/src/libraries/System.Text.Json/tests/Serialization/TestClasses/TestClasses.ImmutableCollections.cs index e28c63d43df..8296225d485 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/TestClasses/TestClasses.ImmutableCollections.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/TestClasses/TestClasses.ImmutableCollections.cs @@ -5,7 +5,6 @@ using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; -using System.Linq; using Xunit; namespace System.Text.Json.Serialization.Tests diff --git a/src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.cs b/src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.cs index 1f495efa27c..030c464e42b 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/Value.ReadTests.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Globalization; +using System.Runtime.CompilerServices; using Xunit; namespace System.Text.Json.Serialization.Tests -- 2.34.1