TestHelpers.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.Break += (sender, info) =>
  38. {
  39. didBreak = true;
  40. breakHandler(info);
  41. return StepMode.None;
  42. };
  43. engine.Execute(script);
  44. Assert.True(didBreak);
  45. }
  46. }
  47. }