ScopeTests.cs 16 KB

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