Skip to content

Commit 79f22fb

Browse files
authored
Fix few issues with DataRow implementation (#159)
1 parent fd44943 commit 79f22fb

File tree

4 files changed

+78
-15
lines changed

4 files changed

+78
-15
lines changed

poc/TestOfTestFrameworkByReference/DataRowTests.cs

+17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace nanoFramework.TestFramework.Test
1313
[TestClass]
1414
public class TestOfDataRow
1515
{
16+
[TestMethod]
1617
[DataRow(1, 2, 3)]
1718
[DataRow(5, 6, 11)]
1819
public void TestAddition(int number1, int number2, int result)
@@ -22,10 +23,26 @@ public void TestAddition(int number1, int number2, int result)
2223
Assert.Equal(additionResult, result);
2324
}
2425

26+
[TestMethod]
2527
[DataRow("TestString")]
2628
public void TestString(string testData)
2729
{
2830
Assert.Equal(testData, "TestString");
2931
}
32+
33+
[TestMethod]
34+
[DataRow("adsdasdasasddassaadsdasdasasddassaadsdasdasasddassaadsdasdasasddassaadsdasdasasddassaadsdasdasasddassa")]
35+
public void TestLongString(string testData)
36+
{
37+
Assert.Equal(testData, "adsdasdasasddassaadsdasdasasddassaadsdasdasasddassaadsdasdasasddassaadsdasdasasddassaadsdasdasasddassa");
38+
}
39+
40+
[TestMethod]
41+
[DataRow("Right align in 10 chars: {0,10:N2}: and then more", 1234.5641, "Right align in 10 chars: 1,234.56: and then more")]
42+
public void TestStringWithComma(string formatString, double value, string outcomeMessage)
43+
{
44+
// Test alignment operator which is the "," and a number. Negative is right aligned, positive left aligned
45+
Assert.Equal(string.Format(formatString, value), outcomeMessage);
46+
}
3047
}
3148
}

source/TestAdapter/Discover.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,21 @@ public static List<TestCase> FindTestCases(string source)
132132
foreach (var method in methods)
133133
{
134134
var attribs = method.GetCustomAttributes(true);
135-
136-
foreach (var attrib in attribs)
135+
attribs = Helper.RemoveTestMethodIfDataRowExists(attribs);
136+
for (int i = 0; i < attribs.Length; i++)
137137
{
138+
var attrib = attribs[i];
139+
138140
if (attrib.GetType().FullName == typeof(SetupAttribute).FullName ||
139141
attrib.GetType().FullName == typeof(TestMethodAttribute).FullName ||
140142
attrib.GetType().FullName == typeof(CleanupAttribute).FullName ||
141143
attrib.GetType().FullName == typeof(DataRowAttribute).FullName)
142144
{
143-
var testCase = GetFileNameAndLineNumber(allCsFils, type, method, attrib);
145+
var testCase = GetFileNameAndLineNumber(allCsFils, type, method, attrib, i);
144146
testCase.Source = source;
145147
testCase.ExecutorUri = new Uri(TestsConstants.NanoExecutor);
146148
testCase.FullyQualifiedName = $"{type.FullName}.{testCase.DisplayName}";
147-
testCase.Traits.Add(new Trait("Type", attrib.GetType().Name.Replace("Attribute","")));
149+
testCase.Traits.Add(new Trait("Type", attrib.GetType().Name.Replace("Attribute", "")));
148150
testCases.Add(testCase);
149151
}
150152
}
@@ -222,7 +224,7 @@ private static FileInfo[] FindNfprojSources(string source)
222224
}
223225
}
224226

225-
private static TestCase GetFileNameAndLineNumber(string[] csFiles, Type className, MethodInfo method, object attribute)
227+
private static TestCase GetFileNameAndLineNumber(string[] csFiles, Type className, MethodInfo method, object attribute, int attributeIndex)
226228
{
227229
var clName = className.Name;
228230
var methodName = method.Name;
@@ -243,7 +245,7 @@ private static TestCase GetFileNameAndLineNumber(string[] csFiles, Type classNam
243245
{
244246
flret.CodeFilePath = csFile;
245247
flret.LineNumber = lineNum;
246-
flret.DisplayName = Helper.GetTestDisplayName(method, attribute);
248+
flret.DisplayName = Helper.GetTestDisplayName(method, attribute, attributeIndex);
247249
return flret;
248250
}
249251

source/TestFrameworkShared/Helper.cs

+49-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace nanoFramework.TestFramework
1313
/// </summary>
1414
public static class Helper
1515
{
16+
private delegate bool AnyDelegateType(object source);
17+
1618
private static string GetJoinedParams(object[] data)
1719
{
1820
var returnString = string.Empty;
@@ -28,24 +30,64 @@ private static string GetJoinedParams(object[] data)
2830
return returnString.Substring(0, returnString.Length - 4);
2931
}
3032

33+
private static bool Any(this object[] array, AnyDelegateType predicate)
34+
{
35+
foreach (var item in array)
36+
{
37+
if (predicate(item))
38+
return true;
39+
}
40+
41+
return false;
42+
}
43+
3144
/// <summary>
32-
/// Generates test display name based on passed <paramref name="method"/> and <paramref name="attribute"/>.
45+
/// Generates test display name based on passed <paramref name="method"/>, <paramref name="attribute"/> and <paramref name="attributeIndex"/>.
3346
/// </summary>
34-
/// <returns>Returns method name with parameters if passed attribute is of DataRow type</returns>
35-
public static string GetTestDisplayName(MethodInfo method, object attribute)
47+
/// <returns>Returns method name with attributeIndex if passed attribute is of DataRow type</returns>
48+
public static string GetTestDisplayName(MethodInfo method, object attribute, int attributeIndex)
3649
{
3750
// Comparing via full name, because attribute parameter is from "TestFramework.dll"
3851
// and current type TestCaseAttribute is in scope of "TestAdapter" due to shared project
3952
// The same reason - reflection to get value
4053
if (attribute.GetType().FullName == typeof(DataRowAttribute).FullName)
4154
{
42-
var methodParameters = (object[])attribute.GetType()
43-
.GetMethod($"get_{nameof(DataRowAttribute.MethodParameters)}").Invoke(attribute, null);
44-
45-
return $"{method.Name} - (params: {GetJoinedParams(methodParameters)})";
55+
return $"{method.Name} (index {attributeIndex})";
4656
}
4757

4858
return method.Name;
4959
}
60+
61+
/// <summary>
62+
/// Removes "TestMethod" attribute from array if "DataRow" attribute exists in the same array
63+
/// </summary>
64+
/// <param name="attribs">Array of attributes to check</param>
65+
/// <returns>New array without TestMethod if DataRow exists, if not the same array</returns>
66+
public static object[] RemoveTestMethodIfDataRowExists(object[] attribs)
67+
{
68+
//If method attribute contains TestMethod and DataRow - add only DataRow
69+
if (attribs.Any(x => x.GetType().FullName == typeof(TestMethodAttribute).FullName) &&
70+
attribs.Any(x => x.GetType().FullName == typeof(DataRowAttribute).FullName))
71+
{
72+
var newAttribs = new object[attribs.Length - 1];
73+
74+
var newAttribsIndex = 0;
75+
for (int i = 0; i < attribs.Length; i++)
76+
{
77+
var attrib = attribs[i];
78+
if (attrib.GetType().FullName == typeof(TestMethodAttribute).FullName)
79+
{
80+
continue;
81+
}
82+
83+
newAttribs[newAttribsIndex] = attrib;
84+
newAttribsIndex++;
85+
}
86+
87+
return newAttribs;
88+
}
89+
90+
return attribs;
91+
}
5092
}
5193
}

source/UnitTestLauncher/Program.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ private static bool RunTest(
6565
foreach (var method in methods)
6666
{
6767
var attribs = method.GetCustomAttributes(true);
68+
attribs = Helper.RemoveTestMethodIfDataRowExists(attribs);
6869

69-
foreach (var attrib in attribs)
70+
for (int i = 0; i < attribs.Length; i++)
7071
{
71-
var methodName = Helper.GetTestDisplayName(method, attrib);
72+
var attrib = attribs[i];
73+
var methodName = Helper.GetTestDisplayName(method, attrib, i);
7274
if (attribToRun == attrib.GetType())
7375
{
7476
try

0 commit comments

Comments
 (0)