using Jint.Runtime.Debugger;
namespace Jint.Tests.Runtime.Debugger;
public static class TestHelpers
{
public static bool IsLiteral(this Node node, string requiredValue = null)
{
return node switch
{
Directive directive => requiredValue == null || directive.Value == requiredValue,
NonSpecialExpressionStatement expr => requiredValue == null || (expr.Expression is StringLiteral literal && literal.Value == requiredValue),
_ => false
};
}
public static bool ReachedLiteral(this DebugInformation info, string requiredValue)
{
return info.CurrentNode.IsLiteral(requiredValue);
}
///
/// Initializes engine in debugmode and executes script until debugger statement,
/// before calling stepHandler for assertions. Also asserts that a break was triggered.
///
/// Action to initialize and execute scripts
/// Handler for assertions
public static void TestAtBreak(Action initialization, Action breakHandler)
{
var engine = new Engine(options => options
.DebugMode()
.DebuggerStatementHandling(DebuggerStatementHandling.Script)
);
bool didBreak = false;
engine.Debugger.Break += (sender, info) =>
{
didBreak = true;
breakHandler(sender as Engine, info);
return StepMode.None;
};
initialization(engine);
Assert.True(didBreak, "Test script did not break (e.g. didn't reach debugger statement)");
}
///
public static void TestAtBreak(Action initialization, Action breakHandler)
{
TestAtBreak(engine => initialization(engine), (engine, info) => breakHandler(info));
}
///
/// Initializes engine in debugmode and executes script until debugger statement,
/// before calling stepHandler for assertions. Also asserts that a break was triggered.
///
/// Script that is basis for testing
/// Handler for assertions
public static void TestAtBreak(string script, Action breakHandler)
{
TestAtBreak(engine => engine.Execute(script), breakHandler);
}
///
public static void TestAtBreak(string script, Action breakHandler)
{
TestAtBreak(script, (engine, info) => breakHandler(info));
}
}