Skip to content
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

Merged
merged 8 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions src/TestFramework/MSTest.Core/Attributes/DynamicDataAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 DynamicDataDisplayName { get; set; }

/// <summary>
/// Gets or sets the declaring type used to customize the display name in test results.
/// </summary>
public Type DynamicDataDisplayNameDeclaringType { get; set; }

/// <inheritdoc />
public IEnumerable<object[]> GetData(MethodInfo methodInfo)
{
Expand Down Expand Up @@ -131,12 +141,40 @@ public IEnumerable<object[]> GetData(MethodInfo methodInfo)
/// <inheritdoc />
public string GetDisplayName(MethodInfo methodInfo, object[] data)
{
if (data != null)
if (this.DynamicDataDisplayName != null)
{
var dynamicDisplayNameDeclaringType = this.DynamicDataDisplayNameDeclaringType ?? methodInfo.DeclaringType;

var method = dynamicDisplayNameDeclaringType.GetTypeInfo().GetDeclaredMethod(this.DynamicDataDisplayName);
if (method == null)
{
throw new ArgumentNullException(string.Format("{0} {1}", DynamicDataSourceType.Method, this.DynamicDataDisplayName));
}

var parameters = method.GetParameters();
if (parameters.Length != 2 ||
parameters[0].ParameterType != typeof(MethodInfo) ||
parameters[1].ParameterType != typeof(object[]) ||
method.ReturnType != typeof(string) ||
!method.IsStatic ||
!method.IsPublic)
{
throw new ArgumentNullException(
string.Format(
FrameworkMessages.DynamicDataDisplayName,
this.DynamicDataDisplayName,
Copy link
Member

@jayaranigarg jayaranigarg Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bstoney : Method signature should also have "public" access modifier specified. Can you please add that as well??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will have to update xlf files as well if you modify FrameworkMessages.DynamicDataDisplayName string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Interestingly it did work with a private static method even though the documentation for GetDeclaredMethod states that "Returns an object that represents the specified public method declared by the current type." I have updated the exception message and added a test for public accessibility.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super 👍

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()));
}

return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface ITestDataSource
/// Gets the display name corresponding to test data row for displaying in TestResults.
/// </summary>
/// <param name="methodInfo">
/// The method Info of test method.
/// The method info of test method.
/// </param>
/// <param name="data">
/// The test data which is passed to test method.
Expand Down

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
Expand Up @@ -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">
Copy link
Member

@jayaranigarg jayaranigarg Mar 29, 2018

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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: public static {1} {0}({2}).</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ Trasování zásobníku: {4}</target>
<target state="translated">Hodnota vrácená vlastností nebo metodou {0} by neměla být null.</target>
<note />
</trans-unit>
<trans-unit id="DynamicDataDisplayName">
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ Stapelüberwachung: {4}</target>
<target state="translated">Der von der Eigenschaft oder Methode "{0}" zurückgegebene Wert darf nicht NULL sein.</target>
<note />
</trans-unit>
<trans-unit id="DynamicDataDisplayName">
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ Seguimiento de la pila: {4}</target>
<target state="translated">El valor devuelto por la propiedad o el método {0} no debe ser nulo.</target>
<note />
</trans-unit>
<trans-unit id="DynamicDataDisplayName">
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ Trace de la pile : {4}</target>
<target state="translated">La valeur retournée par la propriété ou la méthode {0} ne doit pas être Null.</target>
<note />
</trans-unit>
<trans-unit id="DynamicDataDisplayName">
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ Analisi dello stack: {4}</target>
<target state="translated">Il valore restituito dalla proprietà o dal metodo {0} non deve essere Null.</target>
<note />
</trans-unit>
<trans-unit id="DynamicDataDisplayName">
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ Stack Trace: {4}</source>
<target state="translated">プロパティやメソッド {0} によって返される値を null にすることはできません。</target>
<note />
</trans-unit>
<trans-unit id="DynamicDataDisplayName">
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ Stack Trace: {4}</source>
<target state="translated">속성 또는 메서드 {0}에 의해 반환된 값은 null일 수 없습니다.</target>
<note />
</trans-unit>
<trans-unit id="DynamicDataDisplayName">
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ Komunikat o wyjątku: {3}
<target state="translated">Wartość zwracana przez właściwość lub metodę {0} nie powinna być równa null.</target>
<note />
</trans-unit>
<trans-unit id="DynamicDataDisplayName">
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ Rastreamento de Pilha: {4}</target>
<target state="translated">O valor retornado pela propriedade ou o método {0} não deve ser nulo null.</target>
<note />
</trans-unit>
<trans-unit id="DynamicDataDisplayName">
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ Stack Trace: {4}</source>
<target state="translated">Значение, возвращаемое свойством или методом {0}, не должно быть равно NULL.</target>
<note />
</trans-unit>
<trans-unit id="DynamicDataDisplayName">
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ Yığın İzleme: {4}</target>
<target state="translated">{0} özelliği veya metodu tarafından döndürülen değer null olamaz.</target>
<note />
</trans-unit>
<trans-unit id="DynamicDataDisplayName">
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ Stack Trace: {4}</target>
<target state="new">Value returned by property or method {0} shouldn't be null.</target>
<note></note>
</trans-unit>
<trans-unit id="DynamicDataDisplayName">
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ Stack Trace: {4}</source>
<target state="translated">属性或方法 {0} 返回的值不能为 null。</target>
<note />
</trans-unit>
<trans-unit id="DynamicDataDisplayName">
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ Stack Trace: {4}</source>
<target state="translated">屬性或方法 {0} 傳回的值不可為 null。</target>
<note />
</trans-unit>
<trans-unit id="DynamicDataDisplayName">
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,20 @@ public void GetDisplayNameShouldReturnAppropriateName()
displayName = dataRowAttribute.GetDisplayName(this.testMethodInfo, data2);
Assert.AreEqual("DataRowTestMethod (First,,Second)", displayName);
}

[TestMethod]
public void GetDisplayNameShouldReturnSpecifiedDisplayName()
{
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);
}
}
}
Loading