EvaluateTests.cs 3.3 KB

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