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