// // MonoTests.System.Diagnostics.StackFrameTest.cs // // Author: // Alexander Klyubin (klyubin@aqris.com) // // (C) 2001 // using System; using System.Diagnostics; using System.Reflection; using NUnit.Framework; namespace MonoTests.System.Diagnostics { /// /// Tests the case where StackFrame is created for specified file name and /// location inside it. /// [TestFixture] public class StackFrameTest1 { private StackFrame frame1; private StackFrame frame2; [SetUp] public void SetUp () { frame1 = new StackFrame ("dir/someFile", 13, 45); frame2 = new StackFrame ("SomeFile2.cs", 24); } [TearDown] public void TearDown () { frame1 = null; frame2 = null; } /// /// Tests whether getting file name works. /// [Test] [Category("LLVMNotWorking")] public void TestGetFileName () { Assert.AreEqual ("dir/someFile", frame1.GetFileName (), "File name (1)"); Assert.AreEqual ("SomeFile2.cs", frame2.GetFileName (), "File name (2)"); } /// /// Tests whether getting file line number works. /// [Test] [Category("LLVMNotWorking")] public void TestGetFileLineNumber () { Assert.AreEqual (13, frame1.GetFileLineNumber (), "Line number (1)"); Assert.AreEqual (24, frame2.GetFileLineNumber (), "Line number (2)"); } /// /// Tests whether getting file column number works. /// [Test] public void TestGetFileColumnNumber () { Assert.AreEqual (45, frame1.GetFileColumnNumber (), "Column number (1)"); Assert.AreEqual (0, frame2.GetFileColumnNumber (), "Column number (2)"); } /// /// Tests whether getting method associated with frame works. /// [Test] public void TestGetMethod () { Assert.IsTrue ((frame1.GetMethod () != null), "Method not null (1)"); Assert.AreEqual (this.GetType (), frame1.GetMethod ().DeclaringType, "Class declaring the method (1)"); Assert.AreEqual ("SetUp", frame1.GetMethod ().Name, "Method name (1)"); Assert.IsTrue ((frame2.GetMethod () != null), "Method not null (2)"); Assert.AreEqual (this.GetType (), frame2.GetMethod ().DeclaringType, "Class declaring the method (2)"); Assert.AreEqual ("SetUp", frame2.GetMethod ().Name, "Method name (2)"); } } /// /// Tests the case where StackFrame is created for current method. /// /// /// FIXME: Must be compiled with /debug switch. Otherwise some file /// information will be incorrect for the following test cases. /// What's the best way to do both types of tests with and without /// debug information? /// [TestFixture] public class StackFrameTest2 { private StackFrame frame1; private StackFrame frame2; private StackFrame frame3; [SetUp] public void SetUp () { frame1 = new StackFrame (); frame2 = new StackFrame (true); frame3 = new StackFrame (0); } [TearDown] public void TearDown () { frame1 = null; frame2 = null; frame3 = null; } /// /// Tests whether getting file name works. /// [Test] public void TestGetFileName1 () { Assert.IsNull (frame1.GetFileName (), "File name (1)"); } [Test] public void TestGetFileName2 () { Assert.IsNotNull (frame2.GetFileName (), "File name not null"); Assert.IsTrue (frame2.GetFileName ().Length != 0, "File name not empty"); Assert.IsTrue (frame2.GetFileName ().EndsWith ("StackFrameTest.cs"), "File name (2) " + frame2.GetFileName () + " ends with StackFrameTest.cs"); } /// /// Tests whether getting file line number works. /// [Test] public void TestGetFileLineNumber () { Assert.AreEqual (0, frame1.GetFileLineNumber (), "Line number (1)"); Assert.AreEqual (134, frame2.GetFileLineNumber (), "Line number (2)"); Assert.AreEqual (0, frame3.GetFileLineNumber (), "Line number (3)"); } /// /// Tests whether getting file column number works. /// [Test] [Category ("NotWorking")] // bug #45730 - Column numbers always zero public void TestGetFileColumnNumber () { Assert.AreEqual (0, frame1.GetFileColumnNumber (), "Column number (1)"); Assert.AreEqual (4, frame2.GetFileColumnNumber (), "Column number (2)"); Assert.AreEqual (0, frame3.GetFileColumnNumber (), "Column number (3)"); } /// /// Tests whether getting method associated with frame works. /// [Test] public void TestGetMethod () { Assert.IsNotNull (frame1.GetMethod (), "Method not null (1)"); Assert.AreEqual (this.GetType (), frame1.GetMethod ().DeclaringType, "Class declaring the method (1)"); Assert.AreEqual ("SetUp", frame1.GetMethod ().Name, "Method name (1)"); Assert.IsNotNull (frame2.GetMethod (), "Method not null (2)"); Assert.AreEqual (this.GetType (), frame2.GetMethod ().DeclaringType, "Class declaring the method (2)"); Assert.AreEqual ("SetUp", frame2.GetMethod ().Name, "Method name (2)"); Assert.IsNotNull (frame3.GetMethod (), "Method not null (3)"); Assert.AreEqual (this.GetType (), frame3.GetMethod ().DeclaringType, "Class declaring the method (3)"); Assert.AreEqual ("SetUp", frame3.GetMethod ().Name, "Method name (3)"); } } /// /// Tests the case where StackFrame is created for current method but /// skipping some frames. /// /// /// FIXME: Must be compiled with /debug switch. Otherwise some file /// information will be incorrect for the following test cases. /// What's the best way to do both types of tests with and without /// debug information? /// [TestFixture] public class StackFrameTest3 { protected StackFrame frame1; protected StackFrame frame2; [SetUp] public void SetUp () { // In order to get better test cases with stack traces NestedSetUp (); } private void NestedSetUp () { frame1 = new StackFrame (2); frame2 = new StackFrame (1, true); // Without this access of frame2 on the RHS, none of // the properties or methods seem to return any data ??? string s = frame2.GetFileName (); } [TearDown] public void TearDown () { frame1 = null; frame2 = null; } /// /// Tests whether getting file name works. /// [Test] public void TestGetFileName () { Assert.IsNull (frame1.GetFileName (), "File name (1)"); Assert.IsNotNull (frame2.GetFileName (), "File name (2) should not be null"); Assert.IsTrue (frame2.GetFileName ().EndsWith ("StackFrameTest.cs"), "File name (2) " + frame2.GetFileName () + " ends with StackFrameTest.cs"); } /// /// Tests whether getting file line number works. /// [Test] #if ONLY_1_1 [Category ("NotDotNet")] // .NET 1.1 is off by one #endif public void TestGetFileLineNumber () { Assert.AreEqual (0, frame1.GetFileLineNumber (), "Line number (1)"); Assert.AreEqual (262, frame2.GetFileLineNumber (), "Line number (2)"); } /// /// Tests whether getting file column number works. /// [Test] #if ONLY_1_1 [Category ("NotDotNet")] // .NET 1.1 is off by one #endif [Category ("NotWorking")] // bug #45730 - Column numbers always zero public void TestGetFileColumnNumber () { Assert.AreEqual (0, frame1.GetFileColumnNumber (), "Column number (1)"); Assert.AreEqual (4, frame2.GetFileColumnNumber (), "Column number (2)"); } /// /// Tests whether getting method associated with frame works. /// [Test] public void TestGetMethod () { Assert.IsTrue ((frame1.GetMethod () != null), "Method not null (1)"); Assert.IsTrue ((frame2.GetMethod () != null), "Method not null (2)"); Assert.AreEqual (this.GetType (), frame2.GetMethod ().DeclaringType, "Class declaring the method (2)"); Assert.AreEqual ("SetUp", frame2.GetMethod ().Name, "Method name (2)"); } } }