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