From ceb498aaca77ce28a095040e71ee180ee712e85b Mon Sep 17 00:00:00 2001 From: Robin Lindner Date: Sun, 19 Dec 2021 19:32:27 +0100 Subject: [PATCH 1/9] Expose AppContext.SetData in ref assembly --- src/libraries/System.Runtime/ref/System.Runtime.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index f9462613a698b3..435594f5b9402e 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; } } From 4ccaeca55bcd347c80db7f14fd21cd1bafde1a84 Mon Sep 17 00:00:00 2001 From: Robin Lindner Date: Sun, 19 Dec 2021 21:39:57 +0100 Subject: [PATCH 2/9] Implement test for Set/GetData --- .../tests/System.Runtime.Tests.csproj | 1 + .../tests/System/AppContext/AppContext.cs | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj b/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj index 805d3610003be0..76889c30eed51e 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj @@ -35,6 +35,7 @@ + diff --git a/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs b/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs new file mode 100644 index 00000000000000..3a57895bc298a8 --- /dev/null +++ b/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs @@ -0,0 +1,29 @@ +// 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 data equality + Assert.Equal(value, actual); + + // Validate instance equality + Assert.Same(value, actual); + } + } +} From d94662359ebea69d8dfd79730370903bd2ad35d1 Mon Sep 17 00:00:00 2001 From: Robin Lindner Date: Mon, 20 Dec 2021 15:24:52 +0100 Subject: [PATCH 3/9] Add doc comment --- .../System.Private.CoreLib/src/System/AppContext.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/AppContext.cs b/src/libraries/System.Private.CoreLib/src/System/AppContext.cs index 35e45f472bb8b1..6b28c7b2a561d7 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) From 278ebbc99e47b4125bdbedff763f5bf0b6cc075d Mon Sep 17 00:00:00 2001 From: Robin Lindner Date: Mon, 20 Dec 2021 15:34:21 +0100 Subject: [PATCH 4/9] Reduce unnessecary validation in Theory Co-authored-by: Jan Kotas --- .../System.Runtime/tests/System/AppContext/AppContext.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs b/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs index 3a57895bc298a8..3a0b9c1f9a0fff 100644 --- a/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs +++ b/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs @@ -19,9 +19,6 @@ public void AppContext_GetSetDataTest(string dataKey, object value) // Get previously set data object actual = AppContext.GetData(dataKey); - // Validate data equality - Assert.Equal(value, actual); - // Validate instance equality Assert.Same(value, actual); } From 1316bdc62e30e0ba0eacb4366bf532fe5e61d183 Mon Sep 17 00:00:00 2001 From: Robin Lindner Date: Mon, 20 Dec 2021 15:42:18 +0100 Subject: [PATCH 5/9] Add Test case for name == null --- .../System.Runtime/tests/System/AppContext/AppContext.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs b/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs index 3a57895bc298a8..b7293b813d662c 100644 --- a/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs +++ b/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs @@ -25,5 +25,12 @@ public void AppContext_GetSetDataTest(string dataKey, object value) // Validate instance equality Assert.Same(value, actual); } + + [Fact] + public void AppContext_ThrowTest() + { + ArgumentNullException exception = Assert.Throws(() => AppContext.SetData(null, 123)); + Assert.Contains("name", exception.Message); + } } } From c04a672521bc99c6eaa064d7b6edc9b344438225 Mon Sep 17 00:00:00 2001 From: Robin Lindner Date: Mon, 20 Dec 2021 20:24:53 +0100 Subject: [PATCH 6/9] Update Test name Co-authored-by: Jan Kotas --- .../System.Runtime/tests/System/AppContext/AppContext.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs b/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs index 154b0aaf2a6380..f548e723c3f33b 100644 --- a/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs +++ b/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs @@ -24,7 +24,7 @@ public void AppContext_GetSetDataTest(string dataKey, object value) } [Fact] - public void AppContext_ThrowTest() + public void SetData_ThrowTest() { ArgumentNullException exception = Assert.Throws(() => AppContext.SetData(null, 123)); Assert.Contains("name", exception.Message); From c3da55b815cc5be13358cf040ef4636ce4ce12a6 Mon Sep 17 00:00:00 2001 From: Robin Lindner Date: Mon, 20 Dec 2021 20:24:58 +0100 Subject: [PATCH 7/9] Update Test name Co-authored-by: Jan Kotas --- .../System.Runtime/tests/System/AppContext/AppContext.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs b/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs index f548e723c3f33b..79b6f4d109c3d2 100644 --- a/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs +++ b/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs @@ -11,7 +11,7 @@ public partial class AppContextTests [InlineData("AppContext_Case1", 123)] [InlineData("AppContext_Case2", "")] [InlineData("AppContext_Case3", null)] - public void AppContext_GetSetDataTest(string dataKey, object value) + public void GetSetDataTest(string dataKey, object value) { // Set data AppContext.SetData(dataKey, value); From abb5e2fdecf2070e323ddb65387ac69d87f8acab Mon Sep 17 00:00:00 2001 From: Robin Lindner Date: Mon, 20 Dec 2021 20:29:31 +0100 Subject: [PATCH 8/9] Usage of AssertExtensions.Throws instead of Assert.Throws --- .../System.Runtime/tests/System/AppContext/AppContext.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs b/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs index 79b6f4d109c3d2..9b47feed876c80 100644 --- a/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs +++ b/src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs @@ -11,7 +11,7 @@ public partial class AppContextTests [InlineData("AppContext_Case1", 123)] [InlineData("AppContext_Case2", "")] [InlineData("AppContext_Case3", null)] - public void GetSetDataTest(string dataKey, object value) + public void AppContext_GetSetDataTest(string dataKey, object value) { // Set data AppContext.SetData(dataKey, value); @@ -24,10 +24,9 @@ public void GetSetDataTest(string dataKey, object value) } [Fact] - public void SetData_ThrowTest() + public void AppContext_ThrowTest() { - ArgumentNullException exception = Assert.Throws(() => AppContext.SetData(null, 123)); - Assert.Contains("name", exception.Message); + AssertExtensions.Throws("name", () => AppContext.SetData(null, 123)); } } } From d2a53027a72927fc6331402b424dfa02fb117e06 Mon Sep 17 00:00:00 2001 From: Robin Lindner Date: Mon, 20 Dec 2021 20:30:30 +0100 Subject: [PATCH 9/9] Fix Test file naming --- .../System.Runtime/tests/System.Runtime.Tests.csproj | 6 +++--- ...h.Validation.cs => AppContextTests.Switch.Validation.cs} | 0 .../{AppContext.Switch.cs => AppContextTests.Switch.cs} | 0 .../System/AppContext/{AppContext.cs => AppContextTests.cs} | 0 4 files changed, 3 insertions(+), 3 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%) rename src/libraries/System.Runtime/tests/System/AppContext/{AppContext.cs => AppContextTests.cs} (100%) diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj b/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj index 76889c30eed51e..92eb65f2c0ebce 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests.csproj @@ -35,7 +35,6 @@ - @@ -145,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/AppContext.cs b/src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.cs similarity index 100% rename from src/libraries/System.Runtime/tests/System/AppContext/AppContext.cs rename to src/libraries/System.Runtime/tests/System/AppContext/AppContextTests.cs