diff --git a/NUnitTfsTestCase/NUnitTfsTestCase/TestCaseData/TestCaseDataTfs.cs b/NUnitTfsTestCase/NUnitTfsTestCase/TestCaseData/TestCaseDataTfs.cs index cfb821b..5edde50 100644 --- a/NUnitTfsTestCase/NUnitTfsTestCase/TestCaseData/TestCaseDataTfs.cs +++ b/NUnitTfsTestCase/NUnitTfsTestCase/TestCaseData/TestCaseDataTfs.cs @@ -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) + /// + /// Takes all parameters from specified test case. + /// Same as , but with casting to dynamic. + /// Saved to avoid errors while users updating from one version to another. + /// + /// Number of test case where parameters taken. + /// IEnumerable with array of values casted to dynamic. + public static IEnumerable GetTestDataInternal(int testCaseId) + { + return GetTestDataAsArray(testCaseId); + } + + /// + /// Takes all parameters from specified test case. + /// Params stored as { ParamValue1, ParamValue2, ParamValue3 }. + /// Same as , but without casting to dynamic. + /// + /// Number of test case where parameters taken. + /// IEnumerable with array of values. Each object [] represents single line with parameters. + public static IEnumerable GetTestDataAsArray(int testCaseId) + { + return GetTestData(testCaseId, CollectTestDataAsArray); + } + + /// + /// Takes all parameters from specified test case. + /// Params stored as { { ParamName1, ParamValue1 }, { ParamName2, ParamValue2 }, { ParamName3, ParamValue3 } }. + /// + /// Number of test case where parameters taken. + /// IEnumerable with dictionary of names and values. Each Dictionary represents single line with pairs of name and value of parameter. + public static IEnumerable> 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 GetTestDataInternal(int TestCaseID) + private static IEnumerable GetTestData(int testCaseId, Func storageFunc) where T: IEnumerable + { + var tcParams = GetTestCaseParams(testCaseId); + foreach (DataRow dr in tcParams.Rows) + { + yield return storageFunc(dr, tcParams.Columns); + } + } + + private static Dictionary CollectTestDataAsDictionary(DataRow dr, DataColumnCollection dcc) + { + var dict = new Dictionary(); + 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(); - for (var i = 0; i < tb.Columns.Count; i++) - { - ob.Add(dr[i]); - } - yield return ob.ToArray(); + array[i] = dr[i]; } + return array; } } }