TestHelpers.cs 2.0 KB

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