ConsoleInputTests.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace UnitTests.ConsoleDrivers.V2;
  8. public class ConsoleInputTests
  9. {
  10. class FakeInput : ConsoleInput<char>
  11. {
  12. private readonly string [] _reads;
  13. public FakeInput (params string [] reads ) { _reads = reads; }
  14. int iteration = 0;
  15. /// <inheritdoc />
  16. protected override bool Peek ()
  17. {
  18. return iteration < _reads.Length;
  19. }
  20. /// <inheritdoc />
  21. protected override IEnumerable<char> Read ()
  22. {
  23. return _reads [iteration++];
  24. }
  25. }
  26. [Fact]
  27. public void Test_ThrowsIfNotInitialized ()
  28. {
  29. var input = new FakeInput ("Fish");
  30. var ex = Assert.Throws<Exception>(()=>input.Run (new (canceled: true)));
  31. Assert.Equal ("Cannot run input before Initialization", ex.Message);
  32. }
  33. [Fact]
  34. public void Test_Simple ()
  35. {
  36. var input = new FakeInput ("Fish");
  37. var queue = new ConcurrentQueue<char> ();
  38. input.Initialize (queue);
  39. var cts = new CancellationTokenSource ();
  40. cts.CancelAfter (25); // Cancel after 25 milliseconds
  41. CancellationToken token = cts.Token;
  42. Assert.Empty (queue);
  43. input.Run (token);
  44. Assert.Equal ("Fish",new string (queue.ToArray ()));
  45. }
  46. }