This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8570 from danmosemsft/danmosegetuninit
Add FormatterServices.GetUninitializedObject, partial for #8133
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/System.Runtime/tests/System/Runtime/Serialization/FormatterServices.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// 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 | ||
{ | ||
[Fact] | ||
public static void NoArgument_Throws_ArgumentNullException() | ||
{ | ||
Assert.Throws<ArgumentNullException>("type", () => FormatterServices.GetUninitializedObject(null)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(typeof(string))] | ||
[InlineData(typeof(int*))] | ||
public static void StringsAndPointers_Throw_ArgumentException(Type type) | ||
{ | ||
Assert.Throws<ArgumentException>(null /* really should be 'type' */, () => FormatterServices.GetUninitializedObject(type)); | ||
} | ||
|
||
[Fact] | ||
public static void InstantiatedArrays_Throw_ArgumentException() | ||
{ | ||
Assert.Throws<ArgumentException>(null /* really should be 'type' */, () => FormatterServices.GetUninitializedObject((new int[] { }).GetType())); | ||
} | ||
|
||
[Theory] | ||
[InlineData(typeof(Array))] | ||
[InlineData(typeof(ICollection))] | ||
[InlineData(typeof(Stream))] | ||
public static void InterfacesAndAbstractClasses_Throw_MemberAccessException(Type type) | ||
{ | ||
Assert.Throws<MemberAccessException>(() => FormatterServices.GetUninitializedObject(type)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(typeof(object))] | ||
[InlineData(typeof(MyClass))] | ||
public static void PlainObjects_Success(Type type) | ||
{ | ||
Assert.Equal(type, FormatterServices.GetUninitializedObject(type).GetType()); | ||
} | ||
|
||
[Fact] | ||
public static void Generic_Success() | ||
{ | ||
var type = typeof(List<int>); | ||
Assert.Equal(0, ((List<int>)FormatterServices.GetUninitializedObject(type)).Count); | ||
Assert.Equal(type, FormatterServices.GetUninitializedObject(type).GetType()); | ||
} | ||
|
||
[Theory] | ||
[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()); | ||
Assert.Equal(type, FormatterServices.GetUninitializedObject(type).GetType()); | ||
} | ||
|
||
[Fact] | ||
public static void Nullable_BecomesNonNullable_Success() | ||
{ | ||
Assert.Equal(typeof(int), FormatterServices.GetUninitializedObject(typeof(int?)).GetType()); | ||
} | ||
|
||
[Fact] | ||
public static void Result_Is_Mutable() | ||
{ | ||
// Sanity check the object is actually useable | ||
MyClass mc = ((MyClass)FormatterServices.GetUninitializedObject(typeof(MyClass))); | ||
mc.MyMember = "foo"; | ||
Assert.Equal("foo", mc.MyMember); | ||
} | ||
|
||
private class MyClass | ||
{ | ||
public string MyMember { get; set; } | ||
} | ||
} | ||
} |