-
-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathStreamTests.cs
More file actions
266 lines (225 loc) · 5.26 KB
/
StreamTests.cs
File metadata and controls
266 lines (225 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
public class StreamTests
{
[Fact]
public Task Stream() =>
Verify(
new MemoryStream(
[
1
]));
#pragma warning disable IDE0017
[Fact]
public Task MemoryStreamNotAtStart()
{
var stream = new MemoryStream(
[
1
]);
stream.Position = 1;
return Verify(stream);
}
[Fact]
public Task MemoryStreamWithExtensionNotAtStart()
{
var stream = new MemoryStream(
[
1
]);
stream.Position = 1;
return Verify(stream, extension: "bin");
}
#pragma warning restore IDE0017
class LengthNotSupportedStream(byte[] bytes) : MemoryStream(bytes)
{
public override long Length =>
throw new NotSupportedException();
}
[Fact]
public Task LengthNotSupportedException() =>
Verify(
new LengthNotSupportedStream(
[
1
]));
[Fact]
public Task StreamTask() =>
Verify(
Task.FromResult(
new MemoryStream(
[
1
])));
[Fact]
public Task ByteArray() =>
Verify(
[
1
]);
[Fact]
public Task ByteArrayTask() =>
Verify(
Task.FromResult(
new byte[]
{
1
}));
[Fact]
public Task ByteArrayWithExtension() =>
Verify(
[
1
],
"bin");
[Fact]
public Task StreamMember()
{
var stream = new MemoryStream("value"u8.ToArray());
return Verify(
new
{
stream
});
}
[Fact]
public Task VerifyBytes() =>
Verify(File.ReadAllBytes("sample.jpg"), "jpg");
[Fact]
public async Task EmptyBinary()
{
var exception = await Assert.ThrowsAsync<Exception>(() => Verify([], "bin"));
Assert.StartsWith("Empty data is not allowed. Path: ", exception.InnerException!.Message);
}
[Fact]
public Task NestedByteArray() =>
Verify(
new
{
bytes = new byte[]
{
1
}
});
#region StreamWithExtension
[Fact]
public Task StreamWithExtension()
{
var stream = new MemoryStream(File.ReadAllBytes("sample.png"));
return Verify(stream, "png");
}
#endregion
#region FileStream
[Fact]
public Task FileStream() =>
Verify(File.OpenRead("sample.txt"));
#endregion
[Fact]
public Task FileStreamTask() =>
Verify(Task.FromResult(File.OpenRead("sample.txt")));
[Fact]
public Task StreamNotAtStart()
{
var stream = new MemoryStream(
[
1,
2,
3,
4
])
{
Position = 2
};
return Verify(stream);
}
[Fact]
public Task StreamNotAtStartAsText()
{
var stream = new MemoryStream("foo"u8.ToArray())
{
Position = 2
};
return Verify(stream, "txt");
}
[Fact]
public Task NoLengthStream()
{
var stream = new NoLengthStream(
[
1
]);
return Verify(stream);
}
[Fact]
public Task Streams() =>
Verify(
new List<Stream>
{
new MemoryStream(
[
1
]),
new MemoryStream(
[
2
])
},
"bin");
[Fact]
public Task StreamsWithInfo() =>
Verify(
new List<Stream>
{
new MemoryStream(
[
1
]),
new MemoryStream(
[
2
])
},
"bin",
info: "info");
[Fact]
public Task VerifyBytesAsync() =>
Verify(File.ReadAllBytesAsync("sample.jpg"), "jpg");
#region VerifyFile
[Fact]
public Task VerifyFilePath() =>
VerifyFile("sample.txt");
#endregion
#region VerifyFileExtension
[Fact]
public Task VerifyFilePathWithExtension() =>
VerifyFile("sample.txt", extension: "csv");
#endregion
#if NET6_0_OR_GREATER
[Fact]
public async Task VerifyFileNotLocked()
{
await VerifyFile("sampleNotLocked.txt");
Assert.False(FileEx.IsFileLocked("sampleNotLocked.txt"));
}
#endif
#region VerifyFileWithInfo
[Fact]
public Task VerifyFileWithInfo() =>
VerifyFile(
"sample.txt",
info: "the info");
#endregion
[Fact]
public Task VerifyFileWithAppend() =>
VerifyFile("sample.txt")
.AppendValue("key", "value");
[Fact]
public Task OnlyExtension() =>
VerifyFile(".sample");
[Fact]
public async Task OnlyExtensionAppendFile() =>
await VerifyFile("sample.txt")
.AppendFile(".sample");
[Fact]
public async Task OnlyExtensionAppendTextFile() =>
await VerifyFile("sample.txt")
.AppendFile(".txt");
}