Skip to content

Commit

Permalink
TestContext Properties type fixed to be IDictionary (#563)
Browse files Browse the repository at this point in the history
* Ensured that the TestContext exposed is always IDictionary for back compat reasons.
  • Loading branch information
cltshivash authored Feb 9, 2019
1 parent 11304f0 commit b231cad
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ public override DataRow DataRow
}

/// <inheritdoc/>
public override IDictionary<string, object> Properties
public override IDictionary Properties
{
get
{
return this.properties;
return this.properties as IDictionary;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -115,11 +116,11 @@ public override string TestName
/// An System.Collections.IDictionary object that contains key/value pairs that
/// represent the test properties.
/// </returns>
public override IDictionary<string, object> Properties
public override IDictionary Properties
{
get
{
return this.properties as IDictionary<string, object>;
return this.properties as IDictionary;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/TestFramework/Extension.Desktop/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Microsoft.VisualStudio.TestTools.UnitTesting
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
Expand All @@ -19,7 +20,7 @@ public abstract class TestContext
/// <summary>
/// Gets test properties for a test.
/// </summary>
public abstract IDictionary<string, object> Properties { get; }
public abstract IDictionary Properties { get; }

/// <summary>
/// Gets the current data row when test is used for data driven testing.
Expand Down
35 changes: 9 additions & 26 deletions src/TestFramework/Extension.Shared/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Microsoft.VisualStudio.TestTools.UnitTesting
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand All @@ -18,7 +19,7 @@ public abstract class TestContext
/// <summary>
/// Gets test properties for a test.
/// </summary>
public abstract IDictionary<string, object> Properties { get; }
public abstract IDictionary Properties { get; }

/// <summary>
/// Gets Fully-qualified name of the class containing the test method currently being executed
Expand All @@ -29,32 +30,17 @@ public abstract class TestContext
/// in the test results. Users can benefit from messages that include the fully-qualified
/// class name in addition to the name of the test method currently being executed.
/// </remarks>
public virtual string FullyQualifiedTestClassName
{
get
{
return this.GetProperty<string>("FullyQualifiedTestClassName");
}
}
public virtual string FullyQualifiedTestClassName => this.GetProperty<string>("FullyQualifiedTestClassName");

/// <summary>
/// Gets the Name of the test method currently being executed
/// </summary>
public virtual string TestName
{
get
{
return this.GetProperty<string>("TestName");
}
}
public virtual string TestName => this.GetProperty<string>("TestName");

/// <summary>
/// Gets the current test outcome.
/// </summary>
public virtual UnitTestOutcome CurrentTestOutcome
{
get { return UnitTestOutcome.Unknown; }
}
public virtual UnitTestOutcome CurrentTestOutcome => UnitTestOutcome.Unknown;

/// <summary>
/// Used to write trace messages while the test is running
Expand All @@ -72,21 +58,18 @@ public virtual UnitTestOutcome CurrentTestOutcome
private T GetProperty<T>(string name)
where T : class
{
object o;

if (!this.Properties.TryGetValue(name, out o))
if (!((IDictionary<string, object>)this.Properties).TryGetValue(name, out object propertyValue))
{
return null;
}

if (o != null && !(o is T))
if (propertyValue != null && !(propertyValue is T))
{
// If o has a value, but it's not the right type
Debug.Assert(false, "How did an invalid value get in here?");
throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, FrameworkMessages.InvalidPropertyType, name, o.GetType(), typeof(T)));
throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, FrameworkMessages.InvalidPropertyType, name, propertyValue.GetType(), typeof(T)));
}

return (T)o;
return (T)propertyValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ public static IDictionary<string, object> TestContextProperties
[UTF.TestCategory("Foo")]
public void PassingTest()
{
TestContextProperties = this.TestContext.Properties;
TestContextProperties = this.TestContext.Properties as IDictionary<string, object>;
}

[UTF.TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ public void GetTestMethodInfoShouldSetTestContextWithCustomProperty()
new Dictionary<string, object>());

this.typeCache.GetTestMethodInfo(testMethod, testContext, false);
var customProperty = testContext.Properties.FirstOrDefault(p => p.Key.Equals("WhoAmI"));
var customProperty = ((IDictionary<string, object>)testContext.Properties).FirstOrDefault(p => p.Key.Equals("WhoAmI"));

Assert.IsNotNull(customProperty);
Assert.AreEqual("Me", customProperty.Value);
Expand Down Expand Up @@ -1009,7 +1009,7 @@ public void GetTestMethodInfoShouldNotAddDuplicateTestPropertiesToTestContext()

// Verify that the first value gets set.
object value;
Assert.IsTrue(testContext.Properties.TryGetValue("WhoAmI", out value));
Assert.IsTrue(((IDictionary<string, object>)testContext.Properties).TryGetValue("WhoAmI", out value));
Assert.AreEqual("Me", value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public void GetTestContextShouldReturnAValidTestContext()
// Assert.
Assert.AreEqual("A.C.M", testContext.Context.FullyQualifiedTestClassName);
Assert.AreEqual("M", testContext.Context.TestName);
Assert.IsTrue(testContext.Context.Properties.Contains(properties.ToArray()[0]));
Assert.IsTrue(testContext.Context.Properties.Contains(properties.ToArray()[0].Key));
Assert.IsTrue(((IDictionary<string, object>)testContext.Context.Properties).Contains(properties.ToArray()[0]));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public void TestContextConstructorShouldInitializeDefaultProperties()
Assert.IsNotNull(this.testContextImplementation.Properties);

CollectionAssert.Contains(
this.testContextImplementation.Properties.ToList(),
this.testContextImplementation.Properties,
new KeyValuePair<string, object>("FullyQualifiedTestClassName", "A.C.M"));
CollectionAssert.Contains(
this.testContextImplementation.Properties.ToList(),
this.testContextImplementation.Properties,
new KeyValuePair<string, object>("TestName", "M"));
}

Expand Down Expand Up @@ -115,8 +115,8 @@ public void PropertiesShouldReturnPropertiesPassedToTestContext()

this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties);

CollectionAssert.Contains(this.testContextImplementation.Properties.ToList(), property1);
CollectionAssert.Contains(this.testContextImplementation.Properties.ToList(), property2);
CollectionAssert.Contains(this.testContextImplementation.Properties, property1);
CollectionAssert.Contains(this.testContextImplementation.Properties, property2);
}

[TestMethod]
Expand Down Expand Up @@ -158,11 +158,10 @@ public void TryGetPropertyValueShouldReturnFalseIfPropertyIsNotPresent()
public void AddPropertyShouldAddPropertiesToThePropertyBag()
{
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties);

this.testContextImplementation.AddProperty("SomeNewProperty", "SomeValue");

CollectionAssert.Contains(
this.testContextImplementation.Properties.ToList(),
this.testContextImplementation.Properties,
new KeyValuePair<string, object>("SomeNewProperty", "SomeValue"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ namespace MSTestAdapter.PlatformServices.Tests.Services
using System.Linq;

using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.ObjectModel;
using Moq;
using ITestMethod = Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.ObjectModel.ITestMethod;

Expand All @@ -48,7 +47,7 @@ public void TestInit()
[TestMethod]
public void TestContextConstructorShouldInitializeProperties()
{
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties);
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties);

Assert.IsNotNull(this.testContextImplementation.Properties);
}
Expand All @@ -59,30 +58,30 @@ public void TestContextConstructorShouldInitializeDefaultProperties()
this.testMethod.Setup(tm => tm.FullClassName).Returns("A.C.M");
this.testMethod.Setup(tm => tm.Name).Returns("M");

this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties);
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties);

Assert.IsNotNull(this.testContextImplementation.Properties);

CollectionAssert.Contains(
this.testContextImplementation.Properties.ToList(),
this.testContextImplementation.Properties,
new KeyValuePair<string, object>("FullyQualifiedTestClassName", "A.C.M"));
CollectionAssert.Contains(
this.testContextImplementation.Properties.ToList(),
this.testContextImplementation.Properties,
new KeyValuePair<string, object>("TestName", "M"));
}

[TestMethod]
public void CurrentTestOutcomeShouldReturnDefaultOutcome()
{
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties);
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties);

Assert.AreEqual(UnitTestOutcome.Failed, this.testContextImplementation.CurrentTestOutcome);
}

[TestMethod]
public void CurrentTestOutcomeShouldReturnOutcomeSet()
{
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties);
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties);

this.testContextImplementation.SetOutcome(UnitTestOutcome.InProgress);

Expand All @@ -94,7 +93,7 @@ public void FullyQualifiedTestClassNameShouldReturnTestMethodsFullClassName()
{
this.testMethod.Setup(tm => tm.FullClassName).Returns("A.C.M");

this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties);
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties);

Assert.AreEqual("A.C.M", this.testContextImplementation.FullyQualifiedTestClassName);
}
Expand All @@ -104,7 +103,7 @@ public void TestNameShouldReturnTestMethodsName()
{
this.testMethod.Setup(tm => tm.Name).Returns("M");

this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties);
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties);

Assert.AreEqual("M", this.testContextImplementation.TestName);
}
Expand All @@ -118,18 +117,18 @@ public void PropertiesShouldReturnPropertiesPassedToTestContext()
this.properties.Add(property1);
this.properties.Add(property2);

this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties);
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties);

CollectionAssert.Contains(this.testContextImplementation.Properties.ToList(), property1);
CollectionAssert.Contains(this.testContextImplementation.Properties.ToList(), property2);
CollectionAssert.Contains(this.testContextImplementation.Properties, property1);
CollectionAssert.Contains(this.testContextImplementation.Properties, property2);
}

[TestMethod]
public void ContextShouldReturnTestContextObject()
{
this.testMethod.Setup(tm => tm.Name).Returns("M");

this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties);
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties);

Assert.IsNotNull(this.testContextImplementation.Context);
Assert.AreEqual("M", this.testContextImplementation.Context.TestName);
Expand All @@ -140,34 +139,28 @@ public void TryGetPropertyValueShouldReturnTrueIfPropertyIsPresent()
{
this.testMethod.Setup(tm => tm.Name).Returns("M");

this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties);

object propValue;

Assert.IsTrue(this.testContextImplementation.TryGetPropertyValue("TestName", out propValue));
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties);
Assert.IsTrue(this.testContextImplementation.TryGetPropertyValue("TestName", out object propValue));
Assert.AreEqual("M", propValue);
}

[TestMethod]
public void TryGetPropertyValueShouldReturnFalseIfPropertyIsNotPresent()
{
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties);

object propValue;

Assert.IsFalse(this.testContextImplementation.TryGetPropertyValue("Random", out propValue));
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties);
Assert.IsFalse(this.testContextImplementation.TryGetPropertyValue("Random", out object propValue));
Assert.IsNull(propValue);
}

[TestMethod]
public void AddPropertyShouldAddPropertiesToThePropertyBag()
{
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new System.IO.StringWriter(), this.properties);
this.testContextImplementation = new TestContextImplementation(this.testMethod.Object, new StringWriter(), this.properties);

this.testContextImplementation.AddProperty("SomeNewProperty", "SomeValue");

CollectionAssert.Contains(
this.testContextImplementation.Properties.ToList(),
this.testContextImplementation.Properties,
new KeyValuePair<string, object>("SomeNewProperty", "SomeValue"));
}

Expand Down

0 comments on commit b231cad

Please sign in to comment.