CallStackTests.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. using Esprima;
  2. using Esprima.Ast;
  3. using Jint.Runtime.Debugger;
  4. using Xunit;
  5. namespace Jint.Tests.Runtime.Debugger
  6. {
  7. public class CallStackTests
  8. {
  9. [Fact]
  10. public void IncludesFunctionNames()
  11. {
  12. var script = @"
  13. function foo()
  14. {
  15. debugger;
  16. }
  17. function bar()
  18. {
  19. foo();
  20. }
  21. bar()";
  22. TestHelpers.TestAtBreak(script, info =>
  23. {
  24. Assert.Collection(info.CallStack,
  25. frame => Assert.Equal("foo", frame.FunctionName),
  26. frame => Assert.Equal("bar", frame.FunctionName),
  27. frame => Assert.Equal("(anonymous)", frame.FunctionName)
  28. );
  29. });
  30. }
  31. [Fact]
  32. public void IncludesLocations()
  33. {
  34. var script = @"
  35. function foo()
  36. {
  37. debugger;
  38. }
  39. function bar()
  40. {
  41. foo();
  42. }
  43. bar()";
  44. TestHelpers.TestAtBreak(script, info =>
  45. {
  46. // The line numbers here may mislead - the positions are, as would be expected,
  47. // at the position before the currently executing line, not the line after.
  48. // Remember that Esprima (and hence Jint) line numbers are 1-based, not 0-based.
  49. Assert.Collection(info.CallStack,
  50. // "debugger;"
  51. frame => Assert.Equal(new Position(4, 0), frame.Location.Start),
  52. // "foo();"
  53. frame => Assert.Equal(new Position(9, 0), frame.Location.Start),
  54. // "bar();"
  55. frame => Assert.Equal(new Position(12, 0), frame.Location.Start)
  56. );
  57. });
  58. }
  59. [Fact]
  60. public void IncludesFunctionLocations()
  61. {
  62. var script = @"
  63. function foo()
  64. {
  65. debugger;
  66. }
  67. function bar()
  68. {
  69. foo();
  70. }
  71. bar()";
  72. TestHelpers.TestAtBreak(script, info =>
  73. {
  74. // Remember that Esprima (and hence Jint) line numbers are 1-based, not 0-based.
  75. Assert.Collection(info.CallStack,
  76. // function foo()
  77. frame => Assert.Equal(new Position(2, 0), frame.FunctionLocation?.Start),
  78. // function bar()
  79. frame => Assert.Equal(new Position(7, 0), frame.FunctionLocation?.Start),
  80. // global - no function location
  81. frame => Assert.Equal(null, frame.FunctionLocation?.Start)
  82. );
  83. });
  84. }
  85. [Fact]
  86. public void HasReturnValue()
  87. {
  88. string script = @"
  89. function foo()
  90. {
  91. return 'result';
  92. }
  93. foo();";
  94. var engine = new Engine(options => options.DebugMode());
  95. bool atReturn = false;
  96. bool didCheckReturn = false;
  97. engine.DebugHandler.Step += (sender, info) =>
  98. {
  99. if (atReturn)
  100. {
  101. Assert.NotNull(info.CurrentCallFrame.ReturnValue);
  102. Assert.Equal("result", info.CurrentCallFrame.ReturnValue.AsString());
  103. didCheckReturn = true;
  104. atReturn = false;
  105. }
  106. if (info.CurrentStatement is ReturnStatement)
  107. {
  108. // Step one further, and we should have the return value
  109. atReturn = true;
  110. }
  111. return StepMode.Into;
  112. };
  113. engine.Execute(script);
  114. Assert.True(didCheckReturn);
  115. }
  116. [Fact]
  117. public void HasThis()
  118. {
  119. string script = @"
  120. function Thing(name)
  121. {
  122. this.name = name;
  123. }
  124. Thing.prototype.test = function()
  125. {
  126. debugger;
  127. }
  128. var car = new Thing('car');
  129. car.test();
  130. ";
  131. TestHelpers.TestAtBreak(script, (engine, info) =>
  132. {
  133. Assert.Collection(info.CallStack,
  134. frame => Assert.Equal(engine.Realm.GlobalObject.Get("car"), frame.This),
  135. frame => Assert.Equal(engine.Realm.GlobalObject, frame.This)
  136. );
  137. });
  138. }
  139. [Fact]
  140. public void NamesRegularFunction()
  141. {
  142. string script = @"
  143. function regularFunction() { debugger; }
  144. regularFunction();";
  145. TestHelpers.TestAtBreak(script, info =>
  146. {
  147. Assert.Equal("regularFunction", info.CurrentCallFrame.FunctionName);
  148. });
  149. }
  150. [Fact]
  151. public void NamesFunctionExpression()
  152. {
  153. string script = @"
  154. const functionExpression = function() { debugger; }
  155. functionExpression()";
  156. TestHelpers.TestAtBreak(script, info =>
  157. {
  158. Assert.Equal("functionExpression", info.CurrentCallFrame.FunctionName);
  159. });
  160. }
  161. [Fact]
  162. public void NamesNamedFunctionExpression()
  163. {
  164. string script = @"
  165. const functionExpression = function namedFunction() { debugger; }
  166. functionExpression()";
  167. TestHelpers.TestAtBreak(script, info =>
  168. {
  169. Assert.Equal("namedFunction", info.CurrentCallFrame.FunctionName);
  170. });
  171. }
  172. [Fact]
  173. public void NamesArrowFunction()
  174. {
  175. string script = @"
  176. const arrowFunction = () => { debugger; }
  177. arrowFunction()";
  178. TestHelpers.TestAtBreak(script, info =>
  179. {
  180. Assert.Equal("arrowFunction", info.CurrentCallFrame.FunctionName);
  181. });
  182. }
  183. [Fact]
  184. public void NamesNewFunction()
  185. {
  186. string script = @"
  187. const newFunction = new Function('debugger;');
  188. newFunction()";
  189. TestHelpers.TestAtBreak(script, info =>
  190. {
  191. // Ideally, this should be "(anonymous)", but FunctionConstructor sets the "anonymous" name.
  192. Assert.Equal("anonymous", info.CurrentCallFrame.FunctionName);
  193. });
  194. }
  195. [Fact]
  196. public void NamesMemberFunction()
  197. {
  198. string script = @"
  199. const obj = { memberFunction() { debugger; } };
  200. obj.memberFunction()";
  201. TestHelpers.TestAtBreak(script, info =>
  202. {
  203. Assert.Equal("memberFunction", info.CurrentCallFrame.FunctionName);
  204. });
  205. }
  206. [Fact]
  207. public void NamesAnonymousFunction()
  208. {
  209. string script = @"
  210. (function()
  211. {
  212. debugger;
  213. }());";
  214. TestHelpers.TestAtBreak(script, info =>
  215. {
  216. Assert.Equal("(anonymous)", info.CurrentCallFrame.FunctionName);
  217. });
  218. }
  219. [Fact]
  220. public void NamesGetAccessor()
  221. {
  222. string script = @"
  223. const obj = {
  224. get accessor()
  225. {
  226. debugger;
  227. return 'test';
  228. }
  229. };
  230. const x = obj.accessor;";
  231. TestHelpers.TestAtBreak(script, info =>
  232. {
  233. Assert.Equal("get accessor", info.CurrentCallFrame.FunctionName);
  234. });
  235. }
  236. [Fact]
  237. public void NamesSetAccessor()
  238. {
  239. string script = @"
  240. const obj = {
  241. set accessor(value)
  242. {
  243. debugger;
  244. this.value = value;
  245. }
  246. };
  247. obj.accessor = 42;";
  248. TestHelpers.TestAtBreak(script, info =>
  249. {
  250. Assert.Equal("set accessor", info.CurrentCallFrame.FunctionName);
  251. });
  252. }
  253. }
  254. }