Skip to content

Commit f277097

Browse files
authored
Update TestContext.Properties return type to IDictionary<string, object?> (#1481)
1 parent d1c91dc commit f277097

File tree

5 files changed

+10
-20
lines changed

5 files changed

+10
-20
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.IDictionary!
133+
override Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation.Properties.get -> System.Collections.Generic.IDictionary<string!, object?>!
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: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,6 @@ public class TestContextImplementation : TestContext, ITestContext
4646
/// </summary>
4747
private bool _stringWriterDisposed = false;
4848

49-
/// <summary>
50-
/// Properties.
51-
/// </summary>
52-
private IDictionary<string, object?> _properties;
53-
5449
/// <summary>
5550
/// Unit test outcome.
5651
/// </summary>
@@ -89,7 +84,7 @@ public TestContextImplementation(ITestMethod testMethod, StringWriter stringWrit
8984

9085
// Cannot get this type in constructor directly, because all signatures for all platforms need to be the same.
9186
_threadSafeStringWriter = (ThreadSafeStringWriter)stringWriter;
92-
_properties = new Dictionary<string, object?>(properties)
87+
Properties = new Dictionary<string, object?>(properties)
9388
{
9489
[FullyQualifiedTestClassNameLabel] = _testMethod.FullClassName,
9590
[ManagedTypeLabel] = _testMethod.ManagedTypeName,
@@ -116,7 +111,7 @@ public TestContextImplementation(ITestMethod testMethod, StringWriter stringWrit
116111
#endif
117112

118113
/// <inheritdoc/>
119-
public override IDictionary Properties => (IDictionary)_properties;
114+
public override IDictionary<string, object?> Properties { get; }
120115

121116
#if !WINDOWS_UWP && !WIN_UI
122117
/// <inheritdoc/>
@@ -297,13 +292,13 @@ public void SetDataConnection(object? dbConnection)
297292
/// <returns>True if found.</returns>
298293
public bool TryGetPropertyValue(string propertyName, out object? propertyValue)
299294
{
300-
if (_properties == null)
295+
if (Properties == null)
301296
{
302297
propertyValue = null;
303298
return false;
304299
}
305300

306-
return _properties.TryGetValue(propertyName, out propertyValue);
301+
return Properties.TryGetValue(propertyName, out propertyValue);
307302
}
308303

309304
/// <summary>
@@ -313,7 +308,7 @@ public bool TryGetPropertyValue(string propertyName, out object? propertyValue)
313308
/// <param name="propertyValue">The property value.</param>
314309
public void AddProperty(string propertyName, string propertyValue)
315310
{
316-
_properties.Add(propertyName, propertyValue);
311+
Properties.Add(propertyName, propertyValue);
317312
}
318313

319314
/// <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.IDictionary!
3+
abstract Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.Properties.get -> System.Collections.Generic.IDictionary<string!, object?>!
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: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public abstract class TestContext
4949
/// <summary>
5050
/// Gets test properties for a test.
5151
/// </summary>
52-
public abstract IDictionary Properties { get; }
52+
public abstract IDictionary<string, object?> Properties { get; }
5353

5454
/// <summary>
5555
/// 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.
@@ -196,15 +196,10 @@ public abstract class TestContext
196196
where T : class
197197
{
198198
DebugEx.Assert(Properties is not null, "Properties is null");
199-
#if WINDOWS_UWP || WIN_UI
200-
if (!((IDictionary<string, object>)Properties).TryGetValue(name, out object? propertyValue))
199+
if (!Properties.TryGetValue(name, out object? propertyValue))
201200
{
202201
return null;
203202
}
204-
#else
205-
// This old API doesn't throw when key is not found, but returns null.
206-
object? propertyValue = Properties[name];
207-
#endif
208203

209204
// If propertyValue has a value, but it's not the right type
210205
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.Contains(properties.ToArray()[0].Key));
86+
Verify(testContext.Context.Properties.ContainsKey(properties.ToArray()[0].Key));
8787
Verify(((IDictionary<string, object>)testContext.Context.Properties).Contains(properties.ToArray()[0]));
8888
}
8989
}

0 commit comments

Comments
 (0)