TestHelpers.cs 2.1 KB

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