Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TestResult.cs « NUnitCore « src « nunit « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ff274a8c4ff22c554eacc80a4a893271c68b7a4f (plain)
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
namespace NUnit.Framework 
{
	using System;
	using System.Collections;
	using System.Threading;

	/// <summary>A <c>TestResult</c> collects the results of executing
	/// a test case. It is an instance of the Collecting Parameter pattern.
	/// </summary><remarks>
	/// The test framework distinguishes between failures and errors.
	/// A failure is anticipated and checked for with assertions. Errors are
	/// unanticipated problems like an <c>ArgumentOutOfRangeException</c>.
	/// </remarks><seealso cref="ITest"/>
	public class TestResult : MarshalByRefObject
	{
		#region Instance Variables
		private ArrayList fFailures;
		private ArrayList fErrors;
		private ArrayList fListeners;
		private int fRunTests;
		private bool fStop;
		#endregion
		
		#region Constructors
		/// <summary>
		/// 
		/// </summary>
		public TestResult() 
		{
			fFailures= new ArrayList();
			fErrors= new ArrayList();
			fListeners= new ArrayList();
		}
		#endregion

		#region Collection Methods
		/// <summary>
		/// Adds an error to the list of errors. The passed in exception
		/// caused the error.
		/// </summary>
		public void AddError(ITest test, Exception error) 
		{
			lock(this)
			{
				this.fErrors.Add(new TestFailure(test, error));
				foreach (ITestListener listner in CloneListeners()) 
				{
					listner.AddError(test, error);
				}
			}
		}
		/// <summary>
		/// Adds a failure to the list of failures. The passed in
		/// exception caused the failure.
		/// </summary>
		public void AddFailure(ITest test, AssertionFailedError failure)
		{
			lock(this)
			{
				fFailures.Add(new TestFailure(test, failure));
				foreach (ITestListener listner in CloneListeners()) 
				{
					listner.AddFailure(test, failure);
				}
			}
		}
		#endregion

		#region Events
		/// <summary>Registers a TestListener.</summary>
		public void AddListener(ITestListener listener) 
		{
			lock(this) 
				this.fListeners.Add(listener);
		}
		/// <summary>Unregisters a TestListener</summary>
		public void RemoveListener(ITestListener listener) 
		{
			lock(this) 
			{
				fListeners.Remove(listener);
			}
		}
		/// <summary>Returns a copy of the listeners.</summary>
		private ArrayList CloneListeners() 
		{
			lock(this)
			{
				return (ArrayList)fListeners.Clone();
			}
		}
		/// <summary>Informs the result that a test was completed.</summary>
		public void EndTest(ITest test) 
		{
			foreach (ITestListener listner in CloneListeners()) 
			{
				listner.EndTest(test);
			}
		}
		/// <summary>Informs the result that a test will be started.</summary>
		public void StartTest(ITest test) 
		{
			lock(this)
			{
				this.fRunTests += test.CountTestCases;
			}
			foreach (ITestListener listner in CloneListeners()) 
			{
				listner.StartTest(test);
			}
		}
		#endregion

		#region Properties
		/// <value>Gets the number of run tests.</value>
		public int RunCount 
		{
			get {lock(this)return this.fRunTests; }
		}
		/// <value>Gets the number of detected errors.</value>
		public int ErrorCount 
		{
			get {lock(this)return this.fErrors.Count; }
		}  
		/// <value>Gets the number of detected failures.</value>
		public int FailureCount 
		{
			get {lock(this)return this.fFailures.Count; }
		}
		/// <summary>Checks whether the test run should stop.</summary>
		public bool ShouldStop 
		{
			get {lock(this)return this.fStop; }
		}
		/// <value>Returns whether the entire test was successful or not.</value>
		public bool WasSuccessful 
		{
			get 
			{
				lock(this)
				{
					return (this.FailureCount == 0)
						&& (this.ErrorCount == 0);
				}
			}
		}
		/// <value>Returns a TestFailure[] for the errors.</value>
		public TestFailure[] Errors
		{
			get
			{ 
				lock(this)
				{
					TestFailure[] retVal = new TestFailure[this.fErrors.Count];
					this.fErrors.CopyTo(retVal);
					return retVal;
				}	
			}
		}
		/// <value>Returns a TestFauiler[] for the failures.</value>
		public TestFailure[] Failures 
		{
			get
			{
				lock(this)
				{
					TestFailure[] retVal = new TestFailure[this.fFailures.Count];
					this.fFailures.CopyTo(retVal);
					return retVal;
				}
			}
		}
		#endregion
		
		#region Nested Classes
		/// <summary>Runs a TestCase.</summary>
		protected class ProtectedProtect: IProtectable 
		{
			private TestCase fTest;
			/// <summary>
			/// 
			/// </summary>
			/// <param name="test"></param>
			public ProtectedProtect(TestCase test) 
			{
				if(test != null)
				{
					this.fTest = test;
				}
				else
				{
					throw new ArgumentNullException("test");
				}
			}
			/// <summary>
			/// 
			/// </summary>
			public void Protect() 
			{
				this.fTest.RunBare();
			}
		}
		#endregion

		#region Run Methods
		/// <summary>Runs a TestCase.</summary>
		internal void Run(TestCase test) 
		{
			StartTest(test);
			IProtectable p = new ProtectedProtect(test);
			RunProtected(test, p);
			EndTest(test);
		}

		/// <summary>Runs a TestCase.</summary>
		public void RunProtected(ITest test, IProtectable p) 
		{
			try 
			{
				p.Protect();
			}
			catch (AssertionFailedError e) 
			{
				AddFailure(test, e);
			}
			catch (NUnitException e) 
			{
				if (e.IsAssertionFailure)
					AddFailure(test, (AssertionFailedError)e.InnerException);
				else
					AddError(test, e.InnerException);
			}
			catch (ThreadAbortException e) 
			{ // don't catch by accident
				throw e;
			}
			catch (System.Exception e) 
			{
				AddError(test, e);
			}
		}
		/// <summary>Marks that the test run should stop.</summary>
		public void Stop() 
		{
			fStop= true;
		}
		#endregion
	}
}