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