2
0

EvaluateTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Esprima;
  2. using Jint.Native;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Debugger;
  5. using Xunit;
  6. namespace Jint.Tests.Runtime.Debugger
  7. {
  8. public class EvaluateTests
  9. {
  10. [Fact]
  11. public void EvalutesInCurrentContext()
  12. {
  13. var script = @"
  14. function test(x)
  15. {
  16. x *= 10;
  17. debugger;
  18. }
  19. test(5);
  20. ";
  21. TestHelpers.TestAtBreak(script, (engine, info) =>
  22. {
  23. var evaluated = engine.DebugHandler.Evaluate("x");
  24. Assert.IsType<JsNumber>(evaluated);
  25. Assert.Equal(50, evaluated.AsNumber());
  26. });
  27. }
  28. [Fact]
  29. public void ThrowsIfNoCurrentContext()
  30. {
  31. var engine = new Engine(options => options.DebugMode());
  32. var exception = Assert.Throws<DebugEvaluationException>(() => engine.DebugHandler.Evaluate("let x = 1;"));
  33. Assert.Null(exception.InnerException); // Not a JavaScript or parser exception
  34. }
  35. [Fact]
  36. public void ThrowsOnRuntimeError()
  37. {
  38. var script = @"
  39. function test(x)
  40. {
  41. x *= 10;
  42. debugger;
  43. }
  44. test(5);
  45. ";
  46. TestHelpers.TestAtBreak(script, (engine, info) =>
  47. {
  48. var exception = Assert.Throws<DebugEvaluationException>(() => engine.DebugHandler.Evaluate("y"));
  49. Assert.IsType<JavaScriptException>(exception.InnerException);
  50. });
  51. }
  52. [Fact]
  53. public void ThrowsOnExecutionError()
  54. {
  55. var script = @"
  56. function test(x)
  57. {
  58. x *= 10;
  59. debugger;
  60. }
  61. test(5);
  62. ";
  63. TestHelpers.TestAtBreak(script, (engine, info) =>
  64. {
  65. var exception = Assert.Throws<DebugEvaluationException>(() =>
  66. engine.DebugHandler.Evaluate("this is a syntax error"));
  67. Assert.IsType<ParserException>(exception.InnerException);
  68. });
  69. }
  70. [Fact]
  71. public void RestoresStackAfterEvaluation()
  72. {
  73. var script = @"
  74. function throws()
  75. {
  76. throw new Error('Take this!');
  77. }
  78. function test(x)
  79. {
  80. x *= 10;
  81. debugger;
  82. }
  83. test(5);
  84. ";
  85. TestHelpers.TestAtBreak(script, (engine, info) =>
  86. {
  87. Assert.Equal(1, engine.CallStack.Count);
  88. var frameBefore = engine.CallStack.Stack[0];
  89. Assert.Throws<DebugEvaluationException>(() => engine.DebugHandler.Evaluate("throws()"));
  90. Assert.Equal(1, engine.CallStack.Count);
  91. var frameAfter = engine.CallStack.Stack[0];
  92. // Stack frames and some of their properties are structs - can't check reference equality
  93. // Besides, even if we could, it would be no guarantee. Neither is the following, but it'll do for now.
  94. Assert.Equal(frameBefore.CallingExecutionContext.Function,
  95. frameAfter.CallingExecutionContext.Function);
  96. Assert.Equal(frameBefore.CallingExecutionContext.LexicalEnvironment,
  97. frameAfter.CallingExecutionContext.LexicalEnvironment);
  98. Assert.Equal(frameBefore.CallingExecutionContext.PrivateEnvironment,
  99. frameAfter.CallingExecutionContext.PrivateEnvironment);
  100. Assert.Equal(frameBefore.CallingExecutionContext.VariableEnvironment,
  101. frameAfter.CallingExecutionContext.VariableEnvironment);
  102. Assert.Equal(frameBefore.CallingExecutionContext.Realm, frameAfter.CallingExecutionContext.Realm);
  103. Assert.Equal(frameBefore.Arguments, frameAfter.Arguments);
  104. Assert.Equal(frameBefore.Expression, frameAfter.Expression);
  105. Assert.Equal(frameBefore.Location, frameAfter.Location);
  106. Assert.Equal(frameBefore.Function, frameAfter.Function);
  107. });
  108. }
  109. }
  110. }