2
0

ScopeTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. using Jint.Native;
  2. using Jint.Runtime.Debugger;
  3. using System.Linq;
  4. using Xunit;
  5. namespace Jint.Tests.Runtime.Debugger
  6. {
  7. public class ScopeTests
  8. {
  9. private static JsValue AssertOnlyScopeContains(DebugScopes scopes, string name, DebugScopeType scopeType)
  10. {
  11. var containingScope = Assert.Single(scopes, s => s.ScopeType == scopeType && s.BindingNames.Contains(name));
  12. Assert.DoesNotContain(scopes, s => s != containingScope && s.BindingNames.Contains(name));
  13. return containingScope.GetBindingValue(name);
  14. }
  15. private static void AssertScope(DebugScope actual, DebugScopeType expectedType, params string[] expectedBindingNames)
  16. {
  17. Assert.Equal(expectedType, actual.ScopeType);
  18. // Global scope will have a number of intrinsic bindings that are outside the scope [no pun] of these tests
  19. if (actual.ScopeType != DebugScopeType.Global)
  20. {
  21. Assert.Equal(expectedBindingNames.Length, actual.BindingNames.Count);
  22. }
  23. foreach (string expectedName in expectedBindingNames)
  24. {
  25. Assert.Contains(expectedName, actual.BindingNames);
  26. }
  27. }
  28. [Fact]
  29. public void GlobalScopeIncludesGlobalConst()
  30. {
  31. string script = @"
  32. const globalConstant = 'test';
  33. debugger;
  34. ";
  35. TestHelpers.TestAtBreak(script, info =>
  36. {
  37. var value = AssertOnlyScopeContains(info.CurrentScopeChain, "globalConstant", DebugScopeType.Global);
  38. Assert.Equal("test", value.AsString());
  39. });
  40. }
  41. [Fact]
  42. public void GlobalScopeIncludesGlobalLet()
  43. {
  44. string script = @"
  45. let globalLet = 'test';
  46. debugger;";
  47. TestHelpers.TestAtBreak(script, info =>
  48. {
  49. var value = AssertOnlyScopeContains(info.CurrentScopeChain, "globalLet", DebugScopeType.Global);
  50. Assert.Equal("test", value.AsString());
  51. });
  52. }
  53. [Fact]
  54. public void GlobalScopeIncludesGlobalVar()
  55. {
  56. string script = @"
  57. var globalVar = 'test';
  58. debugger;";
  59. TestHelpers.TestAtBreak(script, info =>
  60. {
  61. var value = AssertOnlyScopeContains(info.CurrentScopeChain, "globalVar", DebugScopeType.Global);
  62. Assert.Equal("test", value.AsString());
  63. });
  64. }
  65. [Fact]
  66. public void TopLevelBlockScopeIsIdentified()
  67. {
  68. string script = @"
  69. function test()
  70. {
  71. const localConst = 'test';
  72. debugger;
  73. }
  74. test();";
  75. TestHelpers.TestAtBreak(script, info =>
  76. {
  77. Assert.Equal(3, info.CurrentScopeChain.Count);
  78. Assert.Equal(DebugScopeType.Block, info.CurrentScopeChain[0].ScopeType);
  79. Assert.True(info.CurrentScopeChain[0].IsTopLevel);
  80. });
  81. }
  82. [Fact]
  83. public void NonTopLevelBlockScopeIsIdentified()
  84. {
  85. string script = @"
  86. function test()
  87. {
  88. {
  89. const localConst = 'test';
  90. debugger;
  91. }
  92. }
  93. test();";
  94. TestHelpers.TestAtBreak(script, info =>
  95. {
  96. // We only have 3 scopes, because the function top level block scope is empty.
  97. Assert.Equal(3, info.CurrentScopeChain.Count);
  98. Assert.Equal(DebugScopeType.Block, info.CurrentScopeChain[0].ScopeType);
  99. Assert.False(info.CurrentScopeChain[0].IsTopLevel);
  100. });
  101. }
  102. [Fact]
  103. public void BlockScopeIncludesLocalConst()
  104. {
  105. string script = @"
  106. function test()
  107. {
  108. {
  109. const localConst = 'test';
  110. debugger;
  111. }
  112. }
  113. test();";
  114. TestHelpers.TestAtBreak(script, info =>
  115. {
  116. var value = AssertOnlyScopeContains(info.CurrentScopeChain, "localConst", DebugScopeType.Block);
  117. Assert.Equal("test", value.AsString());
  118. });
  119. }
  120. [Fact]
  121. public void BlockScopeIncludesLocalLet()
  122. {
  123. string script = @"
  124. function test()
  125. {
  126. {
  127. let localLet = 'test';
  128. debugger;
  129. }
  130. }
  131. test();";
  132. TestHelpers.TestAtBreak(script, info =>
  133. {
  134. var value = AssertOnlyScopeContains(info.CurrentScopeChain, "localLet", DebugScopeType.Block);
  135. Assert.Equal("test", value.AsString());
  136. });
  137. }
  138. [Fact]
  139. public void LocalScopeIncludesLocalVar()
  140. {
  141. string script = @"
  142. function test()
  143. {
  144. var localVar = 'test';
  145. debugger;
  146. }
  147. test();";
  148. TestHelpers.TestAtBreak(script, info =>
  149. {
  150. AssertOnlyScopeContains(info.CurrentScopeChain, "localVar", DebugScopeType.Local);
  151. });
  152. }
  153. [Fact]
  154. public void LocalScopeIncludesBlockVar()
  155. {
  156. string script = @"
  157. function test()
  158. {
  159. debugger;
  160. {
  161. var localVar = 'test';
  162. }
  163. }
  164. test();";
  165. TestHelpers.TestAtBreak(script, info =>
  166. {
  167. AssertOnlyScopeContains(info.CurrentScopeChain, "localVar", DebugScopeType.Local);
  168. });
  169. }
  170. [Fact]
  171. public void BlockScopedConstIsVisibleInsideBlock()
  172. {
  173. string script = @"
  174. 'dummy statement';
  175. {
  176. debugger; // const is initialized (as undefined) at beginning of block
  177. const blockConst = 'block';
  178. }";
  179. TestHelpers.TestAtBreak(script, info =>
  180. {
  181. var value = AssertOnlyScopeContains(info.CurrentScopeChain, "blockConst", DebugScopeType.Block);
  182. Assert.Equal(JsUndefined.Undefined, value);
  183. });
  184. }
  185. [Fact]
  186. public void BlockScopedLetIsVisibleInsideBlock()
  187. {
  188. string script = @"
  189. 'dummy statement';
  190. {
  191. let blockLet = 'block';
  192. debugger; // let isn't initialized until declaration
  193. }";
  194. TestHelpers.TestAtBreak(script, info =>
  195. {
  196. AssertOnlyScopeContains(info.CurrentScopeChain, "blockLet", DebugScopeType.Block);
  197. });
  198. }
  199. [Fact]
  200. public void HasCorrectScopeChainForFunction()
  201. {
  202. string script = @"
  203. function add(a, b)
  204. {
  205. debugger;
  206. return a + b;
  207. }
  208. const x = 1;
  209. const y = 2;
  210. const z = add(x, y);";
  211. TestHelpers.TestAtBreak(script, info =>
  212. {
  213. Assert.Collection(info.CurrentScopeChain,
  214. scope => AssertScope(scope, DebugScopeType.Local, "arguments", "a", "b"),
  215. scope => AssertScope(scope, DebugScopeType.Global, "x", "y", "z", "add"));
  216. });
  217. }
  218. [Fact]
  219. public void HasCorrectScopeChainForNestedFunction()
  220. {
  221. string script = @"
  222. function add(a, b)
  223. {
  224. function power(a)
  225. {
  226. debugger;
  227. return a * a;
  228. }
  229. return power(a) + b;
  230. }
  231. const x = 1;
  232. const y = 2;
  233. const z = add(x, y);";
  234. TestHelpers.TestAtBreak(script, info =>
  235. {
  236. Assert.Collection(info.CurrentScopeChain,
  237. scope => AssertScope(scope, DebugScopeType.Local, "arguments", "a"),
  238. scope => AssertScope(scope, DebugScopeType.Closure, "b", "power"), // a, this, arguments shadowed by local
  239. scope => AssertScope(scope, DebugScopeType.Global, "x", "y", "z", "add"));
  240. });
  241. }
  242. [Fact]
  243. public void HasCorrectScopeChainForBlock()
  244. {
  245. string script = @"
  246. function add(a, b)
  247. {
  248. if (a > 0)
  249. {
  250. const y = b / a;
  251. debugger;
  252. }
  253. return a + b;
  254. }
  255. const x = 1;
  256. const y = 2;
  257. const z = add(x, y);";
  258. TestHelpers.TestAtBreak(script, info =>
  259. {
  260. Assert.Collection(info.CurrentScopeChain,
  261. scope => AssertScope(scope, DebugScopeType.Block, "y"),
  262. scope => AssertScope(scope, DebugScopeType.Local, "arguments", "a", "b"),
  263. scope => AssertScope(scope, DebugScopeType.Global, "x", "z", "add")); // y shadowed
  264. });
  265. }
  266. [Fact]
  267. public void HasCorrectScopeChainForNestedBlock()
  268. {
  269. string script = @"
  270. function add(a, b)
  271. {
  272. if (a > 0)
  273. {
  274. const y = b / a;
  275. if (y > 0)
  276. {
  277. const x = b / y;
  278. debugger;
  279. }
  280. }
  281. return a + b;
  282. }
  283. const x = 1;
  284. const y = 2;
  285. const z = add(x, y);";
  286. TestHelpers.TestAtBreak(script, info =>
  287. {
  288. Assert.Collection(info.CurrentScopeChain,
  289. scope => AssertScope(scope, DebugScopeType.Block, "x"),
  290. scope => AssertScope(scope, DebugScopeType.Block, "y"),
  291. scope => AssertScope(scope, DebugScopeType.Local, "arguments", "a", "b"),
  292. scope => AssertScope(scope, DebugScopeType.Global, "z", "add")); // x, y shadowed
  293. });
  294. }
  295. [Fact]
  296. public void HasCorrectScopeChainForCatch()
  297. {
  298. string script = @"
  299. function func()
  300. {
  301. let a = 1;
  302. try
  303. {
  304. throw new Error('test');
  305. }
  306. catch (error)
  307. {
  308. debugger;
  309. }
  310. }
  311. func();";
  312. TestHelpers.TestAtBreak(script, info =>
  313. {
  314. Assert.Collection(info.CurrentScopeChain,
  315. scope => AssertScope(scope, DebugScopeType.Catch, "error"),
  316. scope => AssertScope(scope, DebugScopeType.Block, "a"),
  317. scope => AssertScope(scope, DebugScopeType.Local, "arguments"),
  318. scope => AssertScope(scope, DebugScopeType.Global, "func"));
  319. });
  320. }
  321. [Fact]
  322. public void HasCorrectScopeChainForWith()
  323. {
  324. string script = @"
  325. const obj = { a: 2, b: 4 };
  326. with (obj)
  327. {
  328. const x = a;
  329. debugger;
  330. };";
  331. TestHelpers.TestAtBreak(script, info =>
  332. {
  333. Assert.Collection(info.CurrentScopeChain,
  334. scope => AssertScope(scope, DebugScopeType.Block, "x"),
  335. scope => AssertScope(scope, DebugScopeType.With, "a", "b"),
  336. scope => AssertScope(scope, DebugScopeType.Global, "obj"));
  337. });
  338. }
  339. [Fact]
  340. public void ScopeChainIncludesNonEmptyScopes()
  341. {
  342. string script = @"
  343. const x = 2;
  344. if (x > 0)
  345. {
  346. const y = x;
  347. if (x > 1)
  348. {
  349. const z = x;
  350. debugger;
  351. }
  352. }";
  353. TestHelpers.TestAtBreak(script, info =>
  354. {
  355. Assert.Collection(info.CurrentScopeChain,
  356. scope => AssertScope(scope, DebugScopeType.Block, "z"),
  357. scope => AssertScope(scope, DebugScopeType.Block, "y"),
  358. scope => AssertScope(scope, DebugScopeType.Global, "x"));
  359. });
  360. }
  361. [Fact]
  362. public void ScopeChainExcludesEmptyScopes()
  363. {
  364. string script = @"
  365. const x = 2;
  366. if (x > 0)
  367. {
  368. if (x > 1)
  369. {
  370. const z = x;
  371. debugger;
  372. }
  373. }";
  374. TestHelpers.TestAtBreak(script, info =>
  375. {
  376. Assert.Collection(info.CurrentScopeChain,
  377. scope => AssertScope(scope, DebugScopeType.Block, "z"),
  378. scope => AssertScope(scope, DebugScopeType.Global, "x"));
  379. });
  380. }
  381. [Fact]
  382. public void ResolvesScopeChainsUpTheCallStack()
  383. {
  384. string script = @"
  385. const x = 1;
  386. function foo(a, c)
  387. {
  388. debugger;
  389. }
  390. function bar(b)
  391. {
  392. foo(b, 2);
  393. }
  394. bar(x);";
  395. TestHelpers.TestAtBreak(script, info =>
  396. {
  397. Assert.Collection(info.CallStack,
  398. frame => Assert.Collection(frame.ScopeChain,
  399. // in foo()
  400. scope => AssertScope(scope, DebugScopeType.Local, "arguments", "a", "c"),
  401. scope => AssertScope(scope, DebugScopeType.Global, "x", "foo", "bar")
  402. ),
  403. frame => Assert.Collection(frame.ScopeChain,
  404. // in bar()
  405. scope => AssertScope(scope, DebugScopeType.Local, "arguments", "b"),
  406. scope => AssertScope(scope, DebugScopeType.Global, "x", "foo", "bar")
  407. ),
  408. frame => Assert.Collection(frame.ScopeChain,
  409. // in global
  410. scope => AssertScope(scope, DebugScopeType.Global, "x", "foo", "bar")
  411. )
  412. );
  413. });
  414. }
  415. }
  416. }