ExampleContext.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Text.Json;
  2. namespace Terminal.Gui.Examples;
  3. /// <summary>
  4. /// Defines the execution context for running an example application.
  5. /// This context is used to configure how an example should be executed, including driver selection,
  6. /// keystroke injection, timeouts, and metrics collection.
  7. /// </summary>
  8. public class ExampleContext
  9. {
  10. /// <summary>
  11. /// Gets or sets the name of the driver to use (e.g., "FakeDriver", "DotnetDriver").
  12. /// If <see langword="null"/>, the default driver for the platform is used.
  13. /// </summary>
  14. public string? DriverName { get; set; } = null;
  15. /// <summary>
  16. /// Gets or sets the list of key names to inject into the example during execution.
  17. /// Each string should be a valid key name that can be parsed by <see cref="Input.Key.TryParse"/>.
  18. /// </summary>
  19. public List<string> KeysToInject { get; set; } = new ();
  20. /// <summary>
  21. /// Gets or sets the maximum time in milliseconds to allow the example to run before forcibly terminating it.
  22. /// </summary>
  23. public int TimeoutMs { get; set; } = 30000;
  24. /// <summary>
  25. /// Gets or sets the maximum number of iterations to allow before stopping the example.
  26. /// If set to -1, no iteration limit is enforced.
  27. /// </summary>
  28. public int MaxIterations { get; set; } = -1;
  29. /// <summary>
  30. /// Gets or sets a value indicating whether to collect and report performance metrics during execution.
  31. /// </summary>
  32. public bool CollectMetrics { get; set; } = false;
  33. /// <summary>
  34. /// Gets or sets the execution mode for the example.
  35. /// </summary>
  36. public ExecutionMode Mode { get; set; } = ExecutionMode.OutOfProcess;
  37. /// <summary>
  38. /// The name of the environment variable used to pass the serialized <see cref="ExampleContext"/>
  39. /// to example applications.
  40. /// </summary>
  41. public const string EnvironmentVariableName = "TERMGUI_TEST_CONTEXT";
  42. /// <summary>
  43. /// Serializes this context to a JSON string for passing via environment variables.
  44. /// </summary>
  45. /// <returns>A JSON string representation of this context.</returns>
  46. public string ToJson ()
  47. {
  48. return JsonSerializer.Serialize (this);
  49. }
  50. /// <summary>
  51. /// Deserializes a <see cref="ExampleContext"/> from a JSON string.
  52. /// </summary>
  53. /// <param name="json">The JSON string to deserialize.</param>
  54. /// <returns>The deserialized context, or <see langword="null"/> if deserialization fails.</returns>
  55. public static ExampleContext? FromJson (string json)
  56. {
  57. try
  58. {
  59. return JsonSerializer.Deserialize<ExampleContext> (json);
  60. }
  61. catch
  62. {
  63. return null;
  64. }
  65. }
  66. }