|
| 1 | +using Syndiesis.Utilities; |
| 2 | + |
| 3 | +namespace Syndiesis.Tests; |
| 4 | + |
| 5 | +public sealed class RightSideBufferWriterTests |
| 6 | +{ |
| 7 | + [Test] |
| 8 | + public async Task UseCase0() |
| 9 | + { |
| 10 | + const int capacity = 10; |
| 11 | + Span<char> s = stackalloc char[capacity]; |
| 12 | + var writer = new RightSideBufferWriter<char>(s); |
| 13 | + writer.Write("312"); |
| 14 | + writer.Write('4'); |
| 15 | + writer.Write("01"); |
| 16 | + |
| 17 | + var finalized = writer.GetFinalized(); |
| 18 | + await Assert.That(finalized.ToString()).IsEqualTo("014312"); |
| 19 | + } |
| 20 | + |
| 21 | + [Test] |
| 22 | + public async Task TestOverflowWithChar() |
| 23 | + { |
| 24 | + const int capacity = 1; |
| 25 | + Span<char> s = stackalloc char[capacity]; |
| 26 | + var writer = new RightSideBufferWriter<char>(s); |
| 27 | + writer.Write('4'); |
| 28 | + |
| 29 | + // Can't use writer inside a lambda; hence the manual try block |
| 30 | + try |
| 31 | + { |
| 32 | + writer.Write('0'); |
| 33 | + Assert.Fail("Expected an exception"); |
| 34 | + } |
| 35 | + catch { } |
| 36 | + |
| 37 | + var finalized = writer.GetFinalized(); |
| 38 | + await Assert.That(finalized.ToString()).IsEqualTo("4"); |
| 39 | + } |
| 40 | + |
| 41 | + [Test] |
| 42 | + public async Task TestOverflowWithString0() |
| 43 | + { |
| 44 | + const int capacity = 1; |
| 45 | + Span<char> s = stackalloc char[capacity]; |
| 46 | + var writer = new RightSideBufferWriter<char>(s); |
| 47 | + writer.Write('4'); |
| 48 | + |
| 49 | + // Can't use writer inside a lambda; hence the manual try block |
| 50 | + try |
| 51 | + { |
| 52 | + writer.Write("0"); |
| 53 | + Assert.Fail("Expected an exception"); |
| 54 | + } |
| 55 | + catch { } |
| 56 | + |
| 57 | + try |
| 58 | + { |
| 59 | + writer.Write("041"); |
| 60 | + Assert.Fail("Expected an exception"); |
| 61 | + } |
| 62 | + catch { } |
| 63 | + |
| 64 | + // Expect no exception with an empty string |
| 65 | + writer.Write(string.Empty); |
| 66 | + |
| 67 | + var finalized = writer.GetFinalized(); |
| 68 | + await Assert.That(finalized.ToString()).IsEqualTo("4"); |
| 69 | + } |
| 70 | + |
| 71 | + [Test] |
| 72 | + public async Task TestOverflowWithString1() |
| 73 | + { |
| 74 | + const int capacity = 2; |
| 75 | + Span<char> s = stackalloc char[capacity]; |
| 76 | + var writer = new RightSideBufferWriter<char>(s); |
| 77 | + writer.Write('4'); |
| 78 | + |
| 79 | + try |
| 80 | + { |
| 81 | + writer.Write("041"); |
| 82 | + Assert.Fail("Expected an exception"); |
| 83 | + } |
| 84 | + catch { } |
| 85 | + |
| 86 | + try |
| 87 | + { |
| 88 | + writer.Write("01"); |
| 89 | + Assert.Fail("Expected an exception"); |
| 90 | + } |
| 91 | + catch { } |
| 92 | + |
| 93 | + // Expect no exception with an empty string |
| 94 | + writer.Write(string.Empty); |
| 95 | + |
| 96 | + // Expect the buffer to have one more char of remaining space |
| 97 | + writer.Write("0"); |
| 98 | + |
| 99 | + // Now the buffer should be full |
| 100 | + try |
| 101 | + { |
| 102 | + writer.Write("0"); |
| 103 | + Assert.Fail("Expected an exception"); |
| 104 | + } |
| 105 | + catch { } |
| 106 | + |
| 107 | + var finalized = writer.GetFinalized(); |
| 108 | + await Assert.That(finalized.ToString()).IsEqualTo("04"); |
| 109 | + } |
| 110 | + |
| 111 | + [Test] |
| 112 | + public async Task TestEmpty() |
| 113 | + { |
| 114 | + Span<char> s = []; |
| 115 | + var writer = new RightSideBufferWriter<char>(s); |
| 116 | + var finalized = writer.GetFinalized(); |
| 117 | + await Assert.That(finalized.ToString()).IsEqualTo(string.Empty); |
| 118 | + } |
| 119 | +} |
0 commit comments