-
Notifications
You must be signed in to change notification settings - Fork 254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic data display name #373
Changes from 3 commits
b7efb25
09c30cf
0b428fd
11011dc
b2bb90e
13c83c5
d0af564
80192b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,7 +56,7 @@ public DynamicDataAttribute(string dynamicDataSourceName, DynamicDataSourceType | |
/// Initializes a new instance of the <see cref="DynamicDataAttribute"/> class. | ||
/// </summary> | ||
/// <param name="dynamicDataSourceName"> | ||
/// The type in which the test data is declared (as property or in method). | ||
/// The name of method or property having test data. | ||
/// </param> | ||
/// <param name="dynamicDataDeclaringType"> | ||
/// The declaring type of property or method having data. | ||
|
@@ -70,6 +70,16 @@ public DynamicDataAttribute(string dynamicDataSourceName, Type dynamicDataDeclar | |
this.dynamicDataDeclaringType = dynamicDataDeclaringType; | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the name of method used to customize the display name in test results. | ||
/// </summary> | ||
public string DynamicDisplayName { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the declaring type used to customize the display name in test results. | ||
/// </summary> | ||
public Type DynamicDisplayNameDeclaringType { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. "DynamicDataDisplayNameDeclaringType" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
/// <inheritdoc /> | ||
public IEnumerable<object[]> GetData(MethodInfo methodInfo) | ||
{ | ||
|
@@ -131,7 +141,33 @@ public IEnumerable<object[]> GetData(MethodInfo methodInfo) | |
/// <inheritdoc /> | ||
public string GetDisplayName(MethodInfo methodInfo, object[] data) | ||
{ | ||
if (data != null) | ||
if (this.DynamicDisplayName != null) | ||
{ | ||
var dynamicDisplayNameDeclaringType = this.DynamicDisplayNameDeclaringType ?? methodInfo.DeclaringType; | ||
|
||
var method = dynamicDisplayNameDeclaringType.GetTypeInfo().GetDeclaredMethod(this.DynamicDisplayName); | ||
if (method == null) | ||
{ | ||
throw new ArgumentNullException(string.Format("{0} {1}", DynamicDataSourceType.Method, this.DynamicDisplayName)); | ||
} | ||
|
||
var parameters = method.GetParameters(); | ||
if (parameters.Length != 2 || | ||
parameters[0].ParameterType != typeof(MethodInfo) || | ||
parameters[1].ParameterType != typeof(object[]) || | ||
method.ReturnType != typeof(string)) | ||
{ | ||
throw new ArgumentNullException( | ||
string.Format( | ||
FrameworkMessages.DynamicDataDisplayName, | ||
this.DynamicDisplayName, | ||
typeof(string).Name, | ||
string.Join(", ", typeof(MethodInfo).Name, typeof(object[]).Name))); | ||
} | ||
|
||
return method.Invoke(null, new object[] { methodInfo, data }) as string; | ||
} | ||
else if (data != null) | ||
{ | ||
return string.Format(CultureInfo.CurrentCulture, FrameworkMessages.DataDrivenResultDisplayName, methodInfo.Name, string.Join(",", data.AsEnumerable())); | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -283,4 +283,7 @@ Stack Trace: {4}</value> | |
<data name="DynamicDataValueNull" xml:space="preserve"> | ||
<value>Value returned by property or method {0} shouldn't be null.</value> | ||
</data> | ||
<data name="DynamicDataDisplayName" xml:space="preserve"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also please run "build.cmd -uxlf" so the string addition is localizable. You would have to commit the xlf files changes post running the script. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ran this and have committed the xlf files. I'm assuming someone or some magic will do the translation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes please. We just needed modified xlf files. We will take care of the translation 😄 |
||
<value>Method {0} must match the expected signature: {1} {0}({2}).</value> | ||
</data> | ||
</root> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,5 +81,20 @@ public void GetDisplayNameShouldReturnAppropriateName() | |
displayName = dataRowAttribute.GetDisplayName(this.testMethodInfo, data2); | ||
Assert.AreEqual("DataRowTestMethod (First,,Second)", displayName); | ||
} | ||
|
||
[TestMethod] | ||
public void GetDisplayNameShouldReturnAppropriateNameWithDataRowDisplayName() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please consider changing testmethod name to GetDisplayNameShouldReturnProvidedDisplayName() or GetDisplayNameShouldReturnSpecifiedDisplayName() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
{ | ||
var dataRowAttribute = new DataRowAttribute(null); | ||
dataRowAttribute.DisplayName = "DataRowTestWithDisplayName"; | ||
|
||
this.dummyTestClass = new DummyTestClass(); | ||
this.testMethodInfo = this.dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("DataRowTestMethod"); | ||
|
||
var data = new string[] { "First", "Second", null }; | ||
|
||
var displayName = dataRowAttribute.GetDisplayName(this.testMethodInfo, data); | ||
Assert.AreEqual("DataRowTestWithDisplayName", displayName); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,7 +60,7 @@ public void GetDataShouldReadDataFromProperty() | |
public void GetDataShouldReadDataFromPropertyInDifferntClass() | ||
{ | ||
var methodInfo = this.dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod1"); | ||
this.dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataProperty", typeof(DummyTestClass)); | ||
this.dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataProperty2", typeof(DummyTestClass2)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cheers 😄 |
||
var data = this.dynamicDataAttribute.GetData(methodInfo); | ||
Assert.IsTrue(data is IEnumerable<object[]>); | ||
Assert.IsTrue(data.ToList().Count == 2); | ||
|
@@ -80,7 +80,7 @@ public void GetDataShouldReadDataFromMethod() | |
public void GetDataShouldReadDataFromMethodInDifferentClass() | ||
{ | ||
var methodInfo = this.dummyTestClass.GetType().GetTypeInfo().GetDeclaredMethod("TestMethod2"); | ||
this.dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataMethod", typeof(DummyTestClass), DynamicDataSourceType.Method); | ||
this.dynamicDataAttribute = new DynamicDataAttribute("ReusableTestDataMethod2", typeof(DummyTestClass2), DynamicDataSourceType.Method); | ||
var data = this.dynamicDataAttribute.GetData(methodInfo); | ||
Assert.IsTrue(data is IEnumerable<object[]>); | ||
Assert.IsTrue(data.ToList().Count == 2); | ||
|
@@ -121,6 +121,41 @@ public void GetDisplayNameShouldReturnDisplayName() | |
Assert.AreEqual("TestMethod1 (1,2,3)", displayName); | ||
} | ||
|
||
[TestFrameworkV1.TestMethod] | ||
public void GetDisplayNameShouldReturnDisplayNameWithDataRowDisplayName() | ||
{ | ||
var data = new object[] { 1, 2, 3 }; | ||
|
||
this.dynamicDataAttribute.DynamicDisplayName = "GetCustomDynamicDataDisplayName"; | ||
var displayName = this.dynamicDataAttribute.GetDisplayName(this.testMethodInfo, data); | ||
Assert.AreEqual("DynamicDataTestWithDisplayName TestMethod1 with 3 parameters", displayName); | ||
} | ||
|
||
[TestFrameworkV1.TestMethod] | ||
public void GetDisplayNameShouldReturnDisplayNameWithDataRowDisplayNameInDifferentClass() | ||
{ | ||
var data = new object[] { 1, 2, 3 }; | ||
|
||
this.dynamicDataAttribute.DynamicDisplayName = "GetCustomDynamicDataDisplayName2"; | ||
this.dynamicDataAttribute.DynamicDisplayNameDeclaringType = typeof(DummyTestClass2); | ||
var displayName = this.dynamicDataAttribute.GetDisplayName(this.testMethodInfo, data); | ||
Assert.AreEqual("DynamicDataTestWithDisplayName TestMethod1 with 3 parameters", displayName); | ||
} | ||
|
||
[TestFrameworkV1.TestMethod] | ||
public void GetDisplayNameShouldThrowExceptionIfMethodDoesNotMatchExpectedSignature() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add Tests for cases like :
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Also picked up a bug where I wasn't checking that the method was static. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome 👍 |
||
{ | ||
Action action = () => | ||
{ | ||
var data = new object[] { 1, 2, 3 }; | ||
|
||
this.dynamicDataAttribute.DynamicDisplayName = "InvalidCustomDynamicDataDisplayName"; | ||
var displayName = this.dynamicDataAttribute.GetDisplayName(this.testMethodInfo, data); | ||
}; | ||
|
||
ActionUtility.ActionShouldThrowExceptionOfType(action, typeof(ArgumentNullException)); | ||
} | ||
|
||
[TestFrameworkV1.TestMethod] | ||
public void GetDisplayNameShouldReturnEmptyStringIfDataIsNull() | ||
{ | ||
|
@@ -195,6 +230,30 @@ public static IEnumerable<object[]> ReusableTestDataMethod() | |
return new[] { new object[] { 1, 2, 3 }, new object[] { 4, 5, 6 } }; | ||
} | ||
|
||
/// <summary> | ||
/// The custom display name method. | ||
/// </summary> | ||
/// <param name="methodInfo"> | ||
/// The method info of test method. | ||
/// </param> | ||
/// <param name="data"> | ||
/// The test data which is passed to test method. | ||
/// </param> | ||
/// <returns> | ||
/// The <see cref="string"/>. | ||
/// </returns> | ||
public static string GetCustomDynamicDataDisplayName(MethodInfo methodInfo, object[] data) | ||
{ | ||
return string.Format("DynamicDataTestWithDisplayName {0} with {1} parameters", methodInfo.Name, data.Length); | ||
} | ||
|
||
/// <summary> | ||
/// Invalid custom display name method. | ||
/// </summary> | ||
public static void InvalidCustomDynamicDataDisplayName() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// The test method 1. | ||
/// </summary> | ||
|
@@ -242,4 +301,46 @@ public void DataRowTestMethod() | |
{ | ||
} | ||
} | ||
|
||
public class DummyTestClass2 | ||
{ | ||
/// <summary> | ||
/// Gets the reusable test data property. | ||
/// </summary> | ||
public static IEnumerable<object[]> ReusableTestDataProperty2 | ||
{ | ||
get | ||
{ | ||
return new[] { new object[] { 1, 2, 3 }, new object[] { 4, 5, 6 } }; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The reusable test data method. | ||
/// </summary> | ||
/// <returns> | ||
/// The <see cref="IEnumerable"/>. | ||
/// </returns> | ||
public static IEnumerable<object[]> ReusableTestDataMethod2() | ||
{ | ||
return new[] { new object[] { 1, 2, 3 }, new object[] { 4, 5, 6 } }; | ||
} | ||
|
||
/// <summary> | ||
/// The custom display name method. | ||
/// </summary> | ||
/// <param name="methodInfo"> | ||
/// The method info of test method. | ||
/// </param> | ||
/// <param name="data"> | ||
/// The test data which is passed to test method. | ||
/// </param> | ||
/// <returns> | ||
/// The <see cref="string"/>. | ||
/// </returns> | ||
public static string GetCustomDynamicDataDisplayName2(MethodInfo methodInfo, object[] data) | ||
{ | ||
return string.Format("DynamicDataTestWithDisplayName {0} with {1} parameters", methodInfo.Name, data.Length); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider renaming this to "DynamicDataDisplayName"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done