forked from Krusen/BencodeNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBDictionaryTests.cs
More file actions
441 lines (357 loc) · 13.4 KB
/
Copy pathBDictionaryTests.cs
File metadata and controls
441 lines (357 loc) · 13.4 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipelines;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BencodeNET.Objects;
using FluentAssertions;
using Xunit;
namespace BencodeNET.Tests.Objects
{
public class BDictionaryTests
{
[Fact]
public void Add_NullStringValue_ResultsInEmptyBString()
{
var dict = new BDictionary {{"key", (string) null}};
dict.Get<BString>("key").Value.ToArray().Should().BeEmpty();
}
[Fact]
public void Add_NullIBObjectValue_ThrowsArgumentNullException()
{
var dict = new BDictionary();
Action action = () => dict.Add("key", (IBObject)null);
action.Should().Throw<ArgumentNullException>();
}
[Fact]
public void Add_KeyValuePairWithNullValue_ThrowsArgumentException()
{
var dict = new BDictionary();
Action action = () => dict.Add(new KeyValuePair<BString, IBObject>("key", null));
action.Should().Throw<ArgumentException>();
}
[Fact]
public void Indexer_Set_Null_ThrowsArgumentNullException()
{
var dict = new BDictionary {{"key", "value"}};
Action action = () => dict["key"] = null;
action.Should().Throw<ArgumentNullException>();
}
#region MergeWith
[Fact]
public void MergeWith_StringReplacesExistingKey()
{
var dict1 = new BDictionary {{"key", "value"}};
var dict2 = new BDictionary {{"key", "replaced value"}};
dict1.MergeWith(dict2);
dict1.Should().HaveCount(1);
dict1["key"].Should().Be((BString)"replaced value");
}
[Fact]
public void MergeWith_StringWithNewKeyIsAdded()
{
var dict1 = new BDictionary {{"key", "value"}};
var dict2 = new BDictionary {{"another key", "value"}};
dict1.MergeWith(dict2);
dict1.Should().HaveCount(2);
dict1["key"].Should().Be((BString)"value");
dict1["another key"].Should().Be((BString)"value");
}
[Fact]
public void MergeWith_NumberReplacesExistingKey()
{
var dict1 = new BDictionary {{"key", 1}};
var dict2 = new BDictionary {{"key", 42}};
dict1.MergeWith(dict2);
dict1.Should().HaveCount(1);
dict1["key"].Should().Be((BNumber) 42);
}
[Fact]
public void MergeWith_NumberWithNewKeyIsAdded()
{
var dict1 = new BDictionary {{"key", 1}};
var dict2 = new BDictionary {{"another key", 42}};
dict1.MergeWith(dict2);
dict1.Should().HaveCount(2);
dict1["key"].Should().Be((BNumber) 1);
dict1["another key"].Should().Be((BNumber) 42);
}
[Fact]
public void MergeWith_ExistingKeyOptionMerge_ListAppendedToExistingKeyOfSameType()
{
var dict1 = new BDictionary {{"key", new BList {"item1"}}};
var dict2 = new BDictionary {{"key", new BList {"item2", "item3"}}};
dict1.MergeWith(dict2, ExistingKeyAction.Merge);
dict1.Should().HaveCount(1);
dict1["key"].Should().BeOfType<BList>();
dict1.Get<BList>("key").Should()
.HaveCount(3)
.And.ContainInOrder((BString) "item1", (BString) "item2", (BString) "item3");
}
[Fact]
public void MergeWith_ExistingKeyOption_Replace_ListReplacesExistingKeyOfSameType()
{
var dict1 = new BDictionary {{"key", new BList {"item1"}}};
var dict2 = new BDictionary {{"key", new BList {"item2", "item3"}}};
dict1.MergeWith(dict2, ExistingKeyAction.Replace);
dict1.Should().HaveCount(1);
dict1["key"].Should().BeOfType<BList>();
dict1.Get<BList>("key").Should()
.HaveCount(2)
.And.ContainInOrder((BString) "item2", (BString) "item3");
}
[Fact]
public void MergeWith_ExistingKeyOption_Skip_ListIsSkippedForExistingKeyOfSameType()
{
var dict1 = new BDictionary {{"key", new BList {"item1"}}};
var dict2 = new BDictionary {{"key", new BList {"item2", "item3"}}};
dict1.MergeWith(dict2, ExistingKeyAction.Skip);
dict1.Should().HaveCount(1);
dict1["key"].Should().BeOfType<BList>();
dict1.Get<BList>("key").Should()
.HaveCount(1)
.And.ContainInOrder((BString) "item1");
}
[Fact]
public void MergeWith_ListReplacesExistingKeyOfDifferentType()
{
var dict1 = new BDictionary {{"key", "value"}};
var dict2 = new BDictionary {{"key", new BList {"item1", "item2"}}};
dict1.MergeWith(dict2);
dict1.Should().HaveCount(1);
dict1["key"].Should().BeOfType<BList>();
dict1.Get<BList>("key").Should()
.HaveCount(2)
.And.ContainInOrder((BString) "item1", (BString) "item2");
}
[Fact]
public void MergeWith_ListWithNewKeyIsAdded()
{
var list = new BList {1, 2, 3};
var dict1 = new BDictionary {{"key", 1}};
var dict2 = new BDictionary {{"another key", list}};
dict1.MergeWith(dict2);
dict1.Should().HaveCount(2);
dict1["key"].Should().Be((BNumber) 1);
dict1["another key"].Should().Be(list);
}
[Fact]
public void MergeWith_ExistingKeyOption_Merge_DictionaryMergedWithExistingKeyOfSameType()
{
var dict1 = new BDictionary {{"main", new BDictionary {{"key", "value"}}}};
var dict2 = new BDictionary {{"main", new BDictionary {{"key2", "value2"}}}};
dict1.MergeWith(dict2, ExistingKeyAction.Merge);
dict1.Should().HaveCount(1);
dict1.Get<BDictionary>("main").Should().HaveCount(2).And.ContainKeys("key", "key2");
}
[Fact]
public void MergeWith_ExistingKeyOption_Replace_DictionaryReplacesExistingKeyOfSameType()
{
var dict1 = new BDictionary {{"main", new BDictionary {{"key", "value"}}}};
var dict2 = new BDictionary {{"main", new BDictionary {{"key2", "value2"}}}};
dict1.MergeWith(dict2, ExistingKeyAction.Replace);
dict1.Should().HaveCount(1);
dict1.Get<BDictionary>("main").Should().HaveCount(1).And.ContainKey("key2");
}
[Fact]
public void MergeWith_ExistingKeyOption_Skip_DictionarySkippedForExistingKeyOfSameType()
{
var dict1 = new BDictionary {{"main", new BDictionary {{"key", "value"}}}};
var dict2 = new BDictionary {{"main", new BDictionary {{"key2", "value2"}}}};
dict1.MergeWith(dict2, ExistingKeyAction.Skip);
dict1.Should().HaveCount(1);
dict1.Get<BDictionary>("main").Should().HaveCount(1).And.ContainKey("key");
}
[Fact]
public void MergeWith_DictionaryReplacesExistingKeyOfDifferentType()
{
var dict1 = new BDictionary {{"main", new BList {"item1"}}};
var dict2 = new BDictionary {{"main", new BDictionary {{"key", "value"}}}};
dict1.MergeWith(dict2);
dict1.Should().HaveCount(1);
dict1.Get<BDictionary>("main").Should().HaveCount(1).And.ContainKey("key");
}
[Fact]
public void MergeWith_DictionaryWithNewKeyIsAdded()
{
var dict1 = new BDictionary { { "main", new BDictionary { { "key", "value" } } } };
var dict2 = new BDictionary { { "main2", new BDictionary { { "key2", "value2" } } } };
dict1.MergeWith(dict2);
dict1.Should().HaveCount(2);
dict1.Get<BDictionary>("main").Should().HaveCount(1).And.ContainKeys("key");
dict1.Get<BDictionary>("main2").Should().HaveCount(1).And.ContainKeys("key2");
}
#endregion
#region SequenceEqual
[Fact]
public void SequenceEqual_WithKeysAddedInSameOrder_AreEqual()
{
var bdict1 = new BDictionary
{
{"foobar", "Hello World!"},
{"number", 747},
{"key", "value"}
};
var bdict2 = new BDictionary
{
{"foobar", "Hello World!"},
{"number", 747},
{"key", "value"}
};
bdict1.SequenceEqual(bdict2).Should().BeTrue();
}
[Fact]
public void SequenceEqual_WithKeysAddedInDifferentOrder_AreEqual()
{
var bdict1 = new BDictionary
{
{"foobar", "Hello World!"},
{"number", 747},
{"key", "value"}
};
var bdict2 = new BDictionary
{
{"key", "value"},
{"foobar", "Hello World!"},
{"number", 747}
};
bdict1.SequenceEqual(bdict2).Should().BeTrue();
}
[Fact]
public void SequenceEqual_WithDifferentKeys_AreNotEqual()
{
var bdict1 = new BDictionary
{
{"foobar", "Hello World!"},
};
var bdict2 = new BDictionary
{
{"key", "Hello World!"},
};
bdict1.SequenceEqual(bdict2).Should().BeFalse();
}
[Fact]
public void SequenceEqual_WithDifferentValues_AreNotEqual()
{
var bdict1 = new BDictionary
{
{"foobar", "Hello World!"},
};
var bdict2 = new BDictionary
{
{"foobar", "Another world..."},
};
bdict1.SequenceEqual(bdict2).Should().BeFalse();
}
#endregion
#region Encode
[Fact]
public void CanEncode_Simple()
{
var bdict = new BDictionary
{
{"foobar", "Hello World!"},
{"number", 747}
};
var bencode = bdict.EncodeAsString();
bencode.Should().Be("d6:foobar12:Hello World!6:numberi747ee");
}
[Fact]
public void Encode_KeyAreSortedInLexicalOrder()
{
var bdict = new BDictionary
{
{"number", 747},
{"foobar", "Hello World!"},
{"key", "value"}
};
var bencode = bdict.EncodeAsString();
bencode.Should().Be("d6:foobar12:Hello World!3:key5:value6:numberi747ee");
}
[Fact]
public void CanEncode_EmptyDictionary()
{
var bdict = new BDictionary();
var bencode = bdict.EncodeAsString();
bencode.Should().Be("de");
}
[Fact]
public void CanEncode_Complex()
{
var bdict = new BDictionary
{
{"spam", "egg"},
{
"A List", new BList
{
"foo",
"bar",
123,
new BDictionary
{
{"more spam", "more eggs"}
}
}
},
{
"foobar", new BDictionary
{
{"numbers", new BList {1, 2, 3}}
}
}
};
var bencode = bdict.EncodeAsString();
bencode.Should()
.Be("d6:A Listl3:foo3:bari123ed9:more spam9:more eggsee6:foobard7:numbersli1ei2ei3eee4:spam3:egge");
}
#endregion
[Fact]
public void GetSizeInBytes()
{
var bdict = new BDictionary
{
// 6 + 5
{"spam", "egg"},
// 6 + 3 + 3 + 3 + 3 (+ 2)
{ "list", new BList { 1, 2, 3} },
// 5 + 5
{ "str", "abc" },
// 5 + 4
{ "num", 42 }
}; // 2
bdict.GetSizeInBytes().Should().Be(49);
}
[Fact]
public async Task WriteToPipeWriter()
{
var dict = new BDictionary { { "key", "value" } };
var (reader, writer) = new Pipe();
dict.EncodeTo(writer);
await writer.FlushAsync();
reader.TryRead(out var readResult);
var result = Encoding.UTF8.GetString(readResult.Buffer.First.Span.ToArray());
result.Should().Be("d3:key5:valuee");
}
[Fact]
public async Task WriteToPipeWriterAsync()
{
var dict = new BDictionary { { "key", "value" } };
var (reader, writer) = new Pipe();
await dict.EncodeToAsync(writer);
reader.TryRead(out var readResult);
var result = Encoding.UTF8.GetString(readResult.Buffer.First.Span.ToArray());
result.Should().Be("d3:key5:valuee");
}
[Fact]
public async Task WriteToStreamAsync()
{
var dict = new BDictionary { { "key", "value" } };
var stream = new MemoryStream();
await dict.EncodeToAsync(stream);
var result = Encoding.UTF8.GetString(stream.ToArray());
result.Should().Be("d3:key5:valuee");
}
}
}