From 1100b216225c3b62ec4bc2853824a95cf513a7ae Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Mon, 16 May 2016 08:54:24 -0700 Subject: [PATCH 1/5] Add FormatterServices.GetUninitializedObject, partial for #8133 --- src/System.Runtime/ref/System.Runtime.cs | 7 ++ .../tests/System.Runtime.Tests.csproj | 1 + .../Serialization/FormatterServices.cs | 91 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs diff --git a/src/System.Runtime/ref/System.Runtime.cs b/src/System.Runtime/ref/System.Runtime.cs index 96e942ac203d..09e1bbb70a9e 100644 --- a/src/System.Runtime/ref/System.Runtime.cs +++ b/src/System.Runtime/ref/System.Runtime.cs @@ -3054,6 +3054,13 @@ public StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind layoutKin public System.Runtime.InteropServices.LayoutKind Value { get { return default(System.Runtime.InteropServices.LayoutKind); } } } } +namespace System.Runtime.Serialization +{ + public static partial class FormatterServices + { + public static Object GetUninitializedObject(Type type) { return default(Object); } + } +} namespace System.Runtime.Versioning { [System.AttributeUsageAttribute((System.AttributeTargets)(1), AllowMultiple = false, Inherited = false)] diff --git a/src/System.Runtime/tests/System.Runtime.Tests.csproj b/src/System.Runtime/tests/System.Runtime.Tests.csproj index 766a718ee171..089dfb03c8e7 100644 --- a/src/System.Runtime/tests/System.Runtime.Tests.csproj +++ b/src/System.Runtime/tests/System.Runtime.Tests.csproj @@ -65,6 +65,7 @@ + diff --git a/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs b/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs new file mode 100644 index 000000000000..7704c3ae00a6 --- /dev/null +++ b/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs @@ -0,0 +1,91 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Runtime.Serialization; +using Xunit; + +namespace System.Tests +{ + public static class FormatterServicesTests + { + private static Func geo = FormatterServices.GetUninitializedObject; + + [Fact] + public static void NoArgument() + { + Assert.Throws(() => geo(null)); + } + + [Theory] + [InlineData(typeof(String))] + [InlineData(typeof(int*))] + public static void Throws_ArgumentException(Type type) + { + Assert.Throws(() => geo(type)); + } + + [Theory] + [InlineData(typeof(Array))] + [InlineData(typeof(ICollection))] + [InlineData(typeof(Stream))] + public static void Throws_MemberAccessException(Type type) + { + Assert.Throws(() => geo(type)); + } + + [Theory] + [InlineData(typeof(object))] + [InlineData(typeof(MyClass))] + public static void Types(Type type) + { + Assert.Equal(type, geo(type).GetType()); + } + + [Fact] + public static void Generic() + { + var type = typeof(List); + Assert.Equal(0, ((List)geo(type)).Count); + Assert.Equal(type, geo(type).GetType()); + } + + public static IEnumerable TestData() + { + yield return new object[] { typeof(Int32), 0 }; + yield return new object[] { typeof(short), 0 }; + } + + [Theory] + [MemberData(nameof(TestData))] + public static void Valid(Type type, Object value) + { + Assert.Equal(value.ToString(), geo(type).ToString()); + Assert.Equal(type, geo(type).GetType()); + } + + [Fact] + public static void Nullable() + { + Assert.Equal(typeof(Int32), geo(typeof(Int32?)).GetType()); + } + + [Fact] + public static void Mutable() + { + // Sanity check the object is actually useable + MyClass mc = ((MyClass)geo(typeof(MyClass))); + mc.MyMember = "foo"; + Assert.Equal("foo", mc.MyMember); + } + + private class MyClass + { + public string MyMember { get; set; } + } + } +} From 38ff7507ea708ea55f3a8dce60b8e5868b9b2fc7 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Mon, 16 May 2016 11:01:49 -0700 Subject: [PATCH 2/5] Update with feedback. --- .../Serialization/FormatterServices.cs | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs b/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs index 7704c3ae00a6..bd90d2862407 100644 --- a/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs +++ b/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs @@ -13,45 +13,49 @@ namespace System.Tests { public static class FormatterServicesTests { - private static Func geo = FormatterServices.GetUninitializedObject; - [Fact] - public static void NoArgument() + public static void NoArgument_Throws_ArgumentNullException() { - Assert.Throws(() => geo(null)); + Assert.Throws("type", () => FormatterServices.GetUninitializedObject(null)); } [Theory] [InlineData(typeof(String))] [InlineData(typeof(int*))] - public static void Throws_ArgumentException(Type type) + public static void StringsAndPointers_Throw_ArgumentException(Type type) { - Assert.Throws(() => geo(type)); + Assert.Throws(null /* really should be 'type' */, () => FormatterServices.GetUninitializedObject(type)); } - [Theory] + [Fact] + public static void InstantiatedArrays_Throw_ArgumentException() + { + Assert.Throws(null /* really should be 'type' */, () => FormatterServices.GetUninitializedObject((new int[] { }).GetType())); + } + + [Theory] [InlineData(typeof(Array))] [InlineData(typeof(ICollection))] [InlineData(typeof(Stream))] - public static void Throws_MemberAccessException(Type type) + public static void InterfacesAndAbstractClasses_Throw_MemberAccessException(Type type) { - Assert.Throws(() => geo(type)); + Assert.Throws(() => FormatterServices.GetUninitializedObject(type)); } [Theory] [InlineData(typeof(object))] [InlineData(typeof(MyClass))] - public static void Types(Type type) + public static void PlainObjects_Success(Type type) { - Assert.Equal(type, geo(type).GetType()); + Assert.Equal(type, FormatterServices.GetUninitializedObject(type).GetType()); } [Fact] - public static void Generic() + public static void Generic_Success() { var type = typeof(List); - Assert.Equal(0, ((List)geo(type)).Count); - Assert.Equal(type, geo(type).GetType()); + Assert.Equal(0, ((List)FormatterServices.GetUninitializedObject(type)).Count); + Assert.Equal(type, FormatterServices.GetUninitializedObject(type).GetType()); } public static IEnumerable TestData() @@ -62,23 +66,23 @@ public static IEnumerable TestData() [Theory] [MemberData(nameof(TestData))] - public static void Valid(Type type, Object value) + public static void PrimitiveTypes_Success(Type type, Object value) { - Assert.Equal(value.ToString(), geo(type).ToString()); - Assert.Equal(type, geo(type).GetType()); + Assert.Equal(value.ToString(), FormatterServices.GetUninitializedObject(type).ToString()); + Assert.Equal(type, FormatterServices.GetUninitializedObject(type).GetType()); } [Fact] - public static void Nullable() + public static void Nullable_BecomesNonNullable_Success() { - Assert.Equal(typeof(Int32), geo(typeof(Int32?)).GetType()); + Assert.Equal(typeof(Int32), FormatterServices.GetUninitializedObject(typeof(Int32?)).GetType()); } [Fact] - public static void Mutable() + public static void Result_Is_Mutable() { // Sanity check the object is actually useable - MyClass mc = ((MyClass)geo(typeof(MyClass))); + MyClass mc = ((MyClass)FormatterServices.GetUninitializedObject(typeof(MyClass))); mc.MyMember = "foo"; Assert.Equal("foo", mc.MyMember); } From c9c4734c8a8aaa3885b79f701dd9593253fc6593 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Mon, 16 May 2016 11:06:29 -0700 Subject: [PATCH 3/5] Fix tabs. --- .../Serialization/FormatterServices.cs | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs b/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs index bd90d2862407..765d907cb12e 100644 --- a/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs +++ b/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs @@ -22,10 +22,10 @@ public static void NoArgument_Throws_ArgumentNullException() [Theory] [InlineData(typeof(String))] [InlineData(typeof(int*))] - public static void StringsAndPointers_Throw_ArgumentException(Type type) - { + public static void StringsAndPointers_Throw_ArgumentException(Type type) + { Assert.Throws(null /* really should be 'type' */, () => FormatterServices.GetUninitializedObject(type)); - } + } [Fact] public static void InstantiatedArrays_Throw_ArgumentException() @@ -34,13 +34,13 @@ public static void InstantiatedArrays_Throw_ArgumentException() } [Theory] - [InlineData(typeof(Array))] + [InlineData(typeof(Array))] [InlineData(typeof(ICollection))] [InlineData(typeof(Stream))] public static void InterfacesAndAbstractClasses_Throw_MemberAccessException(Type type) - { - Assert.Throws(() => FormatterServices.GetUninitializedObject(type)); - } + { + Assert.Throws(() => FormatterServices.GetUninitializedObject(type)); + } [Theory] [InlineData(typeof(object))] @@ -67,10 +67,10 @@ public static IEnumerable TestData() [Theory] [MemberData(nameof(TestData))] public static void PrimitiveTypes_Success(Type type, Object value) - { - Assert.Equal(value.ToString(), FormatterServices.GetUninitializedObject(type).ToString()); - Assert.Equal(type, FormatterServices.GetUninitializedObject(type).GetType()); - } + { + Assert.Equal(value.ToString(), FormatterServices.GetUninitializedObject(type).ToString()); + Assert.Equal(type, FormatterServices.GetUninitializedObject(type).GetType()); + } [Fact] public static void Nullable_BecomesNonNullable_Success() @@ -90,6 +90,6 @@ public static void Result_Is_Mutable() private class MyClass { public string MyMember { get; set; } - } + } } } From 5c26a9111fba19135869e215e17ba7077e65eb7e Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Mon, 16 May 2016 12:24:42 -0700 Subject: [PATCH 4/5] Formatting feedback. --- .../System/Runtime/Serialization/FormatterServices.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs b/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs index 765d907cb12e..5ee44332eff2 100644 --- a/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs +++ b/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs @@ -20,7 +20,7 @@ public static void NoArgument_Throws_ArgumentNullException() } [Theory] - [InlineData(typeof(String))] + [InlineData(typeof(string))] [InlineData(typeof(int*))] public static void StringsAndPointers_Throw_ArgumentException(Type type) { @@ -60,13 +60,13 @@ public static void Generic_Success() public static IEnumerable TestData() { - yield return new object[] { typeof(Int32), 0 }; + yield return new object[] { typeof(int), 0 }; yield return new object[] { typeof(short), 0 }; } [Theory] [MemberData(nameof(TestData))] - public static void PrimitiveTypes_Success(Type type, Object value) + public static void PrimitiveTypes_Success(Type type, object value) { Assert.Equal(value.ToString(), FormatterServices.GetUninitializedObject(type).ToString()); Assert.Equal(type, FormatterServices.GetUninitializedObject(type).GetType()); @@ -75,7 +75,7 @@ public static void PrimitiveTypes_Success(Type type, Object value) [Fact] public static void Nullable_BecomesNonNullable_Success() { - Assert.Equal(typeof(Int32), FormatterServices.GetUninitializedObject(typeof(Int32?)).GetType()); + Assert.Equal(typeof(int), FormatterServices.GetUninitializedObject(typeof(int?)).GetType()); } [Fact] From 893414f2fa5860c493cbd5969ff6db222141d045 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Mon, 16 May 2016 12:34:47 -0700 Subject: [PATCH 5/5] Inline data fixup. --- .../System/Runtime/Serialization/FormatterServices.cs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs b/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs index 5ee44332eff2..0c765c0d0f86 100644 --- a/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs +++ b/src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs @@ -58,14 +58,9 @@ public static void Generic_Success() Assert.Equal(type, FormatterServices.GetUninitializedObject(type).GetType()); } - public static IEnumerable TestData() - { - yield return new object[] { typeof(int), 0 }; - yield return new object[] { typeof(short), 0 }; - } - [Theory] - [MemberData(nameof(TestData))] + [InlineData(typeof(int), 0)] + [InlineData(typeof(short), 0)] public static void PrimitiveTypes_Success(Type type, object value) { Assert.Equal(value.ToString(), FormatterServices.GetUninitializedObject(type).ToString());