|
| 1 | +using System.IO; |
| 2 | +using System.Text; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace GitCredentialManager.Tests; |
| 7 | + |
| 8 | +public class GitStreamReaderTests |
| 9 | +{ |
| 10 | + #region ReadLineAsync |
| 11 | + |
| 12 | + [Fact] |
| 13 | + public async Task GitStreamReader_ReadLineAsync_LF() |
| 14 | + { |
| 15 | + // hello\n |
| 16 | + // world\n |
| 17 | + |
| 18 | + byte[] buffer = Encoding.UTF8.GetBytes("hello\nworld\n"); |
| 19 | + using var stream = new MemoryStream(buffer); |
| 20 | + var reader = new GitStreamReader(stream, Encoding.UTF8); |
| 21 | + |
| 22 | + string actual1 = await reader.ReadLineAsync(); |
| 23 | + string actual2 = await reader.ReadLineAsync(); |
| 24 | + string actual3 = await reader.ReadLineAsync(); |
| 25 | + |
| 26 | + Assert.Equal("hello", actual1); |
| 27 | + Assert.Equal("world", actual2); |
| 28 | + Assert.Null(actual3); |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public async Task GitStreamReader_ReadLineAsync_CR() |
| 33 | + { |
| 34 | + // hello\rworld\r |
| 35 | + |
| 36 | + byte[] buffer = Encoding.UTF8.GetBytes("hello\rworld\r"); |
| 37 | + using var stream = new MemoryStream(buffer); |
| 38 | + var reader = new GitStreamReader(stream, Encoding.UTF8); |
| 39 | + |
| 40 | + string actual1 = await reader.ReadLineAsync(); |
| 41 | + string actual2 = await reader.ReadLineAsync(); |
| 42 | + |
| 43 | + Assert.Equal("hello\rworld\r", actual1); |
| 44 | + Assert.Null(actual2); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public async Task GitStreamReader_ReadLineAsync_CRLF() |
| 49 | + { |
| 50 | + // hello\r\n |
| 51 | + // world\r\n |
| 52 | + |
| 53 | + byte[] buffer = Encoding.UTF8.GetBytes("hello\r\nworld\r\n"); |
| 54 | + using var stream = new MemoryStream(buffer); |
| 55 | + var reader = new GitStreamReader(stream, Encoding.UTF8); |
| 56 | + |
| 57 | + string actual1 = await reader.ReadLineAsync(); |
| 58 | + string actual2 = await reader.ReadLineAsync(); |
| 59 | + string actual3 = await reader.ReadLineAsync(); |
| 60 | + |
| 61 | + Assert.Equal("hello", actual1); |
| 62 | + Assert.Equal("world", actual2); |
| 63 | + Assert.Null(actual3); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public async Task GitStreamReader_ReadLineAsync_Mixed() |
| 68 | + { |
| 69 | + // hello\r\n |
| 70 | + // world\rthis\n |
| 71 | + // is\n |
| 72 | + // a\n |
| 73 | + // \rmixed\rnewline\r\n |
| 74 | + // \n |
| 75 | + // string\n |
| 76 | + |
| 77 | + byte[] buffer = Encoding.UTF8.GetBytes("hello\r\nworld\rthis\nis\na\n\rmixed\rnewline\r\n\nstring\n"); |
| 78 | + using var stream = new MemoryStream(buffer); |
| 79 | + var reader = new GitStreamReader(stream, Encoding.UTF8); |
| 80 | + |
| 81 | + string actual1 = await reader.ReadLineAsync(); |
| 82 | + string actual2 = await reader.ReadLineAsync(); |
| 83 | + string actual3 = await reader.ReadLineAsync(); |
| 84 | + string actual4 = await reader.ReadLineAsync(); |
| 85 | + string actual5 = await reader.ReadLineAsync(); |
| 86 | + string actual6 = await reader.ReadLineAsync(); |
| 87 | + string actual7 = await reader.ReadLineAsync(); |
| 88 | + string actual8 = await reader.ReadLineAsync(); |
| 89 | + |
| 90 | + Assert.Equal("hello", actual1); |
| 91 | + Assert.Equal("world\rthis", actual2); |
| 92 | + Assert.Equal("is", actual3); |
| 93 | + Assert.Equal("a", actual4); |
| 94 | + Assert.Equal("\rmixed\rnewline", actual5); |
| 95 | + Assert.Equal("", actual6); |
| 96 | + Assert.Equal("string", actual7); |
| 97 | + Assert.Null(actual8); |
| 98 | + } |
| 99 | + |
| 100 | + #endregion |
| 101 | + |
| 102 | + #region ReadLine |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void GitStreamReader_ReadLine_LF() |
| 106 | + { |
| 107 | + // hello\n |
| 108 | + // world\n |
| 109 | + |
| 110 | + byte[] buffer = Encoding.UTF8.GetBytes("hello\nworld\n"); |
| 111 | + using var stream = new MemoryStream(buffer); |
| 112 | + var reader = new GitStreamReader(stream, Encoding.UTF8); |
| 113 | + |
| 114 | + string actual1 = reader.ReadLine(); |
| 115 | + string actual2 = reader.ReadLine(); |
| 116 | + string actual3 = reader.ReadLine(); |
| 117 | + |
| 118 | + Assert.Equal("hello", actual1); |
| 119 | + Assert.Equal("world", actual2); |
| 120 | + Assert.Null(actual3); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public void GitStreamReader_ReadLine_CR() |
| 125 | + { |
| 126 | + // hello\rworld\r |
| 127 | + |
| 128 | + byte[] buffer = Encoding.UTF8.GetBytes("hello\rworld\r"); |
| 129 | + using var stream = new MemoryStream(buffer); |
| 130 | + var reader = new GitStreamReader(stream, Encoding.UTF8); |
| 131 | + |
| 132 | + string actual1 = reader.ReadLine(); |
| 133 | + string actual2 = reader.ReadLine(); |
| 134 | + |
| 135 | + Assert.Equal("hello\rworld\r", actual1); |
| 136 | + Assert.Null(actual2); |
| 137 | + } |
| 138 | + |
| 139 | + [Fact] |
| 140 | + public void GitStreamReader_ReadLine_CRLF() |
| 141 | + { |
| 142 | + // hello\r\n |
| 143 | + // world\r\n |
| 144 | + |
| 145 | + byte[] buffer = Encoding.UTF8.GetBytes("hello\r\nworld\r\n"); |
| 146 | + using var stream = new MemoryStream(buffer); |
| 147 | + var reader = new GitStreamReader(stream, Encoding.UTF8); |
| 148 | + |
| 149 | + string actual1 = reader.ReadLine(); |
| 150 | + string actual2 = reader.ReadLine(); |
| 151 | + string actual3 = reader.ReadLine(); |
| 152 | + |
| 153 | + Assert.Equal("hello", actual1); |
| 154 | + Assert.Equal("world", actual2); |
| 155 | + Assert.Null(actual3); |
| 156 | + } |
| 157 | + |
| 158 | + [Fact] |
| 159 | + public void GitStreamReader_ReadLine_Mixed() |
| 160 | + { |
| 161 | + // hello\r\n |
| 162 | + // world\rthis\n |
| 163 | + // is\n |
| 164 | + // a\n |
| 165 | + // \rmixed\rnewline\r\n |
| 166 | + // \n |
| 167 | + // string\n |
| 168 | + |
| 169 | + byte[] buffer = Encoding.UTF8.GetBytes("hello\r\nworld\rthis\nis\na\n\rmixed\rnewline\r\n\nstring\n"); |
| 170 | + using var stream = new MemoryStream(buffer); |
| 171 | + var reader = new GitStreamReader(stream, Encoding.UTF8); |
| 172 | + |
| 173 | + string actual1 = reader.ReadLine(); |
| 174 | + string actual2 = reader.ReadLine(); |
| 175 | + string actual3 = reader.ReadLine(); |
| 176 | + string actual4 = reader.ReadLine(); |
| 177 | + string actual5 = reader.ReadLine(); |
| 178 | + string actual6 = reader.ReadLine(); |
| 179 | + string actual7 = reader.ReadLine(); |
| 180 | + string actual8 = reader.ReadLine(); |
| 181 | + |
| 182 | + Assert.Equal("hello", actual1); |
| 183 | + Assert.Equal("world\rthis", actual2); |
| 184 | + Assert.Equal("is", actual3); |
| 185 | + Assert.Equal("a", actual4); |
| 186 | + Assert.Equal("\rmixed\rnewline", actual5); |
| 187 | + Assert.Equal("", actual6); |
| 188 | + Assert.Equal("string", actual7); |
| 189 | + Assert.Null(actual8); |
| 190 | + } |
| 191 | + |
| 192 | + #endregion |
| 193 | +} |
0 commit comments