EvaluateTests.cs 4.1 KB

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