Skip to content

Commit bad3e20

Browse files
bstoneyjayaranigarg
authored andcommitted
Dynamic data display name (#373)
* Corrected documentation * Added dynamic display name to DynamicDataAttribute * Changes requested from PR * Updated the exception message and added a test for public accessibility
1 parent cc25e88 commit bad3e20

20 files changed

+406
-6
lines changed

src/TestFramework/MSTest.Core/Attributes/DynamicDataAttribute.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public DynamicDataAttribute(string dynamicDataSourceName, DynamicDataSourceType
5656
/// Initializes a new instance of the <see cref="DynamicDataAttribute"/> class.
5757
/// </summary>
5858
/// <param name="dynamicDataSourceName">
59-
/// The type in which the test data is declared (as property or in method).
59+
/// The name of method or property having test data.
6060
/// </param>
6161
/// <param name="dynamicDataDeclaringType">
6262
/// The declaring type of property or method having data.
@@ -70,6 +70,16 @@ public DynamicDataAttribute(string dynamicDataSourceName, Type dynamicDataDeclar
7070
this.dynamicDataDeclaringType = dynamicDataDeclaringType;
7171
}
7272

73+
/// <summary>
74+
/// Gets or sets the name of method used to customize the display name in test results.
75+
/// </summary>
76+
public string DynamicDataDisplayName { get; set; }
77+
78+
/// <summary>
79+
/// Gets or sets the declaring type used to customize the display name in test results.
80+
/// </summary>
81+
public Type DynamicDataDisplayNameDeclaringType { get; set; }
82+
7383
/// <inheritdoc />
7484
public IEnumerable<object[]> GetData(MethodInfo methodInfo)
7585
{
@@ -131,12 +141,40 @@ public IEnumerable<object[]> GetData(MethodInfo methodInfo)
131141
/// <inheritdoc />
132142
public string GetDisplayName(MethodInfo methodInfo, object[] data)
133143
{
134-
if (data != null)
144+
if (this.DynamicDataDisplayName != null)
145+
{
146+
var dynamicDisplayNameDeclaringType = this.DynamicDataDisplayNameDeclaringType ?? methodInfo.DeclaringType;
147+
148+
var method = dynamicDisplayNameDeclaringType.GetTypeInfo().GetDeclaredMethod(this.DynamicDataDisplayName);
149+
if (method == null)
150+
{
151+
throw new ArgumentNullException(string.Format("{0} {1}", DynamicDataSourceType.Method, this.DynamicDataDisplayName));
152+
}
153+
154+
var parameters = method.GetParameters();
155+
if (parameters.Length != 2 ||
156+
parameters[0].ParameterType != typeof(MethodInfo) ||
157+
parameters[1].ParameterType != typeof(object[]) ||
158+
method.ReturnType != typeof(string) ||
159+
!method.IsStatic ||
160+
!method.IsPublic)
161+
{
162+
throw new ArgumentNullException(
163+
string.Format(
164+
FrameworkMessages.DynamicDataDisplayName,
165+
this.DynamicDataDisplayName,
166+
typeof(string).Name,
167+
string.Join(", ", typeof(MethodInfo).Name, typeof(object[]).Name)));
168+
}
169+
170+
return method.Invoke(null, new object[] { methodInfo, data }) as string;
171+
}
172+
else if (data != null)
135173
{
136174
return string.Format(CultureInfo.CurrentCulture, FrameworkMessages.DataDrivenResultDisplayName, methodInfo.Name, string.Join(",", data.AsEnumerable()));
137175
}
138176

139177
return null;
140178
}
141179
}
142-
}
180+
}

src/TestFramework/MSTest.Core/Interfaces/ITestDataSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public interface ITestDataSource
2626
/// Gets the display name corresponding to test data row for displaying in TestResults.
2727
/// </summary>
2828
/// <param name="methodInfo">
29-
/// The method Info of test method.
29+
/// The method info of test method.
3030
/// </param>
3131
/// <param name="data">
3232
/// The test data which is passed to test method.

src/TestFramework/MSTest.Core/Resources/FrameworkMessages.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/TestFramework/MSTest.Core/Resources/FrameworkMessages.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,7 @@ Stack Trace: {4}</value>
283283
<data name="DynamicDataValueNull" xml:space="preserve">
284284
<value>Value returned by property or method {0} shouldn't be null.</value>
285285
</data>
286+
<data name="DynamicDataDisplayName" xml:space="preserve">
287+
<value>Method {0} must match the expected signature: public static {1} {0}({2}).</value>
288+
</data>
286289
</root>

src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.cs.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ Trasování zásobníku: {4}</target>
282282
<target state="translated">Hodnota vrácená vlastností nebo metodou {0} by neměla být null.</target>
283283
<note />
284284
</trans-unit>
285+
<trans-unit id="DynamicDataDisplayName">
286+
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
287+
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
288+
<note></note>
289+
</trans-unit>
285290
</body>
286291
</file>
287292
</xliff>

src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.de.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ Stapelüberwachung: {4}</target>
282282
<target state="translated">Der von der Eigenschaft oder Methode "{0}" zurückgegebene Wert darf nicht NULL sein.</target>
283283
<note />
284284
</trans-unit>
285+
<trans-unit id="DynamicDataDisplayName">
286+
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
287+
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
288+
<note></note>
289+
</trans-unit>
285290
</body>
286291
</file>
287292
</xliff>

src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.es.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ Seguimiento de la pila: {4}</target>
282282
<target state="translated">El valor devuelto por la propiedad o el método {0} no debe ser nulo.</target>
283283
<note />
284284
</trans-unit>
285+
<trans-unit id="DynamicDataDisplayName">
286+
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
287+
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
288+
<note></note>
289+
</trans-unit>
285290
</body>
286291
</file>
287292
</xliff>

src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.fr.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ Trace de la pile : {4}</target>
282282
<target state="translated">La valeur retournée par la propriété ou la méthode {0} ne doit pas être Null.</target>
283283
<note />
284284
</trans-unit>
285+
<trans-unit id="DynamicDataDisplayName">
286+
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
287+
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
288+
<note></note>
289+
</trans-unit>
285290
</body>
286291
</file>
287292
</xliff>

src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.it.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ Analisi dello stack: {4}</target>
282282
<target state="translated">Il valore restituito dalla proprietà o dal metodo {0} non deve essere Null.</target>
283283
<note />
284284
</trans-unit>
285+
<trans-unit id="DynamicDataDisplayName">
286+
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
287+
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
288+
<note></note>
289+
</trans-unit>
285290
</body>
286291
</file>
287292
</xliff>

src/TestFramework/MSTest.Core/Resources/xlf/FrameworkMessages.ja.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ Stack Trace: {4}</source>
282282
<target state="translated">プロパティやメソッド {0} によって返される値を null にすることはできません。</target>
283283
<note />
284284
</trans-unit>
285+
<trans-unit id="DynamicDataDisplayName">
286+
<source>Method {0} must match the expected signature: public static {1} {0}({2}).</source>
287+
<target state="new">Method {0} must match the expected signature: {1} {0}({2}).</target>
288+
<note></note>
289+
</trans-unit>
285290
</body>
286291
</file>
287292
</xliff>

0 commit comments

Comments
 (0)