StackFrameTest.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //
  2. // MonoTests.System.Diagnostics.StackFrameTest.cs
  3. //
  4. // Author:
  5. // Alexander Klyubin ([email protected])
  6. //
  7. // (C) 2001
  8. //
  9. using System;
  10. using System.Diagnostics;
  11. using System.Reflection;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Diagnostics {
  14. /// <summary>
  15. /// Tests the case where StackFrame is created for specified file name and
  16. /// location inside it.
  17. /// </summary>
  18. [TestFixture]
  19. public class StackFrameTest1 : TestCase {
  20. private StackFrame frame1;
  21. private StackFrame frame2;
  22. protected override void SetUp() {
  23. frame1 = new StackFrame("dir/someFile", 13, 45);
  24. frame2 = new StackFrame("SomeFile2.cs", 24);
  25. }
  26. protected override void TearDown() {
  27. frame1 = null;
  28. frame2 = null;
  29. }
  30. /// <summary>
  31. /// Tests whether getting file name works.
  32. /// </summary>
  33. public void TestGetFileName() {
  34. AssertEquals("File name (1)",
  35. "dir/someFile",
  36. frame1.GetFileName());
  37. AssertEquals("File name (2)",
  38. "SomeFile2.cs",
  39. frame2.GetFileName());
  40. }
  41. /// <summary>
  42. /// Tests whether getting file line number works.
  43. /// </summary>
  44. public void TestGetFileLineNumber() {
  45. AssertEquals("Line number (1)",
  46. 13,
  47. frame1.GetFileLineNumber());
  48. AssertEquals("Line number (2)",
  49. 24,
  50. frame2.GetFileLineNumber());
  51. }
  52. /// <summary>
  53. /// Tests whether getting file column number works.
  54. /// </summary>
  55. public void TestGetFileColumnNumber() {
  56. AssertEquals("Column number (1)",
  57. 45,
  58. frame1.GetFileColumnNumber());
  59. AssertEquals("Column number (2)",
  60. 0,
  61. frame2.GetFileColumnNumber());
  62. }
  63. /// <summary>
  64. /// Tests whether getting method associated with frame works.
  65. /// </summary>
  66. public void TestGetMethod() {
  67. Assert("Method not null (1)", (frame1.GetMethod() != null));
  68. AssertEquals("Class declaring the method (1)",
  69. this.GetType(),
  70. frame1.GetMethod().DeclaringType);
  71. AssertEquals("Method name (1)",
  72. "SetUp",
  73. frame1.GetMethod().Name);
  74. Assert("Method not null (2)", (frame2.GetMethod() != null));
  75. AssertEquals("Class declaring the method (2)",
  76. this.GetType(),
  77. frame2.GetMethod().DeclaringType);
  78. AssertEquals("Method name (2)",
  79. "SetUp",
  80. frame2.GetMethod().Name);
  81. }
  82. }
  83. /// <summary>
  84. /// Tests the case where StackFrame is created for current method.
  85. /// </summary>
  86. /// <remarks>
  87. /// FIXME: Must be compiled with /debug switch. Otherwise some file
  88. /// information will be incorrect for the following test cases.
  89. /// What's the best way to do both types of tests with and without
  90. /// debug information?
  91. /// </remarks>
  92. [TestFixture]
  93. public class StackFrameTest2 : TestCase {
  94. private StackFrame frame1;
  95. private StackFrame frame2;
  96. private StackFrame frame3;
  97. protected override void SetUp() {
  98. frame1 = new StackFrame();
  99. frame2 = new StackFrame(true);
  100. frame3 = new StackFrame(0);
  101. }
  102. protected override void TearDown() {
  103. frame1 = null;
  104. frame2 = null;
  105. frame3 = null;
  106. }
  107. /// <summary>
  108. /// Tests whether getting file name works.
  109. /// </summary>
  110. public void TestGetFileName() {
  111. AssertNull("File name (1)",
  112. frame1.GetFileName());
  113. Assert("File name (2) " + frame2.GetFileName()
  114. + " ends with StackFrameTest.cs",
  115. frame2.GetFileName().EndsWith("StackFrameTest.cs"));
  116. }
  117. /// <summary>
  118. /// Tests whether getting file line number works.
  119. /// </summary>
  120. public void TestGetFileLineNumber() {
  121. AssertEquals("Line number (1)",
  122. 0,
  123. frame1.GetFileLineNumber());
  124. AssertEquals("Line number (2)",
  125. 116,
  126. frame2.GetFileLineNumber());
  127. AssertEquals("Line number (3)",
  128. 0,
  129. frame3.GetFileLineNumber());
  130. }
  131. /// <summary>
  132. /// Tests whether getting file column number works.
  133. /// </summary>
  134. public void TestGetFileColumnNumber() {
  135. AssertEquals("Column number (1)",
  136. 0,
  137. frame1.GetFileColumnNumber());
  138. AssertEquals("Column number (2)",
  139. 25,
  140. frame2.GetFileColumnNumber());
  141. AssertEquals("Column number (3)",
  142. 0,
  143. frame3.GetFileColumnNumber());
  144. }
  145. /// <summary>
  146. /// Tests whether getting method associated with frame works.
  147. /// </summary>
  148. public void TestGetMethod() {
  149. Assert("Method not null (1)",
  150. (frame1.GetMethod() != null));
  151. AssertEquals("Class declaring the method (1)",
  152. this.GetType(),
  153. frame1.GetMethod().DeclaringType);
  154. AssertEquals("Method name (1)",
  155. "SetUp",
  156. frame1.GetMethod().Name);
  157. Assert("Method not null (2)",
  158. (frame2.GetMethod() != null));
  159. AssertEquals("Class declaring the method (2)",
  160. this.GetType(),
  161. frame2.GetMethod().DeclaringType);
  162. AssertEquals("Method name (2)",
  163. "SetUp",
  164. frame2.GetMethod().Name);
  165. Assert("Method not null (3)",
  166. (frame3.GetMethod() != null));
  167. AssertEquals("Class declaring the method (3)",
  168. this.GetType(),
  169. frame3.GetMethod().DeclaringType);
  170. AssertEquals("Method name (3)",
  171. "SetUp",
  172. frame3.GetMethod().Name);
  173. }
  174. }
  175. /// <summary>
  176. /// Tests the case where StackFrame is created for current method but
  177. /// skipping some frames.
  178. /// </summary>
  179. /// <remarks>
  180. /// FIXME: Must be compiled with /debug switch. Otherwise some file
  181. /// information will be incorrect for the following test cases.
  182. /// What's the best way to do both types of tests with and without
  183. /// debug information?
  184. /// </remarks>
  185. [TestFixture]
  186. public class StackFrameTest3 : TestCase {
  187. private StackFrame frame1;
  188. private StackFrame frame2;
  189. protected override void SetUp() {
  190. // In order to get better test cases with stack traces
  191. NestedSetUp();
  192. }
  193. private void NestedSetUp() {
  194. frame1 = new StackFrame(2);
  195. frame2 = new StackFrame(1, true);
  196. }
  197. protected override void TearDown() {
  198. frame1 = null;
  199. frame2 = null;
  200. }
  201. /// <summary>
  202. /// Tests whether getting file name works.
  203. /// </summary>
  204. public void TestGetFileName() {
  205. AssertNull("File name (1)",
  206. frame1.GetFileName());
  207. Assert("File name (2) " + frame2.GetFileName()
  208. + " ends with StackFrameTest.cs",
  209. frame2.GetFileName().EndsWith("StackFrameTest.cs"));
  210. }
  211. /// <summary>
  212. /// Tests whether getting file line number works.
  213. /// </summary>
  214. public void TestGetFileLineNumber() {
  215. AssertEquals("Line number (1)",
  216. 0,
  217. frame1.GetFileLineNumber());
  218. AssertEquals("Line number (2)",
  219. 230,
  220. frame2.GetFileLineNumber());
  221. }
  222. /// <summary>
  223. /// Tests whether getting file column number works.
  224. /// </summary>
  225. public void TestGetFileColumnNumber() {
  226. AssertEquals("Column number (1)",
  227. 0,
  228. frame1.GetFileColumnNumber());
  229. AssertEquals("Column number (2)",
  230. 17,
  231. frame2.GetFileColumnNumber());
  232. }
  233. /// <summary>
  234. /// Tests whether getting method associated with frame works.
  235. /// </summary>
  236. public void TestGetMethod() {
  237. Assert("Method not null (1)", (frame1.GetMethod() != null));
  238. AssertEquals("Method name (1)",
  239. "InternalInvoke",
  240. frame1.GetMethod().Name);
  241. Assert("Method not null (2)", (frame2.GetMethod() != null));
  242. AssertEquals("Class declaring the method (2)",
  243. this.GetType(),
  244. frame2.GetMethod().DeclaringType);
  245. AssertEquals("Method name (2)",
  246. "SetUp",
  247. frame2.GetMethod().Name);
  248. }
  249. }
  250. }