~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/monomac/samples/CFNetwork/AsyncTests.Framework/AsyncTests.Framework/TestContext.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// AsyncTests.Framework.TestContext
 
3
//
 
4
// Authors:
 
5
//      Martin Baulig (martin.baulig@gmail.com)
 
6
//
 
7
// Copyright 2012 Xamarin Inc. (http://www.xamarin.com)
 
8
//
 
9
//
 
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:
 
17
// 
 
18
// The above copyright notice and this permission notice shall be
 
19
// included in all copies or substantial portions of the Software.
 
20
// 
 
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.
 
28
//
 
29
using System;
 
30
using System.Linq;
 
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;
 
39
 
 
40
namespace AsyncTests.Framework {
 
41
        public abstract class TestContext : IDisposable {
 
42
                List<TestError> errors;
 
43
                List<TestWarning> warnings;
 
44
                List<IDisposable> disposables;
 
45
                int countAssertions;
 
46
 
 
47
                public TestFixture Fixture {
 
48
                        get;
 
49
                        private set;
 
50
                }
 
51
 
 
52
                internal object Instance {
 
53
                        get;
 
54
                        private set;
 
55
                }
 
56
 
 
57
                public ThreadingMode ThreadingMode {
 
58
                        get;
 
59
                        internal set;
 
60
                }
 
61
 
 
62
                public TestConfiguration Configuration {
 
63
                        get;
 
64
                        internal set;
 
65
                }
 
66
 
 
67
                protected TestContext (TestFixture fixture, object instance)
 
68
                {
 
69
                        this.Fixture = fixture;
 
70
                        this.Instance = instance;
 
71
                        ThreadingMode = ThreadingMode.Default;
 
72
                }
 
73
 
 
74
                public virtual void Log (string message, params object[] args)
 
75
                {
 
76
                        Fixture.Log (message, args);
 
77
                }
 
78
 
 
79
                public T GetConfiguration<T> ()
 
80
                        where T : TestConfiguration
 
81
                {
 
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;
 
86
                }
 
87
 
 
88
                internal void ClearErrors ()
 
89
                {
 
90
                        errors = null;
 
91
                        warnings = null;
 
92
                        countAssertions = 0;
 
93
                }
 
94
 
 
95
                internal void AddError (string name, Exception error)
 
96
                {
 
97
                        if (errors == null)
 
98
                                errors = new List<TestError> ();
 
99
                        errors.Add (new TestError (name, null, error));
 
100
                }
 
101
 
 
102
                public bool HasErrors {
 
103
                        get { return errors != null; }
 
104
                }
 
105
 
 
106
                internal IList<TestError> Errors {
 
107
                        get {
 
108
                                return HasErrors ? errors.AsReadOnly () : null;
 
109
                        }
 
110
                }
 
111
 
 
112
                public bool HasWarnings {
 
113
                        get { return warnings != null; }
 
114
                }
 
115
 
 
116
                public IList<TestWarning> Warnings {
 
117
                        get {
 
118
                                return HasWarnings ? warnings.AsReadOnly () : null;
 
119
                        }
 
120
                }
 
121
 
 
122
                internal void CheckErrors (string message)
 
123
                {
 
124
                        if (errors == null)
 
125
                                return;
 
126
                        throw new TestErrorException (message, errors.ToArray ());
 
127
                }
 
128
 
 
129
                protected internal Task Invoke (TestCase test, CancellationToken cancellationToken)
 
130
                {
 
131
                        object[] args;
 
132
                        if (test.Method.GetParameters ().Length == 1)
 
133
                                args = new object[] { this };
 
134
                        else
 
135
                                args = new object[] { this, cancellationToken };
 
136
                        return Invoke_internal (test.Name, test.Method, Instance, args);
 
137
                }
 
138
 
 
139
                protected internal async Task Invoke_internal (string name, MethodInfo method,
 
140
                                                               object instance, object[] args)
 
141
                {
 
142
                        ClearErrors ();
 
143
 
 
144
                        try {
 
145
                                var retval = method.Invoke (instance, args);
 
146
 
 
147
                                var task = retval as Task;
 
148
                                if (task != null)
 
149
                                        await task;
 
150
                        } finally {
 
151
                                AutoDispose ();
 
152
                        }
 
153
 
 
154
                        CheckErrors (name);
 
155
                }
 
156
 
 
157
                #region Assertions
 
158
 
 
159
                /*
 
160
                 * By default, Exepct() is non-fatal.  Multiple failed expections will be
 
161
                 * collected and a TestErrorException will be thrown when the test method
 
162
                 * returns.
 
163
                 * 
 
164
                 * Use Assert() to immediately abort the test method or set 'AlwaysFatal = true'.
 
165
                 * 
 
166
                 */
 
167
 
 
168
                public bool AlwaysFatal {
 
169
                        get;
 
170
                        set;
 
171
                }
 
172
 
 
173
                public bool Expect (object actual, Constraint constraint)
 
174
                {
 
175
                        return Expect (false, actual, constraint, null, null);
 
176
                }
 
177
 
 
178
                public bool Expect (object actual, Constraint constraint, string message)
 
179
                {
 
180
                        return Expect (false, actual, constraint, message, null);
 
181
                }
 
182
 
 
183
                public bool Expect (object actual, Constraint constraint,
 
184
                                    string message, params object[] args)
 
185
                {
 
186
                        return Expect (false, actual, constraint, message, args);
 
187
                }
 
188
 
 
189
                public bool Expect (bool fatal, object actual, Constraint constraint,
 
190
                                    string message, params object[] args)
 
191
                {
 
192
                        if (constraint.Matches (actual)) {
 
193
                                ++countAssertions;
 
194
                                return true;
 
195
                        }
 
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)) {
 
201
                                        if (args != null)
 
202
                                                text = string.Format (message, args);
 
203
                                        else
 
204
                                                text = message;
 
205
                                }
 
206
                                AddError (text, error);
 
207
                                if (AlwaysFatal || fatal)
 
208
                                        throw error;
 
209
                                return false;
 
210
                        }
 
211
                }
 
212
 
 
213
                public void Assert (object actual, Constraint constraint)
 
214
                {
 
215
                        Expect (true, actual, constraint, null, null);
 
216
                }
 
217
 
 
218
                public void Assert (object actual, Constraint constraint, string message)
 
219
                {
 
220
                        Expect (true, actual, constraint, message, null);
 
221
                }
 
222
 
 
223
                public void Assert (object actual, Constraint constraint,
 
224
                                    string message, params object[] args)
 
225
                {
 
226
                        Expect (true, actual, constraint, message, args);
 
227
                }
 
228
 
 
229
                public bool Expect (bool condition, string message, params object[] args)
 
230
                {
 
231
                        return Expect (false, condition, Is.True, message, args);
 
232
                }
 
233
 
 
234
                public bool Expect (bool condition, string message)
 
235
                {
 
236
                        return Expect (false, condition, Is.True, message, null);
 
237
                }
 
238
 
 
239
                public bool Expect (bool condition)
 
240
                {
 
241
                        return Expect (false, condition, Is.True, null, null);
 
242
                }
 
243
 
 
244
                public void Assert (bool condition, string message, params object[] args)
 
245
                {
 
246
                        Expect (true, condition, Is.True, message, args);
 
247
                }
 
248
 
 
249
                public void Assert (bool condition, string message)
 
250
                {
 
251
                        Expect (true, condition, Is.True, message, null);
 
252
                }
 
253
 
 
254
                public void Assert (bool condition)
 
255
                {
 
256
                        Expect (true, condition, Is.True, null, null);
 
257
                }
 
258
 
 
259
                public void Warning (string message, params object[] args)
 
260
                {
 
261
                        Warning (string.Format (message, args));
 
262
                }
 
263
 
 
264
                public void Warning (string message)
 
265
                {
 
266
                        if (warnings == null)
 
267
                                warnings = new List<TestWarning> ();
 
268
                        warnings.Add (new TestWarning (message));
 
269
                }
 
270
 
 
271
                #endregion
 
272
 
 
273
                #region Disposing
 
274
 
 
275
                public void AutoDispose (IDisposable disposable)
 
276
                {
 
277
                        if (disposable == null)
 
278
                                return;
 
279
                        if (disposables == null)
 
280
                                disposables = new List<IDisposable> ();
 
281
                        disposables.Add (disposable);
 
282
                }
 
283
 
 
284
                void AutoDispose ()
 
285
                {
 
286
                        if (disposables == null)
 
287
                                return;
 
288
                        foreach (var disposable in disposables) {
 
289
                                try {
 
290
                                        disposable.Dispose ();
 
291
                                } catch (Exception ex) {
 
292
                                        AddError ("Auto-dispose failed", ex);
 
293
                                }
 
294
                        }
 
295
                        disposables = null;
 
296
                }
 
297
 
 
298
                ~TestContext ()
 
299
                {
 
300
                        Dispose (false);
 
301
                }
 
302
                
 
303
                public void Dispose ()
 
304
                {
 
305
                        Dispose (true);
 
306
                        GC.SuppressFinalize (this);
 
307
                }
 
308
 
 
309
                protected virtual void Dispose (bool disposing)
 
310
                {
 
311
                }
 
312
 
 
313
                #endregion
 
314
        }
 
315
}