Skip to content
Open
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
75 changes: 61 additions & 14 deletions NUnitTfsTestCase/NUnitTfsTestCase/TestCaseData/TestCaseDataTfs.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,80 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NUnitTfsTestCase.TestCaseData
{
public class TestCaseDataTfs
{
private static DataTable GetTestCaseParams(int TestCaseId)
/// <summary>
/// Takes all parameters from specified test case.
/// Same as <see cref="GetTestDataAsArray"/>, but with casting to dynamic.
/// Saved to avoid errors while users updating from one version to another.
/// </summary>
/// <param name="testCaseId">Number of test case where parameters taken.</param>
/// <returns>IEnumerable with array of values casted to dynamic.</returns>
public static IEnumerable<dynamic> GetTestDataInternal(int testCaseId)
{
return GetTestDataAsArray(testCaseId);
}

/// <summary>
/// Takes all parameters from specified test case.
/// Params stored as { ParamValue1, ParamValue2, ParamValue3 }.
/// Same as <see cref="GetTestDataInternal"/>, but without casting to dynamic.
/// </summary>
/// <param name="testCaseId">Number of test case where parameters taken.</param>
/// <returns>IEnumerable with array of values. Each object [] represents single line with parameters.</returns>
public static IEnumerable<object []> GetTestDataAsArray(int testCaseId)
{
return GetTestData(testCaseId, CollectTestDataAsArray);
}

/// <summary>
/// Takes all parameters from specified test case.
/// Params stored as { { ParamName1, ParamValue1 }, { ParamName2, ParamValue2 }, { ParamName3, ParamValue3 } }.
/// </summary>
/// <param name="testCaseId">Number of test case where parameters taken.</param>
/// <returns>IEnumerable with dictionary of names and values. Each Dictionary represents single line with pairs of name and value of parameter.</returns>
public static IEnumerable<Dictionary<string, object>> GetTestDataAsDictionary(int testCaseId)
{
return GetTestData(testCaseId, CollectTestDataAsDictionary);
}

private static DataTable GetTestCaseParams(int testCaseId)
{
int testCaseId = TestCaseId;
var testCase = TfsService.ProjectConnection.TestManagementProject.TestCases.Find(testCaseId);
return testCase.DefaultTableReadOnly;
}

public static IEnumerable<dynamic> GetTestDataInternal(int TestCaseID)
private static IEnumerable<T> GetTestData<T>(int testCaseId, Func<DataRow, DataColumnCollection, T> storageFunc) where T: IEnumerable
{
var tcParams = GetTestCaseParams(testCaseId);
foreach (DataRow dr in tcParams.Rows)
{
yield return storageFunc(dr, tcParams.Columns);
}
}

private static Dictionary<string, object> CollectTestDataAsDictionary(DataRow dr, DataColumnCollection dcc)
{
var dict = new Dictionary<string, object>();
for (int i = 0; i < dcc.Count; i++)
{
dict.Add(dcc[i].ColumnName, dr[i]);
}
return dict;
}

private static object [] CollectTestDataAsArray(DataRow dr, DataColumnCollection dcc)
{
var tb = GetTestCaseParams(TestCaseID);
foreach (DataRow dr in tb.Rows)
var array = new object [dcc.Count];
for (int i = 0; i < dcc.Count; i++)
{
var ob = new List<object>();
for (var i = 0; i < tb.Columns.Count; i++)
{
ob.Add(dr[i]);
}
yield return ob.ToArray();
array[i] = dr[i];
}
return array;
}
}
}