Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions poc/TestOfTestFrameworkByReference/DataRowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace nanoFramework.TestFramework.Test
[TestClass]
public class TestOfDataRow
{
[TestMethod]
[DataRow(1, 2, 3)]
[DataRow(5, 6, 11)]
public void TestAddition(int number1, int number2, int result)
Expand All @@ -22,10 +23,26 @@ public void TestAddition(int number1, int number2, int result)
Assert.Equal(additionResult, result);
}

[TestMethod]
[DataRow("TestString")]
public void TestString(string testData)
{
Assert.Equal(testData, "TestString");
}

[TestMethod]
[DataRow("adsdasdasasddassaadsdasdasasddassaadsdasdasasddassaadsdasdasasddassaadsdasdasasddassaadsdasdasasddassa")]
public void TestLongString(string testData)
{
Assert.Equal(testData, "adsdasdasasddassaadsdasdasasddassaadsdasdasasddassaadsdasdasasddassaadsdasdasasddassaadsdasdasasddassa");
}

[TestMethod]
[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")]
public void TestStringWithComma(string formatString, double value, string outcomeMessage)
{
// Test alignment operator which is the "," and a number. Negative is right aligned, positive left aligned
Assert.Equal(string.Format(formatString, value), outcomeMessage);
}
}
}
14 changes: 8 additions & 6 deletions source/TestAdapter/Discover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,21 @@ public static List<TestCase> FindTestCases(string source)
foreach (var method in methods)
{
var attribs = method.GetCustomAttributes(true);

foreach (var attrib in attribs)
attribs = Helper.RemoveTestMethodIfDataRowExists(attribs);
for (int i = 0; i < attribs.Length; i++)
{
var attrib = attribs[i];

if (attrib.GetType().FullName == typeof(SetupAttribute).FullName ||
attrib.GetType().FullName == typeof(TestMethodAttribute).FullName ||
attrib.GetType().FullName == typeof(CleanupAttribute).FullName ||
attrib.GetType().FullName == typeof(DataRowAttribute).FullName)
{
var testCase = GetFileNameAndLineNumber(allCsFils, type, method, attrib);
var testCase = GetFileNameAndLineNumber(allCsFils, type, method, attrib, i);
testCase.Source = source;
testCase.ExecutorUri = new Uri(TestsConstants.NanoExecutor);
testCase.FullyQualifiedName = $"{type.FullName}.{testCase.DisplayName}";
testCase.Traits.Add(new Trait("Type", attrib.GetType().Name.Replace("Attribute","")));
testCase.Traits.Add(new Trait("Type", attrib.GetType().Name.Replace("Attribute", "")));
testCases.Add(testCase);
}
}
Expand Down Expand Up @@ -222,7 +224,7 @@ private static FileInfo[] FindNfprojSources(string source)
}
}

private static TestCase GetFileNameAndLineNumber(string[] csFiles, Type className, MethodInfo method, object attribute)
private static TestCase GetFileNameAndLineNumber(string[] csFiles, Type className, MethodInfo method, object attribute, int attributeIndex)
{
var clName = className.Name;
var methodName = method.Name;
Expand All @@ -243,7 +245,7 @@ private static TestCase GetFileNameAndLineNumber(string[] csFiles, Type classNam
{
flret.CodeFilePath = csFile;
flret.LineNumber = lineNum;
flret.DisplayName = Helper.GetTestDisplayName(method, attribute);
flret.DisplayName = Helper.GetTestDisplayName(method, attribute, attributeIndex);
return flret;
}

Expand Down
56 changes: 49 additions & 7 deletions source/TestFrameworkShared/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace nanoFramework.TestFramework
/// </summary>
public static class Helper
{
private delegate bool AnyDelegateType(object source);

private static string GetJoinedParams(object[] data)
{
var returnString = string.Empty;
Expand All @@ -28,24 +30,64 @@ private static string GetJoinedParams(object[] data)
return returnString.Substring(0, returnString.Length - 4);
}

private static bool Any(this object[] array, AnyDelegateType predicate)
{
foreach (var item in array)
{
if (predicate(item))
return true;
}

return false;
}

/// <summary>
/// Generates test display name based on passed <paramref name="method"/> and <paramref name="attribute"/>.
/// Generates test display name based on passed <paramref name="method"/>, <paramref name="attribute"/> and <paramref name="attributeIndex"/>.
/// </summary>
/// <returns>Returns method name with parameters if passed attribute is of DataRow type</returns>
public static string GetTestDisplayName(MethodInfo method, object attribute)
/// <returns>Returns method name with attributeIndex if passed attribute is of DataRow type</returns>
public static string GetTestDisplayName(MethodInfo method, object attribute, int attributeIndex)
{
// Comparing via full name, because attribute parameter is from "TestFramework.dll"
// and current type TestCaseAttribute is in scope of "TestAdapter" due to shared project
// The same reason - reflection to get value
if (attribute.GetType().FullName == typeof(DataRowAttribute).FullName)
{
var methodParameters = (object[])attribute.GetType()
.GetMethod($"get_{nameof(DataRowAttribute.MethodParameters)}").Invoke(attribute, null);

return $"{method.Name} - (params: {GetJoinedParams(methodParameters)})";
return $"{method.Name} (index {attributeIndex})";
}

return method.Name;
}

/// <summary>
/// Removes "TestMethod" attribute from array if "DataRow" attribute exists in the same array
/// </summary>
/// <param name="attribs">Array of attributes to check</param>
/// <returns>New array without TestMethod if DataRow exists, if not the same array</returns>
public static object[] RemoveTestMethodIfDataRowExists(object[] attribs)
{
//If method attribute contains TestMethod and DataRow - add only DataRow
if (attribs.Any(x => x.GetType().FullName == typeof(TestMethodAttribute).FullName) &&
attribs.Any(x => x.GetType().FullName == typeof(DataRowAttribute).FullName))
{
var newAttribs = new object[attribs.Length - 1];

var newAttribsIndex = 0;
for (int i = 0; i < attribs.Length; i++)
{
var attrib = attribs[i];
if (attrib.GetType().FullName == typeof(TestMethodAttribute).FullName)
{
continue;
}

newAttribs[newAttribsIndex] = attrib;
newAttribsIndex++;
}

return newAttribs;
}

return attribs;
}
}
}
6 changes: 4 additions & 2 deletions source/UnitTestLauncher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ private static bool RunTest(
foreach (var method in methods)
{
var attribs = method.GetCustomAttributes(true);
attribs = Helper.RemoveTestMethodIfDataRowExists(attribs);

foreach (var attrib in attribs)
for (int i = 0; i < attribs.Length; i++)
{
var methodName = Helper.GetTestDisplayName(method, attrib);
var attrib = attribs[i];
var methodName = Helper.GetTestDisplayName(method, attrib, i);
if (attribToRun == attrib.GetType())
{
try
Expand Down