Esempio n. 1
0
        private XElement GetElement(TranquireXmlReportItem item)
        {
            var content = new List <object>()
            {
                new XAttribute("start-date", item.StartDate.ToString(CultureInfo.InvariantCulture)),
                new XAttribute("end-date", item.EndDate.ToString(CultureInfo.InvariantCulture)),
                new XAttribute("duration", (int)item.Duration.TotalMilliseconds),
                new XAttribute("name", item.Name),
                new XAttribute("has-error", item.HasError),
                item.Children.Select(GetElement)
            };

            if (item.Attachments.Count > 0)
            {
                content.Add(new XElement("attachments", item.Attachments.Select(getAttachment)));
            }
            if (item is TranquireXmlReportThen then)
            {
                content.Add(new XAttribute("outcome", then.Outcome.ToString().ToLower()));
                if (then.Outcome != ThenOutcome.Pass)
                {
                    content.Add(new XElement("outcomeDetail", new XCData(then.Error.Message)));
                }
            }
            else if (item.HasError && !hasExceptionInChildren(item.Error, item.Children))
            {
                content.Add(new XElement("error", new XCData(item.Error.ToString())));
            }
            var element = new XElement(
                elementName(),
                content.ToArray()
                );

            return(element);

            string elementName()
            {
                switch (item)
                {
                case TranquireXmlReportQuestion _:
                    return("question");

                case TranquireXmlReportAction _:
                    return("action");

                case TranquireXmlReportThen _:
                    return("then");

                case TranquireXmlReportGiven _:
                    return("given");

                case TranquireXmlReportWhen _:
                    return("when");

                default:
                    return("root");
                }
            }

            XElement getAttachment(ActionFileAttachment attachment)
            {
                return(new XElement(
                           "attachment",
                           new XAttribute("filepath", attachment.FilePath),
                           new XAttribute("description", attachment.Description)
                           ));
            }

            bool hasExceptionInChildren(Exception ex, IEnumerable <TranquireXmlReportItem> items)
            {
                return(items.Any(i => i.Error == ex || hasExceptionInChildren(ex, i.Children)));
            }
        }
 public void Add(TranquireXmlReportItem item) => _children = Children.Add(item);