forked from Krusen/BencodeNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBListTests.cs
More file actions
254 lines (210 loc) · 6.85 KB
/
Copy pathBListTests.cs
File metadata and controls
254 lines (210 loc) · 6.85 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
using System;
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 BListTests
{
[Fact]
public void Add_Null_ThrowsArgumentNullException()
{
var blist = new BList();
Action action = () => blist.Add((IBObject) null);
action.Should().Throw<ArgumentNullException>();
}
[Fact]
public void AddRange_AppendsList()
{
var blist1 = new BList {"item1", "item2"};
var blist2 = new BList {"item3", "item4"};
blist1.AddRange(blist2);
blist1.Should().HaveCount(4);
blist1.Should().ContainInOrder((BString) "item1", (BString) "item2", (BString) "item3", (BString) "item4");
}
[Fact]
public void Indexer_Set_Null_ThrowsArgumentNullException()
{
var blist = new BList {0};
Action action = () => blist[0] = null;
action.Should().Throw<ArgumentNullException>();
}
[Fact]
public void SequenceEqual_SameOrder_AreEqual()
{
var blist1 = new BList
{
"foo",
"bar"
};
var blist2 = new BList
{
"foo",
"bar"
};
blist1.SequenceEqual(blist2).Should().BeTrue();
}
[Fact]
public void SequenceEqual_DifferentOrder_AreNotEqual()
{
var blist1 = new BList
{
"foo",
"bar"
};
var blist2 = new BList
{
"bar",
"foo"
};
blist1.SequenceEqual(blist2).Should().BeFalse();
}
#region Encode
[Fact]
public void CanEncode_Simple()
{
var blist = new BList {"hello world", 987, "foobar"};
var bencode = blist.EncodeAsString();
bencode.Should().Be("l11:hello worldi987e6:foobare");
}
[Fact]
public void CanEncode_EmptyList()
{
var blist = new BList();
var bencode = blist.EncodeAsString();
bencode.Should().Be("le");
}
[Fact]
public void CanEncode_UTF8()
{
var blist = new BList { "æøå äö èéê ñ" };
var bencode = blist.EncodeAsString();
bencode.Should().Be("l21:æøå äö èéê ñe");
}
[Fact]
public void CanEncode_ISO88591()
{
var encoding = Encoding.GetEncoding("ISO-8859-1");
var blist = new BList { new BString("æøå äö èéê ñ", encoding) };
var bencode = blist.EncodeAsString(encoding);
bencode.Should().Be("l12:æøå äö èéê ñe");
}
[Fact]
public void CanEncode_Complex()
{
var blist = new BList
{
"spam",
666,
new BList
{
"foo",
"bar",
123,
new BDictionary
{
{"more spam", "more eggs"}
}
},
"foobar",
new BDictionary
{
{"numbers", new BList {1, 2, 3}}
}
};
var bencode = blist.EncodeAsString();
bencode.Should().Be("l4:spami666el3:foo3:bari123ed9:more spam9:more eggsee6:foobard7:numbersli1ei2ei3eeee");
}
#endregion
#region AsType/AsStrings/AsNumbers
[Fact]
public void AsType_ConvertsToListOfType()
{
var blist = new BList {1, 2, 3};
var bnumbers = blist.AsType<BNumber>();
bnumbers.Should<object>()
.HaveCount(3)
.And.ContainItemsAssignableTo<BNumber>()
.And.ContainInOrder(blist);
}
[Fact]
public void AsType_ContainingWrongType_ThrowsInvalidCastException()
{
var blist = new BList {1, "2", 3};
Action action = () => blist.AsType<BNumber>();
action.Should().Throw<InvalidCastException>();
}
[Fact]
public void AsStrings_ConvertsToListOfStrings()
{
var blist = new BList {"a", "b", "c"};
var strings = blist.AsStrings();
strings.Should().HaveCount(3);
strings.Should().ContainInOrder("a", "b", "c");
}
[Fact]
public void AsStrings_ContainingNonBStringType_ThrowsInvalidCastException()
{
var blist = new BList {"a", "b", 3};
Action action = () => blist.AsStrings();
action.Should().Throw<InvalidCastException>();
}
[Fact]
public void AsNumbers_ConvertsToListOfLongs()
{
var blist = new BList {1, 2, 3};
var numbers = blist.AsNumbers();
numbers.Should().HaveCount(3);
numbers.Should().ContainInOrder(1L, 2L, 3L);
}
[Fact]
public void AsNumbers_ContainingNonBNumberType_ThrowsInvalidCastException()
{
var blist = new BList {1, 2, "3"};
Action action = () => blist.AsNumbers();
action.Should().Throw<InvalidCastException>();
}
#endregion
[Fact]
public void GetSizeInBytes()
{
var blist = new BList{1, 2, "abc"};
blist.GetSizeInBytes().Should().Be(13);
}
[Fact]
public async Task WriteToPipeWriter()
{
var blist = new BList { 1, 2, "abc" };
var (reader, writer) = new Pipe();
blist.EncodeTo(writer);
await writer.FlushAsync();
reader.TryRead(out var readResult);
var result = Encoding.UTF8.GetString(readResult.Buffer.First.Span.ToArray());
result.Should().Be("li1ei2e3:abce");
}
[Fact]
public async Task WriteToPipeWriterAsync()
{
var blist = new BList { 1, 2, "abc" };
var (reader, writer) = new Pipe();
await blist.EncodeToAsync(writer);
reader.TryRead(out var readResult);
var result = Encoding.UTF8.GetString(readResult.Buffer.First.Span.ToArray());
result.Should().Be("li1ei2e3:abce");
}
[Fact]
public async Task WriteToStreamAsync()
{
var blist = new BList { 1, 2, "abc" };
var stream = new MemoryStream();
await blist.EncodeToAsync(stream);
var result = Encoding.UTF8.GetString(stream.ToArray());
result.Should().Be("li1ei2e3:abce");
}
}
}