ScopeTests.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using Jint.Native;
  2. using Xunit;
  3. namespace Jint.Tests.Runtime.Debugger
  4. {
  5. public class ScopeTests
  6. {
  7. [Fact]
  8. public void GlobalsAndLocalsIncludeGlobalConst()
  9. {
  10. string script = @"
  11. const globalConstant = 'test';
  12. debugger;
  13. ";
  14. TestHelpers.TestAtBreak(script, info =>
  15. {
  16. var variable = Assert.Single(info.Globals, g => g.Key == "globalConstant");
  17. Assert.Equal("test", variable.Value.AsString());
  18. variable = Assert.Single(info.Locals, g => g.Key == "globalConstant");
  19. Assert.Equal("test", variable.Value.AsString());
  20. });
  21. }
  22. [Fact]
  23. public void GlobalsAndLocalsIncludeGlobalLet()
  24. {
  25. string script = @"
  26. let globalLet = 'test';
  27. debugger;";
  28. TestHelpers.TestAtBreak(script, info =>
  29. {
  30. var variable = Assert.Single(info.Globals, g => g.Key == "globalLet");
  31. Assert.Equal("test", variable.Value.AsString());
  32. variable = Assert.Single(info.Locals, g => g.Key == "globalLet");
  33. Assert.Equal("test", variable.Value.AsString());
  34. });
  35. }
  36. [Fact]
  37. public void GlobalsAndLocalsIncludeGlobalVar()
  38. {
  39. string script = @"
  40. var globalVar = 'test';
  41. debugger;";
  42. TestHelpers.TestAtBreak(script, info =>
  43. {
  44. var variable = Assert.Single(info.Globals, g => g.Key == "globalVar");
  45. Assert.Equal("test", variable.Value.AsString());
  46. variable = Assert.Single(info.Locals, g => g.Key == "globalVar");
  47. Assert.Equal("test", variable.Value.AsString());
  48. });
  49. }
  50. [Fact]
  51. public void OnlyLocalsIncludeLocalConst()
  52. {
  53. string script = @"
  54. function test()
  55. {
  56. const localConst = 'test';
  57. debugger;
  58. }
  59. test();";
  60. TestHelpers.TestAtBreak(script, info =>
  61. {
  62. var variable = Assert.Single(info.Locals, g => g.Key == "localConst");
  63. Assert.Equal("test", variable.Value.AsString());
  64. Assert.DoesNotContain(info.Globals, g => g.Key == "localConst");
  65. });
  66. }
  67. [Fact]
  68. public void OnlyLocalsIncludeLocalLet()
  69. {
  70. string script = @"
  71. function test()
  72. {
  73. let localLet = 'test';
  74. debugger;
  75. }
  76. test();";
  77. TestHelpers.TestAtBreak(script, info =>
  78. {
  79. var variable = Assert.Single(info.Locals, g => g.Key == "localLet");
  80. Assert.Equal("test", variable.Value.AsString());
  81. Assert.DoesNotContain(info.Globals, g => g.Key == "localLet");
  82. });
  83. }
  84. [Fact]
  85. public void OnlyLocalsIncludeLocalVar()
  86. {
  87. string script = @"
  88. function test()
  89. {
  90. var localVar = 'test';
  91. debugger;
  92. }
  93. test();";
  94. TestHelpers.TestAtBreak(script, info =>
  95. {
  96. var variable = Assert.Single(info.Locals, g => g.Key == "localVar");
  97. Assert.Equal("test", variable.Value.AsString());
  98. Assert.DoesNotContain(info.Globals, g => g.Key == "localVar");
  99. });
  100. }
  101. [Fact]
  102. public void BlockScopedVariablesAreInvisibleOutsideBlock()
  103. {
  104. string script = @"
  105. debugger;
  106. {
  107. let blockLet = 'block';
  108. const blockConst = 'block';
  109. }";
  110. TestHelpers.TestAtBreak(script, info =>
  111. {
  112. Assert.DoesNotContain(info.Locals, g => g.Key == "blockLet");
  113. Assert.DoesNotContain(info.Locals, g => g.Key == "blockConst");
  114. });
  115. }
  116. [Fact]
  117. public void BlockScopedConstIsVisibleInsideBlock()
  118. {
  119. string script = @"
  120. 'dummy statement';
  121. {
  122. debugger; // const is initialized (as undefined) at beginning of block
  123. const blockConst = 'block';
  124. }";
  125. TestHelpers.TestAtBreak(script, info =>
  126. {
  127. Assert.Single(info.Locals, c => c.Key == "blockConst" && c.Value == JsUndefined.Undefined);
  128. });
  129. }
  130. [Fact]
  131. public void BlockScopedLetIsVisibleInsideBlock()
  132. {
  133. string script = @"
  134. 'dummy statement';
  135. {
  136. let blockLet = 'block';
  137. debugger; // let isn't initialized until declaration
  138. }";
  139. TestHelpers.TestAtBreak(script, info =>
  140. {
  141. Assert.Single(info.Locals, v => v.Key == "blockLet" && v.Value.AsString() == "block");
  142. });
  143. }
  144. }
  145. }