TestHelpers.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 Statement statement, string requiredValue = null)
  10. {
  11. switch (statement)
  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.CurrentStatement.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<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(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. /// <summary>
  47. /// Initializes engine in debugmode and executes script until debugger statement,
  48. /// before calling stepHandler for assertions. Also asserts that a break was triggered.
  49. /// </summary>
  50. /// <param name="script">Script that is basis for testing</param>
  51. /// <param name="breakHandler">Handler for assertions</param>
  52. public static void TestAtBreak(string script, Action<Engine, DebugInformation> breakHandler)
  53. {
  54. var engine = new Engine(options => options
  55. .DebugMode()
  56. .DebuggerStatementHandling(DebuggerStatementHandling.Script)
  57. );
  58. bool didBreak = false;
  59. engine.DebugHandler.Break += (sender, info) =>
  60. {
  61. didBreak = true;
  62. breakHandler(engine, info);
  63. return StepMode.None;
  64. };
  65. engine.Execute(script);
  66. Assert.True(didBreak, "Test script did not break (e.g. didn't reach debugger statement)");
  67. }
  68. }
  69. }