FakeClipboard.cs 1.6 KB

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