FakeClipboard.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. namespace Terminal.Gui.Drivers;
  2. /// <summary>
  3. /// Implements a fake clipboard for testing purposes.
  4. /// </summary>
  5. public class FakeClipboard : ClipboardBase
  6. {
  7. /// <summary>
  8. /// Gets or sets an exception to be thrown by clipboard operations.
  9. /// </summary>
  10. public Exception? FakeException { get; set; }
  11. private readonly bool _isSupportedAlwaysFalse;
  12. private string _contents = string.Empty;
  13. /// <summary>
  14. /// Constructs a new instance of <see cref="FakeClipboard"/>.
  15. /// </summary>
  16. /// <param name="fakeClipboardThrowsNotSupportedException"></param>
  17. /// <param name="isSupportedAlwaysFalse"></param>
  18. public FakeClipboard (
  19. bool fakeClipboardThrowsNotSupportedException = false,
  20. bool isSupportedAlwaysFalse = false
  21. )
  22. {
  23. _isSupportedAlwaysFalse = isSupportedAlwaysFalse;
  24. if (fakeClipboardThrowsNotSupportedException)
  25. {
  26. FakeException = new NotSupportedException ("Fake clipboard exception");
  27. }
  28. }
  29. /// <inheritdoc />
  30. public override bool IsSupported => !_isSupportedAlwaysFalse;
  31. /// <inheritdoc />
  32. protected override string GetClipboardDataImpl ()
  33. {
  34. if (FakeException is { })
  35. {
  36. throw FakeException;
  37. }
  38. return _contents;
  39. }
  40. /// <inheritdoc />
  41. protected override void SetClipboardDataImpl (string? text)
  42. {
  43. if (FakeException is { })
  44. {
  45. throw FakeException;
  46. }
  47. _contents = text ?? throw new ArgumentNullException (nameof (text));
  48. }
  49. }