2 // MonoTests.System.Diagnostics.StackTraceTest.cs
5 // Alexander Klyubin (klyubin@aqris.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Diagnostics;
33 using System.Reflection;
34 using System.Threading;
35 using NUnit.Framework;
37 namespace MonoTests.System.Diagnostics {
40 public class StackTraceTest {
42 private StackTrace trace;
43 private StackFrame frame;
48 frame = new StackFrame ("dir/someFile", 13, 45);
49 trace = new StackTrace (frame);
53 public void TearDown ()
59 [ExpectedException (typeof (ArgumentOutOfRangeException))]
60 public void StackTrace_Int_Negative ()
66 [ExpectedException (typeof (ArgumentNullException))]
67 public void StackTrace_Exception_Null ()
74 [ExpectedException (typeof (ArgumentNullException))]
75 public void StackTrace_ExceptionBool_Null ()
78 new StackTrace (e, true);
82 [ExpectedException (typeof (ArgumentNullException))]
83 public void StackTrace_ExceptionInt_Null ()
86 new StackTrace (e, 1);
90 [ExpectedException (typeof (ArgumentOutOfRangeException))]
91 public void StackTrace_ExceptionInt_Negative ()
93 new StackTrace (new Exception (), -1);
97 [ExpectedException (typeof (ArgumentNullException))]
98 public void StackTrace_ExceptionIntBool_Null ()
101 new StackTrace (e, 1, true);
105 [ExpectedException (typeof (ArgumentOutOfRangeException))]
106 public void StackTrace_ExceptionIntBool_Negative ()
108 new StackTrace (new Exception (), -1, true);
112 public void StackTrace_StackFrame_Null ()
114 StackFrame sf = null;
115 StackTrace st = new StackTrace (sf);
117 Assert.AreEqual (1, st.FrameCount, "FrameCount");
118 Assert.IsNull (st.GetFrame (0), "Empty Frame");
122 [Ignore ("Not supported in Mono")]
123 public void StackTrace_Thread_Null ()
126 StackTrace st = new StackTrace (t, true);
130 static void EmptyThread ()
136 [ExpectedException (typeof (ThreadStateException))]
137 [Ignore ("Not supported in Mono")]
138 public void StackTrace_Thread_NotSuspended ()
140 Thread t = new Thread (new ThreadStart (EmptyThread));
142 new StackTrace (t, true);
146 [Ignore ("Not supported in Mono")]
147 public void StackTrace_Thread_Suspended ()
149 Thread t = new Thread (new ThreadStart (EmptyThread));
152 new StackTrace (t, true);
156 public void FrameCount ()
158 Assert.AreEqual (1, trace.FrameCount, "Frame count");
162 public void GetFrame_OutOfRange ()
164 Assert.IsNull (trace.GetFrame (-1), "-1");
165 Assert.IsNull (trace.GetFrame (-129), "-129");
166 Assert.IsNull (trace.GetFrame (1), "1");
167 Assert.IsNull (trace.GetFrame (145), "145");
169 Assert.IsNull (trace.GetFrame (Int32.MinValue), "MinValue");
170 Assert.IsNull (trace.GetFrame (Int32.MaxValue), "MaxValue");
174 public void GetFrame ()
176 Assert.AreEqual (frame, trace.GetFrame (0), "0");
179 public void GetFrames ()
181 StackTrace st = new StackTrace ();
182 StackFrame[] sf = st.GetFrames ();
183 Assert.AreEqual (st.FrameCount, sf.Length, "Count");
184 for (int i=0; i < sf.Length; i++) {
185 Assert.AreEqual (sf [i], st.GetFrame (i), i.ToString ());
189 public void UnthrownException ()
191 Assert.AreEqual (0, new StackTrace (new Exception ()).FrameCount, "Unthrown exception");