EvaluateTests.cs 3.5 KB

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