File: StackFrameTest.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (450 lines) | stat: -rw-r--r-- 11,061 bytes parent folder | download | duplicates (5)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//
// MonoTests.System.Diagnostics.StackFrameTest.cs
//
// Author:
//      Alexander Klyubin (klyubin@aqris.com)
//
// (C) 2001
//

using System;
using System.Diagnostics;
using System.Reflection;
using NUnit.Framework;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;

namespace MonoTests.System.Diagnostics
{
	/// <summary>
	/// Tests the case where StackFrame is created for specified file name and
	/// location inside it.
	/// </summary>
	[TestFixture]
	public class StackFrameTest1
	{
		private StackFrame frame1;
		private StackFrame frame2;

		[SetUp]
		public void SetUp ()
		{
			frame1 = new StackFrame ("dir/someFile", 13, 45);
			frame2 = new StackFrame ("SomeFile2.cs", 24);
		}

		[TearDown]
		public void TearDown ()
		{
			frame1 = null;
			frame2 = null;
		}

		/// <summary>
		///   Tests whether getting file name works.
		/// </summary>
		[Test]
		[Category("LLVMNotWorking")]
		public void TestGetFileName ()
		{
			Assert.AreEqual ("dir/someFile",
						 frame1.GetFileName (),
						 "File name (1)");

			Assert.AreEqual ("SomeFile2.cs",
						 frame2.GetFileName (),
						 "File name (2)");
		}

		/// <summary>
		/// Tests whether getting file line number works.
		/// </summary>
		[Test]
		[Category("LLVMNotWorking")]
		public void TestGetFileLineNumber ()
		{
			Assert.AreEqual (13,
							 frame1.GetFileLineNumber (),
							 "Line number (1)");

			Assert.AreEqual (24,
							 frame2.GetFileLineNumber (),
							 "Line number (2)");
		}

		/// <summary>
		/// Tests whether getting file column number works.
		/// </summary>
		[Test]
		public void TestGetFileColumnNumber ()
		{
			Assert.AreEqual (45,
							 frame1.GetFileColumnNumber (),
							 "Column number (1)");

			Assert.AreEqual (0,
							 frame2.GetFileColumnNumber (),
							 "Column number (2)");
		}

		/// <summary>
		/// Tests whether getting method associated with frame works.
		/// </summary>
		[Test]
		[Category("StackWalks")]
		[Category("NotWasm")]
		public void TestGetMethod ()
		{
			Assert.IsTrue ((frame1.GetMethod () != null), "Method not null (1)");

			Assert.AreEqual (this.GetType (),
							 frame1.GetMethod ().DeclaringType,
							 "Class declaring the method (1)");
			Assert.AreEqual ("SetUp",
							 frame1.GetMethod ().Name,
							 "Method name (1)");

			Assert.IsTrue ((frame2.GetMethod () != null), "Method not null (2)");

			Assert.AreEqual (this.GetType (),
							 frame2.GetMethod ().DeclaringType,
							 "Class declaring the method (2)");
			Assert.AreEqual ("SetUp",
							 frame2.GetMethod ().Name,
							 "Method name (2)");
		}
	}

	/// <summary>
	/// Tests the case where StackFrame is created for current method.
	/// </summary>
	/// <remarks>
	/// FIXME: Must be compiled with /debug switch. Otherwise some file
	/// information will be incorrect for the following test cases.
	/// What's the best way to do both types of tests with and without
	/// debug information?
	/// </remarks>
	[TestFixture]
	public class StackFrameTest2
	{
		private StackFrame frame1;
		private StackFrame frame2;
		private StackFrame frame3;

		[SetUp]
		public void SetUp ()
		{
			frame1 = new StackFrame ();
			frame2 = new StackFrame (true);
			frame3 = new StackFrame (0);
		}

		[TearDown]
		public void TearDown ()
		{
			frame1 = null;
			frame2 = null;
			frame3 = null;
		}

		/// <summary>
		/// Tests whether getting file name works.
		/// </summary>
		[Test]
		public void TestGetFileName1 ()
		{
			Assert.IsNull (frame1.GetFileName (),
						   "File name (1)");
		}

		[Test]
		[Category ("LLVMNotWorking")]
		public void TestGetFileName2 ()
		{
#if MOBILE && !DEBUG
			Assert.Ignore ("The .mdb file won't be present inside the app and no file name will be available");
#endif
			Assert.IsNotNull (frame2.GetFileName (), "File name not null");
			Assert.IsTrue (frame2.GetFileName ().Length != 0, "File name not empty");
			Assert.IsTrue (frame2.GetFileName ().EndsWith ("StackFrameTest.cs"),
						   "File name (2) " + frame2.GetFileName () + " ends with StackFrameTest.cs");
		}

		/// <summary>
		/// Tests whether getting file line number works.
		/// </summary>
		[Test]
		[Category ("LLVMNotWorking")]
		public void TestGetFileLineNumber ()
		{
#if MOBILE && !DEBUG
			Assert.Ignore ("The .mdb file won't be present inside the app and no line number will be available");
#endif
			Assert.AreEqual (0,
							 frame1.GetFileLineNumber (),
							 "Line number (1)");

			Assert.AreEqual (138,
							 frame2.GetFileLineNumber (),
							 "Line number (2)");

			Assert.AreEqual (0,
							 frame3.GetFileLineNumber (),
							 "Line number (3)");
		}

		/// <summary>
		/// Tests whether getting file column number works.
		/// </summary>
		[Test]
		[Category ("NotWorking")] // bug #45730 - Column numbers always zero
		public void TestGetFileColumnNumber ()
		{
			Assert.AreEqual (0,
							 frame1.GetFileColumnNumber (),
							 "Column number (1)");

			Assert.AreEqual (4,
							 frame2.GetFileColumnNumber (),
							 "Column number (2)");

			Assert.AreEqual (0,
							 frame3.GetFileColumnNumber (),
							 "Column number (3)");
		}

		/// <summary>
		/// Tests whether getting method associated with frame works.
		/// </summary>
		[Test]
		[Category("StackWalks")]
		[Category("NotWasm")]
		public void TestGetMethod ()
		{
			Assert.IsNotNull (frame1.GetMethod (),
							  "Method not null (1)");

			Assert.AreEqual (this.GetType (),
							 frame1.GetMethod ().DeclaringType,
							 "Class declaring the method (1)");
			Assert.AreEqual ("SetUp",
							 frame1.GetMethod ().Name,
							 "Method name (1)");

			Assert.IsNotNull (frame2.GetMethod (),
							  "Method not null (2)");

			Assert.AreEqual (this.GetType (),
							 frame2.GetMethod ().DeclaringType,
							 "Class declaring the method (2)");
			Assert.AreEqual ("SetUp",
							 frame2.GetMethod ().Name,
							 "Method name (2)");

			Assert.IsNotNull (frame3.GetMethod (),
							  "Method not null (3)");

			Assert.AreEqual (this.GetType (),
							 frame3.GetMethod ().DeclaringType,
							 "Class declaring the method (3)");
			Assert.AreEqual ("SetUp",
							 frame3.GetMethod ().Name,
							 "Method name (3)");
		}
	}

	/// <summary>
	/// Tests the case where StackFrame is created for current method but
	/// skipping some frames.
	/// </summary>
	/// <remarks>
	/// FIXME: Must be compiled with /debug switch. Otherwise some file
	/// information will be incorrect for the following test cases.
	/// What's the best way to do both types of tests with and without
	/// debug information?
	/// </remarks>
	[TestFixture]
	public class StackFrameTest3
	{
		protected StackFrame frame1;
		protected StackFrame frame2;

		[SetUp]
		public void SetUp ()
		{
			// In order to get better test cases with stack traces
			NestedSetUp ();
		}

		private void NestedSetUp ()
		{
			frame1 = new StackFrame (2);
			frame2 = new StackFrame (1, true);
			// Without this access of frame2 on the RHS, none of 
			// the properties or methods seem to return any data ???
			string s = frame2.GetFileName ();
		}

		[TearDown]
		public void TearDown ()
		{
			frame1 = null;
			frame2 = null;
		}

		/// <summary>
		/// Tests whether getting file name works.
		/// </summary>
		[Test]
		[Category ("LLVMNotWorking")]
		public void TestGetFileName ()
		{
#if MOBILE && !DEBUG
			Assert.Ignore ("The .mdb file won't be present inside the app and no file name will be available");
#endif
			Assert.IsNull (frame1.GetFileName (),
						   "File name (1)");

			Assert.IsNotNull (frame2.GetFileName (),
							  "File name (2) should not be null");

			Assert.IsTrue (frame2.GetFileName ().EndsWith ("StackFrameTest.cs"),
						   "File name (2) " + frame2.GetFileName () + " ends with StackFrameTest.cs");
		}

		/// <summary>
		///   Tests whether getting file line number works.
		/// </summary>
		[Test]
		[Category ("LLVMNotWorking")]
		public void TestGetFileLineNumber ()
		{
#if MOBILE && !DEBUG
			Assert.Ignore ("The .mdb file won't be present inside the app and no line number will be available");
#endif
			Assert.AreEqual (0,
							 frame1.GetFileLineNumber (),
							 "Line number (1)");

			Assert.AreEqual (276,
							 frame2.GetFileLineNumber (),
							 "Line number (2)");
		}

		/// <summary>
		/// Tests whether getting file column number works.
		/// </summary>
		[Test]
		[Category ("NotWorking")] // bug #45730 - Column numbers always zero
		public void TestGetFileColumnNumber ()
		{
			Assert.AreEqual (0,
						 frame1.GetFileColumnNumber (),
							 "Column number (1)");

			Assert.AreEqual (4,
							 frame2.GetFileColumnNumber (),
							 "Column number (2)");
		}

		/// <summary>
		/// Test whether GetFrames contains the frames for nested exceptions
		/// </summary>
		[Test]
		[Category("StackWalks")]
		public void GetFramesAndNestedExc ()
		{
			try
			{
				throw new Exception("This is a test");
			}
			catch (Exception e)
			{
				try
				{
					ExceptionDispatchInfo.Capture(e.InnerException ?? e).Throw();
				}
				catch (Exception ee)
				{
					StackTrace st = new StackTrace(ee, true);
					StackFrame[] frames = st.GetFrames();

					//Console.WriteLine("StackFrame.ToString() foreach StackFrame in Stacktrace.GetFrames():");
					//foreach (StackFrame frame in frames)
						//Console.WriteLine(frame);
					//Console.WriteLine("Expecting: Main, Throw, Main");

					Assert.AreEqual (3, frames.Length);
					var wrongFrames = false;
					Assert.AreEqual ("GetFramesAndNestedExc", frames [0].GetMethod ().Name);
					Assert.AreEqual ("Throw", frames [1].GetMethod ().Name);
					Assert.AreEqual ("GetFramesAndNestedExc", frames [2].GetMethod ().Name);
				}
			}
		}

		[Test]
		[Category("NotWasm")]
		[Category("MobileNotWorking")]
		// https://github.com/mono/mono/issues/12688
		public async Task GetFrames_AsyncCalls ()
		{
			await StartAsyncCalls ();
		}

		private async Task StartAsyncCalls ()
		{
			try
			{
				await AsyncMethod1 ();
			}
			catch (Exception exception)
			{
				var stackTrace = new StackTrace (exception, true);
				Assert.AreEqual (25, stackTrace.GetFrames ().Length);
			}
		}

		private async Task<int> AsyncMethod1 ()
		{
			return await AsyncMethod2 ();
		}

		private async Task<int> AsyncMethod2 ()
		{
			return await AsyncMethod3 ();
		}

		private async Task<int> AsyncMethod3 ()
		{
			return await AsyncMethod4 ();
		}

		private async Task<int> AsyncMethod4 ()
		{
			await Task.Delay (10);
			throw new Exception ("Test exception thrown!");
		}

		/// <summary>
		/// Tests whether getting method associated with frame works.
		/// </summary>
		[Test]
		[Category("StackWalks")]
		[Category("NotWasm")]
		public void TestGetMethod ()
		{
			Assert.IsTrue ((frame1.GetMethod () != null), "Method not null (1)");

			Assert.IsTrue ((frame2.GetMethod () != null), "Method not null (2)");

			Assert.AreEqual (this.GetType (),
							 frame2.GetMethod ().DeclaringType,
							 "Class declaring the method (2)");

			Assert.AreEqual ("SetUp",
							 frame2.GetMethod ().Name,
							 "Method name (2)");
		}
	}
}