-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathConsoleTestResult.cs
151 lines (125 loc) · 5.21 KB
/
ConsoleTestResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using NUnit.TextDisplay;
namespace NUnit.ConsoleRunner
{
/// <summary>
/// ConsoleTestResult represents the result of one test being
/// displayed in the console report.
/// </summary>
public class ConsoleTestResult
{
private static readonly char[] EOL_CHARS = new char[] { '\r', '\n' };
private readonly XmlNode _resultNode;
/// <summary>
/// ConsoleTestResult represents the result of a test in the console runner.
/// </summary>
/// <param name="resultNode">An XmlNode representing the result</param>
/// <param name="reportIndex">Integer index used in the report listing</param>
public ConsoleTestResult(XmlNode resultNode, int reportIndex)
{
_resultNode = resultNode;
ReportID = reportIndex.ToString();
Result = resultNode.GetAttribute("result");
Label = resultNode.GetAttribute("label");
Site = resultNode.GetAttribute("site");
Status = Label ?? Result ?? "Unkown";
if (Status == "Failed" || Status == "Error")
if (Site == "SetUp" || Site == "TearDown")
Status = Site + " " + Status;
FullName = resultNode.GetAttribute("fullname") ?? "Unknown";
}
public string? Result { get; private set; }
public string? Label { get; private set; }
public string? Site { get; private set; }
public string FullName { get; private set; }
public string ReportID { get; private set; }
public string Status { get; private set; }
public string? Message
{
get
{
return GetTrimmedInnerText(_resultNode.SelectSingleNode("failure/message")) ??
GetTrimmedInnerText(_resultNode.SelectSingleNode("reason/message"));
}
}
public string? StackTrace
{
get { return GetTrimmedInnerText(_resultNode.SelectSingleNode("failure/stack-trace")); }
}
private List<AssertionResult>? _assertions;
public List<AssertionResult> Assertions
{
get
{
if (_assertions == null)
{
_assertions = new List<AssertionResult>();
XmlNodeList? assertions = _resultNode.SelectNodes("assertions/assertion");
if (assertions is not null)
foreach (XmlNode assertion in assertions)
Assertions.Add(new ConsoleTestResult.AssertionResult(assertion));
}
return _assertions;
}
}
public void WriteResult(ExtendedTextWriter writer)
{
int numAsserts = Assertions.Count;
if (numAsserts > 0)
{
int assertionCounter = 0;
string assertID = ReportID;
foreach (var assertion in Assertions)
{
if (numAsserts > 1)
assertID = string.Format("{0}-{1}", ReportID, ++assertionCounter);
WriteResult(writer, assertID, Status, FullName, assertion.Message, assertion.StackTrace);
}
}
else
WriteResult(writer, ReportID, Status, FullName, Message, StackTrace);
}
private void WriteResult(ExtendedTextWriter writer, string reportID, string status, string fullName, string? message, string? stackTrace)
{
ColorStyle style = GetColorStyle();
writer.WriteLine(style,
string.Format("{0}) {1} : {2}", reportID, status, fullName));
if (!string.IsNullOrEmpty(message))
writer.WriteLine(ColorStyle.Output, message);
if (!string.IsNullOrEmpty(stackTrace))
writer.WriteLine(ColorStyle.Output, stackTrace);
writer.WriteLine(); // Skip after each item
}
private ColorStyle GetColorStyle()
{
return Result == "Failed"
? ColorStyle.Failure
: Result == "Warning" || Status == "Ignored"
? ColorStyle.Warning
: ColorStyle.Output;
}
private static string? GetTrimmedInnerText(XmlNode? node)
{
// In order to control the format, we trim any line-end chars
// from end of the strings we write and supply them via calls
// to WriteLine(). Newlines within the strings are retained.
return node != null
? node.InnerText.TrimEnd(EOL_CHARS)
: null;
}
public struct AssertionResult
{
public AssertionResult(XmlNode assertion)
{
Message = GetTrimmedInnerText(assertion.SelectSingleNode("message"));
StackTrace = GetTrimmedInnerText(assertion.SelectSingleNode("stack-trace"));
}
public string? Message { get; private set; }
public string? StackTrace { get; private set; }
}
}
}