/// <summary> /// Creates and returns an assignment for a parameter in the coverage group /// based on a specific combination that has not yet been covered in a test case. /// </summary> private bool AssignCoveredParameter( IDictionary <string, ParameterAssignment> parameterAssignments, TestSpecification testSpecification, CoverageGroup coverageGroup, IList <string> uncoveredCombination, int groupIndex) { string selectedParameter = coverageGroup.Parameters[groupIndex]; string selectedEquivalenceClass = uncoveredCombination[groupIndex]; if ((selectedParameter == null) || (selectedEquivalenceClass == null)) { return(false); } InputParameter inputParameter = testSpecification.InputParameters[selectedParameter]; if (inputParameter == null) { return(false); } ParameterAssignment parameterAssignment = new ParameterAssignment(); parameterAssignment.Set( inputParameter.Given, inputParameter.Name, inputParameter.Text, inputParameter.EquivalenceClasses[selectedEquivalenceClass]); parameterAssignments.Add(parameterAssignment.Name, parameterAssignment); return(true); }
/// <summary> /// Ensures that every parameter is referenced at least once in a coverage group. If /// not, it adds coverage groups for those that are unused to the specification. /// Returns true if successful. /// </summary> private bool NormalizeCoverage() { IDictionary <string, bool> parametersCovered = new Dictionary <string, bool>(); foreach (string parameter in InputParameters.Keys) { parametersCovered.Add(parameter, false); } if (CoverageGroups == null) { CoverageGroups = new List <CoverageGroup>(); } foreach (CoverageGroup coverageGroup in CoverageGroups) { foreach (string parameter in coverageGroup.Parameters) { parametersCovered[parameter] = true; } } foreach (string parameter in parametersCovered.Keys.Where(x => !parametersCovered[x])) { CoverageGroup coverageGroup = new CoverageGroup(); coverageGroup.Set($"Singleton-{parameter}", new List <string> { parameter }); CoverageGroups.Add(coverageGroup); } return(true); }
/// <summary> /// Generates a single test case that selects an "uncovered" combination from the coverage group. /// Returns the test case if successful, otherwise null. /// </summary> private TestCase PopulateTestCase(TestSpecification testSpecification, CoverageGroup coverageGroup) { IList <string> uncoveredCombination = CoverageMetricsInSuite.FirstUncovered(coverageGroup.Name) .Split(new char[] { '.' }, StringSplitOptions.None); IDictionary <string, ParameterAssignment> parameterAssignments = new Dictionary <string, ParameterAssignment>(); for (int groupIndex = 0; groupIndex < coverageGroup.Parameters.Count; groupIndex++) { if (!AssignCoveredParameter(parameterAssignments, testSpecification, coverageGroup, uncoveredCombination, groupIndex)) { return(null); } } foreach (InputParameter inputParameter in testSpecification.InputParameters.Values .Where(x => !coverageGroup.Parameters.Contains(x.Name))) { if (!AssignUncoveredParameter(parameterAssignments, inputParameter)) { return(null); } } bool valuation = true; foreach (ParameterAssignment parameterAssignment in parameterAssignments.Values) { valuation = valuation && AssociatedParameterValuation(parameterAssignments, testSpecification.InputParameters[parameterAssignment.Name]) && AssociatedEquivalenceClassValuation(parameterAssignments, parameterAssignment.SelectedEquivalenceClass); } IList <ExpectedResult> expectedResults = new List <ExpectedResult>(); foreach (ExpectedResult expectedResult in testSpecification.ExpectedResults.Values .Where(x => x.ConditionExpression.Match(parameterAssignments))) { expectedResults.Add(expectedResult); } valuation = (expectedResults.Count == 0) ? false : valuation; _testCaseNumber++; string given = testSpecification.Given; string name = $"{testSpecification.Name}-{_testCaseNumber}"; string text = $"Test {_testCaseNumber}"; TestCase testCase = new TestCase(); testCase.Set(given, name, text, parameterAssignments, expectedResults, valuation); return(testCase); }
/// <summary> /// Reads the XML contents of the file into the test specification and returns true if successful. /// </summary> public bool ReadAsXml(string filenameWithPath) { string contentsAsString = FileHelper.ContentsAsString(filenameWithPath); XElement xTestSpecification = XmlHelper.Root(contentsAsString, "TestSpecification"); FieldsAsAttributes = XmlHelper.ElementOf(xTestSpecification, "FieldsAsAttributes") == "true"; string given = XmlField(xTestSpecification, "Given"); string name = XmlField(xTestSpecification, "Name"); string text = XmlField(xTestSpecification, "Text"); IList <CoverageGroup> coverageGroups = new List <CoverageGroup>(); foreach (XElement xCoverageGroup in XmlHelper.Children(xTestSpecification, "CoverageGroup")) { CoverageGroup coverageGroup = new CoverageGroup(); if (coverageGroup.ReadAsXml(xCoverageGroup)) { coverageGroups.Add(coverageGroup); } } IDictionary <string, InputParameter> inputParameters = new Dictionary <string, InputParameter>(); foreach (XElement xInputParameter in XmlHelper.Children(xTestSpecification, "InputParameter")) { InputParameter inputParameter = new InputParameter(); if (inputParameter.ReadAsXml(xInputParameter) && !inputParameters.ContainsKey(inputParameter.Name)) { inputParameters.Add(inputParameter.Name, inputParameter); } } IDictionary <string, ExpectedResult> expectedResults = new Dictionary <string, ExpectedResult>(); foreach (XElement xExpectedResult in XmlHelper.Children(xTestSpecification, "ExpectedResult")) { ExpectedResult expectedResult = new ExpectedResult(); if (expectedResult.ReadAsXml(xExpectedResult) && !expectedResults.ContainsKey(expectedResult.Name)) { expectedResults.Add(expectedResult.Name, expectedResult); } } bool compositeRead = Set(given, name, text, coverageGroups, inputParameters, expectedResults); compositeRead = compositeRead && Normalize(); compositeRead = compositeRead && UpdateDependencies(); return(compositeRead); }