-
Notifications
You must be signed in to change notification settings - Fork 624
/
LuceneTestCase.TestFixtureAttribute.cs
265 lines (227 loc) · 9.11 KB
/
LuceneTestCase.TestFixtureAttribute.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using Lucene.Net.Support;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Lucene.Net.Util
{
#region Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License.
// Copyright (c) 2021 Charlie Poole, Rob Prouse
//
// 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.
#endregion
public abstract partial class LuceneTestCase
{
/// <summary>
/// Marks the class as a TestFixture.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public class TestFixtureAttribute : NUnitAttribute, IFixtureBuilder2, ITestFixtureData
{
private readonly NUnitTestFixtureBuilder _builder = new NUnitTestFixtureBuilder();
#region Constructors
/// <summary>
/// Default constructor
/// </summary>
public TestFixtureAttribute() : this(new object[0]) { }
/// <summary>
/// Construct with a object[] representing a set of arguments.
/// The arguments may later be separated into type arguments and constructor arguments.
/// </summary>
/// <param name="arguments"></param>
public TestFixtureAttribute(params object[] arguments)
{
RunState = RunState.Runnable;
Arguments = arguments ?? new object[] { null };
TypeArgs = new Type[0];
Properties = new PropertyBag();
}
#endregion
#region ITestData Members
/// <summary>
/// Gets or sets the name of the test.
/// </summary>
/// <value>The name of the test.</value>
public string TestName { get; set; }
/// <summary>
/// Gets or sets the RunState of this test fixture.
/// </summary>
public RunState RunState { get; private set; }
/// <summary>
/// The arguments originally provided to the attribute
/// </summary>
[WritableArray]
public object[] Arguments { get; }
/// <summary>
/// Properties pertaining to this fixture
/// </summary>
public IPropertyBag Properties { get; }
#endregion
#region ITestFixtureData Members
/// <summary>
/// Get or set the type arguments. If not set
/// explicitly, any leading arguments that are
/// Types are taken as type arguments.
/// </summary>
[WritableArray]
public Type[] TypeArgs { get; set; }
#endregion
#region Other Properties
/// <summary>
/// Descriptive text for this fixture
/// </summary>
public string Description
{
get { return Properties.Get(PropertyNames.Description) as string; }
set { Properties.Set(PropertyNames.Description, value); }
}
/// <summary>
/// The author of this fixture
/// </summary>
public string Author
{
get { return Properties.Get(PropertyNames.Author) as string; }
set { Properties.Set(PropertyNames.Author, value); }
}
/// <summary>
/// The type that this fixture is testing
/// </summary>
public Type TestOf
{
get { return _testOf; }
set
{
_testOf = value;
Properties.Set(PropertyNames.TestOf, value.FullName);
}
}
private Type _testOf;
/// <summary>
/// Gets or sets the ignore reason. May set RunState as a side effect.
/// </summary>
/// <value>The ignore reason.</value>
public string Ignore
{
get { return IgnoreReason; }
set { IgnoreReason = value; }
}
/// <summary>
/// Gets or sets the reason for not running the fixture.
/// </summary>
/// <value>The reason.</value>
public string Reason
{
get { return this.Properties.Get(PropertyNames.SkipReason) as string; }
set { this.Properties.Set(PropertyNames.SkipReason, value); }
}
/// <summary>
/// Gets or sets the ignore reason. When set to a non-null
/// non-empty value, the test is marked as ignored.
/// </summary>
/// <value>The ignore reason.</value>
public string IgnoreReason
{
get { return Reason; }
set
{
RunState = RunState.Ignored;
Reason = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="NUnit.Framework.TestFixtureAttribute"/> is explicit.
/// </summary>
/// <value>
/// <c>true</c> if explicit; otherwise, <c>false</c>.
/// </value>
public bool Explicit
{
get { return RunState == RunState.Explicit; }
set { RunState = value ? RunState.Explicit : RunState.Runnable; }
}
/// <summary>
/// Gets and sets the category for this fixture.
/// May be a comma-separated list of categories.
/// </summary>
public string Category
{
get
{
//return Properties.Get(PropertyNames.Category) as string;
var catList = Properties[PropertyNames.Category];
if (catList is null)
return null;
switch (catList.Count)
{
case 0:
return null;
case 1:
return catList[0] as string;
default:
var cats = new string[catList.Count];
int index = 0;
foreach (string cat in catList)
cats[index++] = cat;
return string.Join(",", cats);
}
}
set
{
foreach (string cat in value.Split(new char[] { ',' }))
Properties.Add(PropertyNames.Category, cat);
}
}
#endregion
#region IFixtureBuilder Members
/// <summary>
/// Builds a single test fixture from the specified type.
/// </summary>
public IEnumerable<TestSuite> BuildFrom(ITypeInfo typeInfo)
{
yield return _builder.BuildFrom(typeInfo, AlwaysMatchPreFilter.Instance, this);
}
#endregion
#region IFixtureBuilder2 Members
/// <summary>
/// Builds a single test fixture from the specified type.
/// </summary>
/// <param name="typeInfo">The type info of the fixture to be used.</param>
/// <param name="filter">Filter used to select methods as tests.</param>
public IEnumerable<TestSuite> BuildFrom(ITypeInfo typeInfo, IPreFilter filter)
{
yield return _builder.BuildFrom(typeInfo, filter, this);
}
#endregion
}
}
internal class AlwaysMatchPreFilter : IPreFilter
{
public static IPreFilter Instance { get; } = new AlwaysMatchPreFilter();
public bool IsMatch(Type type)
{
return true;
}
public bool IsMatch(Type type, MethodInfo method)
{
return true;
}
}
}