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

BaseTestRunner.cs « NUnitCore « src « nunit « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7f3adfc6733ef880a2242fda17767afceb8f67d9 (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
namespace NUnit.Runner 
{
	using System;
	using System.Collections;
	using System.Collections.Specialized;
	using System.IO;
	using System.IO.IsolatedStorage;
	using System.Reflection;
	using NUnit.Framework;

	/// <summary>
	/// Base class for all test runners.
	/// </summary>
	/// <remarks>
	/// 
	/// </remarks>
	public abstract class BaseTestRunner: MarshalByRefObject, ITestListener
	{
		/// <summary>
		/// 
		/// </summary>
		[Obsolete("Shoud be handled by a loader")]
		public static string SUITE_PROPERTYNAME="Suite";

		private static NameValueCollection fPreferences = new NameValueCollection();
		private static int fgMaxMessageLength = 500;
		private  static bool fgFilterStack = true;

		private bool fLoading = true;
		/// <summary>
		/// 
		/// </summary>
		public BaseTestRunner() 
		{
			fPreferences = new NameValueCollection();
			fPreferences.Add("loading", "true");
			fPreferences.Add("filterstack", "true");
			ReadPreferences();
			fgMaxMessageLength = GetPreference("maxmessage", fgMaxMessageLength);
		}
		
		#region ITestListener Methods
		/// <summary>
		/// 
		/// </summary>
		/// <param name="test"></param>
		/// <param name="t"></param>
		public abstract void AddError(ITest test, Exception t);
		
		/// <summary>
		/// 
		/// </summary>
		/// <param name="test"></param>
		/// <param name="t"></param>
		public abstract void AddFailure(ITest test, AssertionFailedError t);
		
		/// <summary>
		/// 
		/// </summary>
		/// <param name="test"></param>
		public abstract void EndTest(ITest test);
		#endregion

#if false
		/// <summary>
		/// Clears the status message.
		/// </summary>
		protected virtual void ClearStatus() 
		{
			// Belongs in the GUI TestRunner class.
		}
#endif		
		/// <summary>
		/// Returns the formatted string of the elapsed time.
		/// </summary>
		public static string ElapsedTimeAsString(long runTime) 
		{
			return ((double)runTime/1000).ToString();
		}
		/// <summary>
		/// Extract the class name from a string in VA/Java style
		/// </summary>
		public static string ExtractClassName(string className) 
		{
			if(className.StartsWith("Default package for")) 
				return className.Substring(className.LastIndexOf(".")+1);
			return className;
		}
    
		static bool FilterLine(string line) 
		{
			string[] patterns = new string[]
				{
					"NUnit.Framework.TestCase",
					"NUnit.Framework.TestResult",
					"NUnit.Framework.TestSuite",
					"NUnit.Framework.Assertion." // don't filter AssertionFailure
				};
			for (int i = 0; i < patterns.Length; i++) 
			{
				if (line.IndexOf(patterns[i]) > 0)
					return true;
			}
			return false;
		}

		/// <summary>
		/// Filters stack frames from internal NUnit classes
		/// </summary>
		public static string FilterStack(string stack) 
		{
			string pref = GetPreference("filterstack");
			if (((pref != null) && !GetPreference("filterstack").Equals("true"))
				|| fgFilterStack == false)
				return stack;

			StringWriter sw = new StringWriter();
			StringReader sr = new StringReader(stack);

			try 
			{
				string line;
				while ((line = sr.ReadLine()) != null) 
				{
					if (!FilterLine(line))
						sw.WriteLine(line);
				}
			} 
			catch (Exception) 
			{
				return stack; // return the stack unfiltered
			}
			return sw.ToString();
		}
		
		/// <summary>
		/// 
		/// </summary>
		/// <param name="t"></param>
		/// <returns></returns>
		public static string GetFilteredTrace(Exception t) 
		{
			return BaseTestRunner.FilterStack(t.StackTrace);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public static string GetPreference(string key) 
		{
			return fPreferences.Get(key);
		}

		private static int GetPreference(String key, int dflt) 
		{
			String value= GetPreference(key);
			int intValue= dflt;
			if (value != null)
			{
				try 
				{
					intValue= int.Parse(value);
				} 
				catch (FormatException) {}
			}
			return intValue;
		}

		private static FileStream GetPreferencesFile() 
		{
			return new IsolatedStorageFileStream("NUnit.Prefs", FileMode.OpenOrCreate);
		}
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public virtual ITestLoader GetLoader() 
		{
			if (UseReloadingTestSuiteLoader())
				return new UnloadingLoader();
			return new StandardLoader();
		}

		/// <summary>
		/// Returns the ITest corresponding to the given suite. This is
		/// a template method, subclasses override RunFailed(), ClearStatus().
		/// </summary>
		public ITest GetTest(string suiteClassName) 
		{
			ITest test = null;
			try 
			{
				test = LoadSuiteClass(suiteClassName);
			} 
			catch (TypeLoadException e) 
			{
				RunFailed(e.Message);
				return null;
			} 
			catch (Exception e) 
			{
				RunFailed("Error: " + e.ToString());
				return null;
			}
			//ClearStatus();
			return test;  
		}

		/// <summary>
		/// Returns the loaded Class for a suite name. 
		/// </summary>
		protected ITest LoadSuiteClass(string suiteClassName) 
		{
			return GetLoader().LoadTest(suiteClassName);
		}
		
		private static void ReadPreferences() 
		{
			FileStream fs= null;
			try 
			{
				fs= GetPreferencesFile();
				fPreferences= new NameValueCollection(fPreferences);
				ReadPrefsFromFile(ref fPreferences, fs);
			} 
			catch (IOException) 
			{
				try 
				{
					if (fs != null)
						fs.Close();
				} 
				catch (IOException) 
				{
				}
			}
		}
		
		/// <summary>
		/// Real method reads name/value pairs, populates, or maybe just
		/// deserializes...
		/// </summary>
		/// <param name="prefs"></param>
		/// <param name="fs"></param>
		private static void ReadPrefsFromFile(ref NameValueCollection prefs, FileStream fs) 
		{
		}
		
		/// <summary>
		/// Override to define how to handle a failed loading of a test suite.
		/// </summary>
		protected abstract void RunFailed(string message);
		
		/// <summary>
		/// Truncates a String to the maximum length.
		/// </summary>
		/// <param name="message"></param>
		/// <returns></returns>
		public static string Truncate(string message) 
		{
			if (fgMaxMessageLength != -1 && message.Length > fgMaxMessageLength)
				message = message.Substring(0, fgMaxMessageLength)+"...";
			return message;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="test"></param>
		public abstract void StartTest(ITest test);

		/// <summary>
		/// 
		/// </summary>
		/// <param name="args"></param>
		/// <param name="wait"></param>
		/// <returns></returns>
		protected string ProcessArguments(string[] args, ref bool wait) 
		{
			string suiteName="";
			wait = false;
			foreach (string arg in args) 
			{
				if (arg.Equals("/noloading"))
					SetLoading(false);
				else if (arg.Equals("/nofilterstack")) 
					fgFilterStack = false;
				else if (arg.Equals("/wait")) 
					wait = true;
				else if (arg.Equals("/c"))
					suiteName= ExtractClassName(arg);
				else if (arg.Equals("/v"))
				{
					Console.Error.WriteLine("NUnit "+NUnit.Runner.Version.id()
						+ " by Philip Craig");
					Console.Error.WriteLine("ported from JUnit 3.6 by Kent Beck"
						+ " and Erich Gamma");
				} 
				else
					suiteName = arg;
			}
			return suiteName;
		}
		
		/// <summary>
		/// Sets the loading behaviour of the test runner
		/// </summary>
		protected void SetLoading(bool enable) 
		{
			fLoading = enable;
		}
		
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		protected bool UseReloadingTestSuiteLoader() 
		{
			return bool.TrueString.Equals( GetPreference("loading")) && fLoading;
		}
	}
}