static Info ParseDescendantsRecursive(XElement rootSuite, Info info) { bool foundSuites = false; string rootSuiteName = NunitHelper.GetRootSuiteName(rootSuite); bool rootSuccess = true; var succAttribute = rootSuite.Attribute("success"); if (succAttribute != null) { rootSuccess = succAttribute.Value.ToLowerInvariant() == "true"; } double rootTime = 0d; var timeAttribute = rootSuite.Attribute("time"); if (timeAttribute != null) rootTime = double.Parse(timeAttribute.Value, NumberFormatInfo.InvariantInfo); int rootAsserts = 0; var assertsAttribute = rootSuite.Attribute("asserts"); if (assertsAttribute != null) rootAsserts = int.Parse(assertsAttribute.Value); var resultsElement = rootSuite.Element("results"); if (resultsElement == null) { resultsElement = new XElement("results"); rootSuite.Add(resultsElement); } string suteResult = "default"; var resultAttibute = rootSuite.Attribute("result"); if (resultAttibute != null) suteResult = resultAttibute.Value.ToLowerInvariant(); if (suteResult == "inconclusive" && !rootSuccess) { succAttribute.SetValue("true"); rootSuccess = true; } if (suteResult == "ignored" && !rootSuccess) { string executed = "true"; var executedAttibute = rootSuite.Attribute("executed"); if (executedAttibute != null) executed = executedAttibute.Value.ToLowerInvariant(); if (executed == "false") rootSuccess = true; } if (info == null) { info = new Info(rootSuiteName, rootSuccess, rootTime, rootAsserts) { Result = suteResult }; } else { if (info.SuiteName == rootSuiteName) { if (rootSuccess != info.Success) info.Success = false; info.Time += rootTime; info.Asserts += rootAsserts; } else //return null; // throw new Exception("!!!"); } // parse descendants foreach (var tmp in resultsElement.Elements("test-suite")) { string suiteName = tmp.Attribute("name").Value; Info result; if (info.DescendantDict.ContainsKey(suiteName)) result = ParseDescendantsRecursive(tmp, info.DescendantDict[suiteName]); else result = ParseDescendantsRecursive(tmp, null); if (result != null) { if (info.DescendantDict.ContainsKey(suiteName)) { info.DescendantDict[suiteName] = result; } else { info.DescendantDict.Add(suiteName, result); } } else { throw new Exception("invalid situation in ParseDescendantsRecursive"); } foundSuites = true; } if (!foundSuites) { System.Xml.XmlReader reader = resultsElement.CreateReader(); reader.MoveToContent(); string innerXml = reader.ReadInnerXml(); /* ReadInnerXml */ if (string.IsNullOrEmpty(info.InnerTestCases)) info.InnerTestCases = "<results>"; info.InnerTestCases += innerXml; //foreach(XElement main in rootSuite.Descendants("test-case")) { // info.InnerTestCases.Add(main.ToString()); //} } return info; }
static XElement ParseInfoRecursive(Info inf) { XElement testSuite = new XElement("test-suite", new XAttribute("name", inf.SuiteName), new XAttribute("success", inf.Success), new XAttribute("time", inf.Time.ToString("#####0.000", NumberFormatInfo.InvariantInfo)), new XAttribute("asserts", inf.Asserts.ToString()), new XAttribute("result", inf.Result.ToString())); //mainElement.Add("results"); //XElement el = mainElement.Element("results"); if (inf.DescendantDict.Count == 0) { if (string.IsNullOrEmpty(inf.InnerTestCases)) throw new Exception("inf.innerTestCases.Count = 0 && inf.descendantDict.Count == 0"); else { //mainElement.CreateWriter() ? XElement results = XElement.Parse(inf.InnerTestCases + "</results>"); testSuite.Add(results); } //XNode xfrag = XDocument.ReadFrom(); //XmlDocument document = new XmlDocument(); //document.Load("contosoBooks.xml"); //XPathNavigator navigator = document.CreateNavigator(); //xfrag. // xfrag.InnerXml = @"<Demographic><Age/><DOB/></Demographic>"; // xdoc.DocumentElement.FirstChild.AppendChild(xfrag); } else { XElement mainElement = new XElement("results"); foreach (var item in inf.DescendantDict.Keys) { mainElement.Add(ParseInfoRecursive(inf.DescendantDict[item])); } testSuite.Add(mainElement); } return testSuite; }