/
NUnitTestEvent.cs
242 lines (203 loc) · 7.43 KB
/
NUnitTestEvent.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// ***********************************************************************
// Copyright (c) 2020-2021 Charlie Poole, Terje Sandstrom
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.Serialization;
using System.Xml;
namespace NUnit.VisualStudio.TestAdapter.NUnitEngine
{
public interface INUnitTestEventForXml
{
XmlNode Node { get; }
}
public interface INUnitTestEvent : INUnitTestNode
{
string Output { get; }
TimeSpan Duration { get; }
IEnumerable<NUnitAttachment> NUnitAttachments { get; }
NUnitTestEvent.CheckedTime StartTime();
NUnitTestEvent.CheckedTime EndTime();
bool IsIgnored { get; }
NUnitTestEvent.ResultType Result();
bool IsFailed { get; }
NUnitTestEvent.SiteType Site();
}
public abstract class NUnitTestEvent : NUnitTestNode, INUnitTestEvent
{
public enum ResultType
{
Failed,
Success,
Skipped,
Warning,
NoIdea
}
public enum SiteType
{
NoIdea,
Test,
Setup,
TearDown
}
public enum TestTypes
{
NoIdea,
TestFixture,
TestMethod
}
public TestTypes TestType()
{
string type = Node.GetAttribute("type");
return type switch
{
"TestFixture" => TestTypes.TestFixture,
"TestMethod" => TestTypes.TestMethod,
_ => TestTypes.NoIdea
};
}
public ResultType Result()
{
string res = Node.GetAttribute("result");
return res switch
{
"Failed" => ResultType.Failed,
"Passed" => ResultType.Success,
"Skipped" => ResultType.Skipped,
"Warning" => ResultType.Warning,
_ => ResultType.NoIdea
};
}
public bool IsFailed => Result() == ResultType.Failed;
public SiteType Site()
{
string site = Node.GetAttribute("site");
return site switch
{
"SetUp" => SiteType.Setup,
"TearDown" => SiteType.TearDown,
_ => SiteType.NoIdea
};
}
public string Label => Node.GetAttribute("label");
public bool IsIgnored => Label == "Ignored";
public TimeSpan Duration => TimeSpan.FromSeconds(Node.GetAttribute("duration", 0.0));
protected NUnitTestEvent(string testEvent) : this(XmlHelper.CreateXmlNode(testEvent))
{ }
protected NUnitTestEvent(XmlNode node) : base(node)
{
}
public string MethodName => Node.GetAttribute("methodname");
public string ClassName => Node.GetAttribute("classname");
public string Output => Node.SelectSingleNode("output")?.InnerText;
public CheckedTime StartTime()
{
string startTime = Node.GetAttribute("start-time");
return startTime != null
? new CheckedTime { Ok = true, Time = DateTimeOffset.Parse(startTime, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal) }
: new CheckedTime { Ok = false, Time = DateTimeOffset.Now };
}
public CheckedTime EndTime()
{
string endTime = Node.GetAttribute("end-time");
return endTime != null
? new CheckedTime { Ok = true, Time = DateTimeOffset.Parse(endTime, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal) }
: new CheckedTime { Ok = false, Time = DateTimeOffset.Now };
}
public struct CheckedTime
{
public bool Ok;
public DateTimeOffset Time;
}
private List<NUnitAttachment> nUnitAttachments;
public IEnumerable<NUnitAttachment> NUnitAttachments
{
get
{
if (nUnitAttachments != null)
return nUnitAttachments;
nUnitAttachments = new List<NUnitAttachment>();
foreach (XmlNode attachment in Node.SelectNodes("attachments/attachment"))
{
var path = attachment.SelectSingleNode("filePath")?.InnerText ?? string.Empty;
var description = attachment.SelectSingleNode("description")?.InnerText;
nUnitAttachments.Add(new NUnitAttachment(path, description));
}
return nUnitAttachments;
}
}
}
public class NUnitAttachment
{
public NUnitAttachment(string path, string description)
{
FilePath = path;
Description = description;
}
public string FilePath { get; }
public string Description { get; }
}
public class NUnitProperty
{
public string Name { get; }
public string Value { get; }
public bool IsInternal => Name.StartsWith("_", StringComparison.Ordinal);
public NUnitProperty(string name, string value)
{
Name = name;
Value = value;
}
}
public class NUnitFailure
{
public string Message { get; }
public string Stacktrace { get; }
public NUnitFailure(string message, string stacktrace)
{
Message = message;
Stacktrace = stacktrace;
}
}
[Serializable]
public class NUnitEventWrongTypeException : Exception
{
// For guidelines regarding the creation of new exception types, see
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp
// and
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp
public NUnitEventWrongTypeException()
{
}
public NUnitEventWrongTypeException(string message) : base(message)
{
}
public NUnitEventWrongTypeException(string message, Exception inner) : base(message, inner)
{
}
protected NUnitEventWrongTypeException(
SerializationInfo info,
StreamingContext context) : base(info, context)
{
}
}
}