Introducing ArrayListTest
[mono.git] / mcs / class / corlib / Test / System.Diagnostics / StackFrameTest.cs
1 //
2 // Ximian.Mono.Tests.StackFrameTest.cs
3 //
4 // Author:
5 //      Alexander Klyubin (klyubin@aqris.com)
6 //
7 // (C) 2001
8 //
9
10 using System;
11 using System.Diagnostics;
12 using System.Reflection;
13 using NUnit.Framework;
14
15 namespace MonoTests.System.Diagnostics {
16         public class StackFrameTest {
17                 private StackFrameTest() {}
18                 public static ITest Suite 
19                 { 
20                         get 
21                         {
22                                 TestSuite suite =  new TestSuite();
23                                 suite.AddTest(StackFrameTest1.Suite);
24                                 suite.AddTest(StackFrameTest2.Suite);
25                                 suite.AddTest(StackFrameTest3.Suite);
26                                 return suite;
27                         }
28                 }
29
30         /// <summary>
31         ///   Tests the case where StackFrame is created for specified file name and
32         ///   location inside it.
33         /// </summary>
34         private class StackFrameTest1 : TestCase {
35                 public StackFrameTest1(string name) : base(name) {}
36                 
37                 private StackFrame frame1;
38                 private StackFrame frame2;
39                 
40                 internal static ITest Suite 
41                 { 
42                         get 
43                         {
44                                 return  new TestSuite(typeof(StackFrameTest1));
45                         }
46                 }
47                 
48                 protected override void SetUp() {
49                         frame1 = new StackFrame("dir/someFile", 13, 45);
50                         frame2 = new StackFrame("SomeFile2.cs", 24);
51                 }
52                 
53                 protected override void TearDown() {
54                         frame1 = null;
55                         frame2 = null;
56                 }
57                 
58                 
59                 
60                 /// <summary>
61                 ///   Tests whether getting file name works.
62                 /// </summary>
63                 public void TestGetFileName() {
64                         AssertEquals("File name (1)",
65                                      "dir/someFile",
66                                      frame1.GetFileName());
67                                      
68                         AssertEquals("File name (2)",
69                                      "SomeFile2.cs",
70                                      frame2.GetFileName());
71                 }
72                 
73                 /// <summary>
74                 ///   Tests whether getting file line number works.
75                 /// </summary>
76                 public void TestGetFileLineNumber() {
77                         AssertEquals("Line number (1)",
78                                      13,
79                                      frame1.GetFileLineNumber());
80                                      
81                         AssertEquals("Line number (2)",
82                                      24,
83                                      frame2.GetFileLineNumber());
84                 }
85                 
86                 /// <summary>
87                 ///   Tests whether getting file column number works.
88                 /// </summary>
89                 public void TestGetFileColumnNumber() {
90                         AssertEquals("Column number (1)",
91                                      45,
92                                      frame1.GetFileColumnNumber());
93                                      
94                         AssertEquals("Column number (2)",
95                                      0,
96                                      frame2.GetFileColumnNumber());
97                 }
98                 
99                                 
100                 /// <summary>
101                 ///   Tests whether getting method associated with frame works.
102                 /// </summary>
103                 public void TestGetMethod() {
104                         Assert("Method not null (1)", (frame1.GetMethod() != null));
105
106                         AssertEquals("Class declaring the method (1)",
107                                      this.GetType(),
108                                      frame1.GetMethod().DeclaringType);
109                         AssertEquals("Method name (1)",
110                                      "SetUp",
111                                      frame1.GetMethod().Name);
112                                      
113                         Assert("Method not null (2)", (frame2.GetMethod() != null));
114                         
115                         AssertEquals("Class declaring the method (2)",
116                                      this.GetType(),
117                                      frame2.GetMethod().DeclaringType);
118                         AssertEquals("Method name (2)",
119                                      "SetUp",
120                                      frame2.GetMethod().Name);
121                 }
122         }
123         
124         /// <summary>
125         ///   Tests the case where StackFrame is created for current method.
126         /// </summary>
127         /// <remarks>
128         ///   FIXME: Must be compiled with /debug switch. Otherwise some file
129         ///   information will be incorrect for the following test cases.
130         ///   What's the best way to do both types of tests with and without
131         ///   debug information?
132         /// </remarks>
133         private class StackFrameTest2 : TestCase {
134                 public StackFrameTest2(string name) : base(name) {}
135                 
136                 private StackFrame frame1;
137                 private StackFrame frame2;
138                 private StackFrame frame3;
139                 
140                 internal static ITest Suite 
141                 { 
142                         get 
143                         {
144                                 return  new TestSuite(typeof(StackFrameTest2));
145                         }
146                 }
147                 
148                 protected override void SetUp() {
149                         frame1 = new StackFrame();
150                         frame2 = new StackFrame(true);
151                         frame3 = new StackFrame(0);
152                 }
153                 
154                 protected override void TearDown() {
155                         frame1 = null;
156                         frame2 = null;
157                         frame3 = null;
158                 }
159                 
160                 
161                 
162                 /// <summary>
163                 ///   Tests whether getting file name works.
164                 /// </summary>
165                 public void TestGetFileName() {
166                         AssertNull("File name (1)",
167                                    frame1.GetFileName());
168                                      
169                         Assert("File name (2) " + frame2.GetFileName()
170                                         + " ends with StackFrameTest.cs",
171                                frame2.GetFileName().EndsWith("StackFrameTest.cs"));
172                 }
173                 
174                 /// <summary>
175                 ///   Tests whether getting file line number works.
176                 /// </summary>
177                 public void TestGetFileLineNumber() {
178                         AssertEquals("Line number (1)",
179                                      0,
180                                      frame1.GetFileLineNumber());
181                                      
182                         AssertEquals("Line number (2)",
183                                      154,
184                                      frame2.GetFileLineNumber());
185                                      
186                         AssertEquals("Line number (3)",
187                                      0,
188                                      frame3.GetFileLineNumber());
189                 }
190                 
191                 /// <summary>
192                 ///   Tests whether getting file column number works.
193                 /// </summary>
194                 public void TestGetFileColumnNumber() {
195                         AssertEquals("Column number (1)",
196                                      0,
197                                      frame1.GetFileColumnNumber());
198                                      
199                         AssertEquals("Column number (2)",
200                                      25,
201                                      frame2.GetFileColumnNumber());
202                         
203                         AssertEquals("Column number (3)",
204                                      0,
205                                      frame3.GetFileColumnNumber());
206                 }
207                 
208                                 
209                 /// <summary>
210                 ///   Tests whether getting method associated with frame works.
211                 /// </summary>
212                 public void TestGetMethod() {
213                         Assert("Method not null (1)",
214                                (frame1.GetMethod() != null));
215
216                         AssertEquals("Class declaring the method (1)",
217                                      this.GetType(),
218                                      frame1.GetMethod().DeclaringType);
219                         AssertEquals("Method name (1)",
220                                      "SetUp",
221                                      frame1.GetMethod().Name);
222                                      
223                         Assert("Method not null (2)",
224                                (frame2.GetMethod() != null));
225                         
226                         AssertEquals("Class declaring the method (2)",
227                                      this.GetType(),
228                                      frame2.GetMethod().DeclaringType);
229                         AssertEquals("Method name (2)",
230                                      "SetUp",
231                                      frame2.GetMethod().Name);
232                                      
233                         Assert("Method not null (3)",
234                                (frame3.GetMethod() != null));
235
236                         AssertEquals("Class declaring the method (3)",
237                                      this.GetType(),
238                                      frame3.GetMethod().DeclaringType);
239                         AssertEquals("Method name (3)",
240                                      "SetUp",
241                                      frame3.GetMethod().Name);
242                 }
243         }
244         
245         
246         /// <summary>
247         ///   Tests the case where StackFrame is created for current method but
248         ///   skipping some frames.
249         /// </summary>
250         /// <remarks>
251         ///   FIXME: Must be compiled with /debug switch. Otherwise some file
252         ///   information will be incorrect for the following test cases.
253         ///   What's the best way to do both types of tests with and without
254         ///   debug information?
255         /// </remarks>
256         private class StackFrameTest3 : TestCase {
257                 public StackFrameTest3(string name) : base(name) {}
258                 
259                 private StackFrame frame1;
260                 private StackFrame frame2;
261                 
262                 internal static ITest Suite 
263                 { 
264                         get 
265                         {
266                                 return  new TestSuite(typeof(StackFrameTest3));
267                         }
268                 }
269                 
270                 protected override void SetUp() {
271                         // In order to get better test cases with stack traces
272                         NestedSetUp();
273                 }
274                 
275                 private void NestedSetUp() {
276                         frame1 = new StackFrame(2);
277                         frame2 = new StackFrame(1, true);
278                 }
279                 
280                 protected override void TearDown() {
281                         frame1 = null;
282                         frame2 = null;
283                 }
284                 
285                 
286                 
287                 /// <summary>
288                 ///   Tests whether getting file name works.
289                 /// </summary>
290                 public void TestGetFileName() {
291                         AssertNull("File name (1)",
292                                    frame1.GetFileName());
293                                      
294                         Assert("File name (2) " + frame2.GetFileName()
295                                         + " ends with StackFrameTest.cs",
296                                frame2.GetFileName().EndsWith("StackFrameTest.cs"));
297                 }
298                 
299                 /// <summary>
300                 ///   Tests whether getting file line number works.
301                 /// </summary>
302                 public void TestGetFileLineNumber() {
303                         AssertEquals("Line number (1)",
304                                      0,
305                                      frame1.GetFileLineNumber());
306                                      
307                         AssertEquals("Line number (2)",
308                                      277,
309                                      frame2.GetFileLineNumber());
310                 }
311                 
312                 /// <summary>
313                 ///   Tests whether getting file column number works.
314                 /// </summary>
315                 public void TestGetFileColumnNumber() {
316                         AssertEquals("Column number (1)",
317                                      0,
318                                      frame1.GetFileColumnNumber());
319                                      
320                         AssertEquals("Column number (2)",
321                                      17,
322                                      frame2.GetFileColumnNumber());
323                 }
324                 
325                                 
326                 /// <summary>
327                 ///   Tests whether getting method associated with frame works.
328                 /// </summary>
329                 public void TestGetMethod() {
330                         Assert("Method not null (1)", (frame1.GetMethod() != null));
331
332                         AssertEquals("Class declaring the method (1)",
333                                      typeof(NUnit.Framework.TestCase),
334                                      frame1.GetMethod().DeclaringType);
335                         AssertEquals("Method name (1)",
336                                      "RunBare",
337                                      frame1.GetMethod().Name);
338                                      
339                         Assert("Method not null (2)", (frame2.GetMethod() != null));
340                         
341                         AssertEquals("Class declaring the method (2)",
342                                      this.GetType(),
343                                      frame2.GetMethod().DeclaringType);
344                         AssertEquals("Method name (2)",
345                                      "SetUp",
346                                      frame2.GetMethod().Name);
347                 }
348         }
349         }
350 }