Skip to content

Commit

Permalink
Dynamic data display name (#373)
Browse files Browse the repository at this point in the history
* Corrected documentation

* Added dynamic display name to DynamicDataAttribute

* Changes requested from PR

* Updated the exception message and added a test for public accessibility
  • Loading branch information
bstoney authored and jayaranigarg committed Apr 12, 2018
1 parent cc25e88 commit bad3e20
Show file tree
Hide file tree
Showing 20 changed files with 406 additions and 6 deletions.
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,
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">
<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

0 comments on commit bad3e20

Please sign in to comment.