StackFrameTest.cs 14 KB

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