From 6a9bfee0c9cd327d4ff4f40d02208ed90135e3ea Mon Sep 17 00:00:00 2001 From: Robin Lindner Date: Tue, 21 Dec 2021 15:55:05 +0100 Subject: [PATCH] Expose AppContext.SetData in ref assembly (#62996) * Expose AppContext.SetData in ref assembly * Implement test for Set/GetData * Add doc comment --- .../src/System/AppContext.cs | 6 ++++ .../System.Runtime/ref/System.Runtime.cs | 1 + .../tests/System.Runtime.Tests.csproj | 5 +-- ...s => AppContextTests.Switch.Validation.cs} | 0 ...xt.Switch.cs => AppContextTests.Switch.cs} | 0 .../System/AppContext/AppContextTests.cs | 32 +++++++++++++++++++ 6 files changed, 42 insertions(+), 2 deletions(-) rename src/libraries/System.Runtime/tests/System/AppContext/{AppContext.Switch.Validation.cs => AppContextTests.Switch.Validation.cs} (100%) rename src/libraries/System.Runtime/tests/System/AppContext/{AppContext.Switch.cs => AppContextTests.Switch.cs} (100%) create mode 100644 src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.cs diff --git a/src/libraries/System.Private.CoreLib/src/System/AppContext.cs b/src/libraries/System.Private.CoreLib/src/System/AppContext.cs index 35e45f472bb..6b28c7b2a56 100644 --- a/src/libraries/System.Private.CoreLib/src/System/AppContext.cs +++ b/src/libraries/System.Private.CoreLib/src/System/AppContext.cs @@ -45,6 +45,12 @@ public static partial class AppContext return data; } + /// + /// Sets the value of the named data element assigned to the current application domain. + /// + /// The name of the data element + /// The value of + /// If is public static void SetData(string name, object? data) { if (name == null) diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index f9462613a69..435594f5b94 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -112,6 +112,7 @@ public static partial class AppContext public static string BaseDirectory { get { throw null; } } public static string? TargetFrameworkName { get { throw null; } } public static object? GetData(string name) { throw null; } + public static void SetData(string name, object? data) { } public static void SetSwitch(string switchName, bool isEnabled) { } public static bool TryGetSwitch(string switchName, out bool isEnabled) { throw null; } } diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj b/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj index 805d3610003..92eb65f2c0e 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj @@ -144,8 +144,9 @@ - - + + + diff --git a/src/libraries/System.Runtime/tests/System/AppContext/AppContext.Switch.Validation.cs b/src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.Switch.Validation.cs similarity index 100% rename from src/libraries/System.Runtime/tests/System/AppContext/AppContext.Switch.Validation.cs rename to src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.Switch.Validation.cs diff --git a/src/libraries/System.Runtime/tests/System/AppContext/AppContext.Switch.cs b/src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.Switch.cs similarity index 100% rename from src/libraries/System.Runtime/tests/System/AppContext/AppContext.Switch.cs rename to src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.Switch.cs diff --git a/src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.cs b/src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.cs new file mode 100644 index 00000000000..9b47feed876 --- /dev/null +++ b/src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.cs @@ -0,0 +1,32 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Xunit; + +namespace System.Tests +{ + public partial class AppContextTests + { + [Theory] + [InlineData("AppContext_Case1", 123)] + [InlineData("AppContext_Case2", "")] + [InlineData("AppContext_Case3", null)] + public void AppContext_GetSetDataTest(string dataKey, object value) + { + // Set data + AppContext.SetData(dataKey, value); + + // Get previously set data + object actual = AppContext.GetData(dataKey); + + // Validate instance equality + Assert.Same(value, actual); + } + + [Fact] + public void AppContext_ThrowTest() + { + AssertExtensions.Throws("name", () => AppContext.SetData(null, 123)); + } + } +} -- GitLab