2
// AsyncTests.Framework.TestContext
5
// Martin Baulig (martin.baulig@gmail.com)
7
// Copyright 2012 Xamarin Inc. (http://www.xamarin.com)
10
// Permission is hereby granted, free of charge, to any person obtaining
11
// a copy of this software and associated documentation files (the
12
// "Software"), to deal in the Software without restriction, including
13
// without limitation the rights to use, copy, modify, merge, publish,
14
// distribute, sublicense, and/or sell copies of the Software, and to
15
// permit persons to whom the Software is furnished to do so, subject to
16
// the following conditions:
18
// The above copyright notice and this permission notice shall be
19
// included in all copies or substantial portions of the Software.
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
using System.Reflection;
32
using System.Collections.Generic;
33
using System.Runtime.CompilerServices;
34
using System.Threading;
35
using System.Threading.Tasks;
36
using NUnit.Framework;
37
using NUnit.Framework.Constraints;
38
using NUnit.Framework.SyntaxHelpers;
40
namespace AsyncTests.Framework {
41
public abstract class TestContext : IDisposable {
42
List<TestError> errors;
43
List<TestWarning> warnings;
44
List<IDisposable> disposables;
47
public TestFixture Fixture {
52
internal object Instance {
57
public ThreadingMode ThreadingMode {
62
public TestConfiguration Configuration {
67
protected TestContext (TestFixture fixture, object instance)
69
this.Fixture = fixture;
70
this.Instance = instance;
71
ThreadingMode = ThreadingMode.Default;
74
public virtual void Log (string message, params object[] args)
76
Fixture.Log (message, args);
79
public T GetConfiguration<T> ()
80
where T : TestConfiguration
82
var message = string.Format ("GetConfiguration({0})", typeof (T).FullName);
83
Assert (Configuration, Is.Not.Null, "GetConfiguration({0})", message);
84
Assert (Configuration, Is.InstanceOfType (typeof (T)), message);
85
return (T)Configuration;
88
internal void ClearErrors ()
95
internal void AddError (string name, Exception error)
98
errors = new List<TestError> ();
99
errors.Add (new TestError (name, null, error));
102
public bool HasErrors {
103
get { return errors != null; }
106
internal IList<TestError> Errors {
108
return HasErrors ? errors.AsReadOnly () : null;
112
public bool HasWarnings {
113
get { return warnings != null; }
116
public IList<TestWarning> Warnings {
118
return HasWarnings ? warnings.AsReadOnly () : null;
122
internal void CheckErrors (string message)
126
throw new TestErrorException (message, errors.ToArray ());
129
protected internal Task Invoke (TestCase test, CancellationToken cancellationToken)
132
if (test.Method.GetParameters ().Length == 1)
133
args = new object[] { this };
135
args = new object[] { this, cancellationToken };
136
return Invoke_internal (test.Name, test.Method, Instance, args);
139
protected internal async Task Invoke_internal (string name, MethodInfo method,
140
object instance, object[] args)
145
var retval = method.Invoke (instance, args);
147
var task = retval as Task;
160
* By default, Exepct() is non-fatal. Multiple failed expections will be
161
* collected and a TestErrorException will be thrown when the test method
164
* Use Assert() to immediately abort the test method or set 'AlwaysFatal = true'.
168
public bool AlwaysFatal {
173
public bool Expect (object actual, Constraint constraint)
175
return Expect (false, actual, constraint, null, null);
178
public bool Expect (object actual, Constraint constraint, string message)
180
return Expect (false, actual, constraint, message, null);
183
public bool Expect (object actual, Constraint constraint,
184
string message, params object[] args)
186
return Expect (false, actual, constraint, message, args);
189
public bool Expect (bool fatal, object actual, Constraint constraint,
190
string message, params object[] args)
192
if (constraint.Matches (actual)) {
196
using (var writer = new TextMessageWriter (message, args)) {
197
constraint.WriteMessageTo (writer);
198
var error = new AssertionException (writer.ToString ());
199
string text = string.Empty;;
200
if ((message != null) && (message != string.Empty)) {
202
text = string.Format (message, args);
206
AddError (text, error);
207
if (AlwaysFatal || fatal)
213
public void Assert (object actual, Constraint constraint)
215
Expect (true, actual, constraint, null, null);
218
public void Assert (object actual, Constraint constraint, string message)
220
Expect (true, actual, constraint, message, null);
223
public void Assert (object actual, Constraint constraint,
224
string message, params object[] args)
226
Expect (true, actual, constraint, message, args);
229
public bool Expect (bool condition, string message, params object[] args)
231
return Expect (false, condition, Is.True, message, args);
234
public bool Expect (bool condition, string message)
236
return Expect (false, condition, Is.True, message, null);
239
public bool Expect (bool condition)
241
return Expect (false, condition, Is.True, null, null);
244
public void Assert (bool condition, string message, params object[] args)
246
Expect (true, condition, Is.True, message, args);
249
public void Assert (bool condition, string message)
251
Expect (true, condition, Is.True, message, null);
254
public void Assert (bool condition)
256
Expect (true, condition, Is.True, null, null);
259
public void Warning (string message, params object[] args)
261
Warning (string.Format (message, args));
264
public void Warning (string message)
266
if (warnings == null)
267
warnings = new List<TestWarning> ();
268
warnings.Add (new TestWarning (message));
275
public void AutoDispose (IDisposable disposable)
277
if (disposable == null)
279
if (disposables == null)
280
disposables = new List<IDisposable> ();
281
disposables.Add (disposable);
286
if (disposables == null)
288
foreach (var disposable in disposables) {
290
disposable.Dispose ();
291
} catch (Exception ex) {
292
AddError ("Auto-dispose failed", ex);
303
public void Dispose ()
306
GC.SuppressFinalize (this);
309
protected virtual void Dispose (bool disposing)