TestHelpers.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Esprima.Ast;
  2. using Jint.Runtime.Debugger;
  3. namespace Jint.Tests.Runtime.Debugger
  4. {
  5. public static class TestHelpers
  6. {
  7. public static bool IsLiteral(this Node node, string requiredValue = null)
  8. {
  9. switch (node)
  10. {
  11. case Directive directive:
  12. return requiredValue == null || directive.Directiv == requiredValue;
  13. case ExpressionStatement expr:
  14. return requiredValue == null || (expr.Expression is Literal literal && literal.StringValue == requiredValue);
  15. }
  16. return false;
  17. }
  18. public static bool ReachedLiteral(this DebugInformation info, string requiredValue)
  19. {
  20. return info.CurrentNode.IsLiteral(requiredValue);
  21. }
  22. /// <summary>
  23. /// Initializes engine in debugmode and executes script until debugger statement,
  24. /// before calling stepHandler for assertions. Also asserts that a break was triggered.
  25. /// </summary>
  26. /// <param name="script">Script that is basis for testing</param>
  27. /// <param name="breakHandler">Handler for assertions</param>
  28. public static void TestAtBreak(string script, Action<Engine, DebugInformation> breakHandler)
  29. {
  30. var engine = new Engine(options => options
  31. .DebugMode()
  32. .DebuggerStatementHandling(DebuggerStatementHandling.Script)
  33. );
  34. bool didBreak = false;
  35. engine.DebugHandler.Break += (sender, info) =>
  36. {
  37. didBreak = true;
  38. breakHandler(sender as Engine, info);
  39. return StepMode.None;
  40. };
  41. engine.Execute(script);
  42. Assert.True(didBreak, "Test script did not break (e.g. didn't reach debugger statement)");
  43. }
  44. /// <inheritdoc cref="TestAtBreak()"/>
  45. public static void TestAtBreak(string script, Action<DebugInformation> breakHandler)
  46. {
  47. TestAtBreak(script, (engine, info) => breakHandler(info));
  48. }
  49. }
  50. }