Skip to content

Commit 957a88e

Browse files
committed
Revert "Update TestContext.Properties return type to IDictionary<string, object?> (#1481)"
This reverts commit f277097.
1 parent 32e667e commit 957a88e

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

src/Adapter/MSTestAdapter.PlatformServices/PublicAPI/PublicAPI.Shipped.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TraceListener
130130
Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TraceListenerWrapper.TraceListenerWrapper(System.IO.TextWriter! textWriter) -> void
131131
override Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation.AddResultFile(string! fileName) -> void
132132
override Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation.CurrentTestOutcome.get -> Microsoft.VisualStudio.TestTools.UnitTesting.UnitTestOutcome
133-
override Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation.Properties.get -> System.Collections.Generic.IDictionary<string!, object?>!
133+
override Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation.Properties.get -> System.Collections.IDictionary!
134134
override Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation.Write(string! format, params object?[]! args) -> void
135135
override Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation.Write(string? message) -> void
136136
override Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation.WriteLine(string! format, params object?[]! args) -> void

src/Adapter/MSTestAdapter.PlatformServices/Services/TestContextImplementation.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public class TestContextImplementation : TestContext, ITestContext
4545
/// </summary>
4646
private bool _stringWriterDisposed = false;
4747

48+
/// <summary>
49+
/// Properties.
50+
/// </summary>
51+
private IDictionary<string, object?> _properties;
52+
4853
/// <summary>
4954
/// Unit test outcome.
5055
/// </summary>
@@ -83,7 +88,7 @@ public TestContextImplementation(ITestMethod testMethod, StringWriter stringWrit
8388

8489
// Cannot get this type in constructor directly, because all signatures for all platforms need to be the same.
8590
_threadSafeStringWriter = (ThreadSafeStringWriter)stringWriter;
86-
Properties = new Dictionary<string, object?>(properties)
91+
_properties = new Dictionary<string, object?>(properties)
8792
{
8893
[FullyQualifiedTestClassNameLabel] = _testMethod.FullClassName,
8994
[ManagedTypeLabel] = _testMethod.ManagedTypeName,
@@ -110,7 +115,7 @@ public TestContextImplementation(ITestMethod testMethod, StringWriter stringWrit
110115
#endif
111116

112117
/// <inheritdoc/>
113-
public override IDictionary<string, object?> Properties { get; }
118+
public override IDictionary Properties => (IDictionary)_properties;
114119

115120
#if !WINDOWS_UWP && !WIN_UI
116121
/// <inheritdoc/>
@@ -291,13 +296,13 @@ public void SetDataConnection(object? dbConnection)
291296
/// <returns>True if found.</returns>
292297
public bool TryGetPropertyValue(string propertyName, out object? propertyValue)
293298
{
294-
if (Properties == null)
299+
if (_properties == null)
295300
{
296301
propertyValue = null;
297302
return false;
298303
}
299304

300-
return Properties.TryGetValue(propertyName, out propertyValue);
305+
return _properties.TryGetValue(propertyName, out propertyValue);
301306
}
302307

303308
/// <summary>
@@ -307,7 +312,7 @@ public bool TryGetPropertyValue(string propertyName, out object? propertyValue)
307312
/// <param name="propertyValue">The property value.</param>
308313
public void AddProperty(string propertyName, string propertyValue)
309314
{
310-
Properties.Add(propertyName, propertyValue);
315+
_properties.Add(propertyName, propertyValue);
311316
}
312317

313318
/// <summary>

src/TestFramework/TestFramework.Extensions/PublicAPI/PublicAPI.Shipped.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#nullable enable
22
abstract Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.AddResultFile(string! fileName) -> void
3-
abstract Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.Properties.get -> System.Collections.Generic.IDictionary<string!, object?>!
3+
abstract Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.Properties.get -> System.Collections.IDictionary!
44
abstract Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.Write(string! format, params object?[]! args) -> void
55
abstract Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.Write(string? message) -> void
66
abstract Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.WriteLine(string! format, params object?[]! args) -> void

src/TestFramework/TestFramework.Extensions/TestContext.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public abstract class TestContext
4848
/// <summary>
4949
/// Gets test properties for a test.
5050
/// </summary>
51-
public abstract IDictionary<string, object?> Properties { get; }
51+
public abstract IDictionary Properties { get; }
5252

5353
/// <summary>
5454
/// Gets or sets the cancellation token source. This token source is canceled when test times out. Also when explicitly canceled the test will be aborted.
@@ -195,10 +195,15 @@ public abstract class TestContext
195195
where T : class
196196
{
197197
DebugEx.Assert(Properties is not null, "Properties is null");
198-
if (!Properties.TryGetValue(name, out object? propertyValue))
198+
#if WINDOWS_UWP || WIN_UI
199+
if (!((IDictionary<string, object>)Properties).TryGetValue(name, out object? propertyValue))
199200
{
200201
return null;
201202
}
203+
#else
204+
// This old API doesn't throw when key is not found, but returns null.
205+
object? propertyValue = Properties[name];
206+
#endif
202207

203208
// If propertyValue has a value, but it's not the right type
204209
if (propertyValue is not null and not T)

test/UnitTests/MSTestAdapter.UnitTests/PlatformServiceProviderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void GetTestContextShouldReturnAValidTestContext()
8383
// Assert.
8484
Verify(testContext.Context.FullyQualifiedTestClassName == "A.C.M");
8585
Verify(testContext.Context.TestName == "M");
86-
Verify(testContext.Context.Properties.ContainsKey(properties.ToArray()[0].Key));
86+
Verify(testContext.Context.Properties.Contains(properties.ToArray()[0].Key));
8787
Verify(((IDictionary<string, object>)testContext.Context.Properties).Contains(properties.ToArray()[0]));
8888
}
8989
}

0 commit comments

Comments
 (0)