/
TestClass.cs
202 lines (178 loc) · 5.77 KB
/
TestClass.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
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Silverlight.Testing.UnitTesting.Metadata;
using StatLight.Client.Harness.Messaging;
using StatLight.Core.Configuration;
namespace StatLight.Client.Harness.Hosts.MSTest.UnitTestProviders.MSTest
{
/// <summary>
/// Test class wrapper.
/// </summary>
public class TestClass : ITestClass
{
/// <summary>
/// Construct a new test class metadata interface.
/// </summary>
/// <param name="assembly">Assembly metadata interface object.</param>
private TestClass(IAssembly assembly)
{
_tests = new List<ITestMethod>();
_m = new Dictionary<Methods, LazyDynamicMethodInfo>(4);
_m[Methods.ClassCleanup] = null;
_m[Methods.ClassInitialize] = null;
_m[Methods.TestCleanup] = null;
_m[Methods.TestInitialize] = null;
Assembly = assembly;
}
/// <summary>
/// Creates a new test class wrapper.
/// </summary>
/// <param name="assembly">Assembly metadata object.</param>
/// <param name="testClassType">Type of the class.</param>
public TestClass(IAssembly assembly, Type testClassType)
: this(assembly)
{
_type = testClassType;
if (_type == null)
{
throw new ArgumentNullException("testClassType");
}
_m[Methods.ClassCleanup] = new LazyDynamicMethodInfo(_type, ProviderAttributes.ClassCleanup);
_m[Methods.ClassInitialize] = new LazyDynamicMethodInfo(_type, ProviderAttributes.ClassInitialize);
_m[Methods.TestCleanup] = new LazyDynamicMethodInfo(_type, ProviderAttributes.TestCleanup);
_m[Methods.TestInitialize] = new LazyDynamicMethodInfo(_type, ProviderAttributes.TestInitialize);
}
/// <summary>
/// Methods enum.
/// </summary>
internal enum Methods
{
/// <summary>
/// Initialize method.
/// </summary>
ClassInitialize,
/// <summary>
/// Cleanup method.
/// </summary>
ClassCleanup,
/// <summary>
/// Test init method.
/// </summary>
TestInitialize,
/// <summary>
/// Test cleanup method.
/// </summary>
TestCleanup,
}
/// <summary>
/// Test Type.
/// </summary>
private Type _type;
/// <summary>
/// Collection of test method interface objects.
/// </summary>
private ICollection<ITestMethod> _tests;
/// <summary>
/// A value indicating whether tests are loaded.
/// </summary>
private bool _testsLoaded;
/// <summary>
/// A dictionary of method types and method interface objects.
/// </summary>
private IDictionary<Methods, LazyDynamicMethodInfo> _m;
/// <summary>
/// Gets the test assembly metadata.
/// </summary>
public IAssembly Assembly
{
get;
protected set;
}
/// <summary>
/// Gets the underlying Type of the test class.
/// </summary>
public Type Type
{
get { return _type; }
}
/// <summary>
/// Gets the name of the test class.
/// </summary>
public string Name
{
get { return _type.Name; }
}
/// <summary>
/// Gets a collection of test method wrapper instances.
/// </summary>
/// <returns>A collection of test method interface objects.</returns>
public ICollection<ITestMethod> GetTestMethods()
{
if (!_testsLoaded)
{
ICollection<MethodInfo> methods = GetTestMethods(_type);
_tests = new List<ITestMethod>(methods.Count);
foreach (MethodInfo method in methods)
{
if (ClientTestRunConfiguration.ContainsMethod(method))
{
Console.WriteLine("Begin - Adding method to test: " + method.FullName());
Server.Debug("Adding method to test: " + method.FullName());
// Using the MSTest frameworks's TestMethod here because
// 1. There is no specific StatLight logic in the original ITestMethod implemenation done by StatLight
// 2. The UnitTestContext class in the SL Testing Framework is protected and therefore I can't create that instance
_tests.Add(new Microsoft.Silverlight.Testing.UnitTesting.Metadata.VisualStudio.TestMethod(method));
Console.WriteLine("END - Adding method to test: " + method.FullName());
}
}
_testsLoaded = true;
}
return _tests;
}
public static ICollection<System.Reflection.MethodInfo> GetTestMethods(Type type)
{
var c = new List<System.Reflection.MethodInfo>();
foreach (var method in type.GetMethods())
if (DynamicAttributeHelper.HasAttribute(method, ProviderAttributes.TestMethod))
c.Add(method);
return c;
}
/// <summary>
/// Gets a value indicating whether an Ignore attribute present
/// on the class.
/// </summary>
public bool Ignore
{
get { return _type.HasAttribute(ProviderAttributes.IgnoreAttribute); }
}
/// <summary>
/// Gets any test initialize method.
/// </summary>
public MethodInfo TestInitializeMethod
{
get { return _m[Methods.TestInitialize] == null ? null : _m[Methods.TestInitialize].GetMethodInfo(); }
}
/// <summary>
/// Gets any test cleanup method.
/// </summary>
public MethodInfo TestCleanupMethod
{
get { return _m[Methods.TestCleanup] == null ? null : _m[Methods.TestCleanup].GetMethodInfo(); }
}
/// <summary>
/// Gets any class initialize method.
/// </summary>
public MethodInfo ClassInitializeMethod
{
get { return _m[Methods.ClassInitialize] == null ? null : _m[Methods.ClassInitialize].GetMethodInfo(); }
}
/// <summary>
/// Gets any class cleanup method.
/// </summary>
public MethodInfo ClassCleanupMethod
{
get { return _m[Methods.ClassCleanup] == null ? null : _m[Methods.ClassCleanup].GetMethodInfo(); }
}
}
}