Skip to content

Commit 77f695b

Browse files
committed
Add AsNumbers() method
Closes #23
1 parent 1d1122d commit 77f695b

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

BencodeNET.Tests/Objects/BListTests.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ public void AsType_ConvertsToListOfType()
154154
public void AsType_ContainingWrongType_ThrowsInvalidCastException()
155155
{
156156
var blist = new BList {1, "2", 3};
157-
158157
Action action = () => blist.AsType<BNumber>();
159-
160158
action.ShouldThrow<InvalidCastException>();
161159
}
162160

@@ -174,9 +172,25 @@ public void AsStrings_ConvertsToListOfStrings()
174172
public void AsStrings_ContainingNonBStringType_ThrowsInvalidCastException()
175173
{
176174
var blist = new BList {"a", "b", 3};
177-
178175
Action action = () => blist.AsStrings();
176+
action.ShouldThrow<InvalidCastException>();
177+
}
179178

179+
[Fact]
180+
public void AsNumbers_ConvertsToListOfLongs()
181+
{
182+
var blist = new BList {1, 2, 3};
183+
var numbers = blist.AsNumbers();
184+
185+
numbers.Should().HaveCount(3);
186+
numbers.Should().ContainInOrder(1L, 2L, 3L);
187+
}
188+
189+
[Fact]
190+
public void AsNumbers_ContainingNonBNumberType_ThrowsInvalidCastException()
191+
{
192+
var blist = new BList {1, 2, "3"};
193+
Action action = () => blist.AsNumbers();
180194
action.ShouldThrow<InvalidCastException>();
181195
}
182196
}

BencodeNET/Objects/BList.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,9 @@ public T Get<T>(int index) where T : class, IBObject
117117
}
118118

119119
/// <summary>
120-
/// Assumes all elements are <see cref="BString"/> and returns
121-
/// an enumerable of their string representation.
120+
/// Assumes all elements are <see cref="BString"/>
121+
/// and returns an enumerable of their string representation.
122122
/// </summary>
123-
/// <returns></returns>
124123
public IEnumerable<string> AsStrings()
125124
{
126125
return AsStrings(Encoding.UTF8);
@@ -130,13 +129,22 @@ public IEnumerable<string> AsStrings()
130129
/// Assumes all elements are <see cref="BString"/> and returns
131130
/// an enumerable of their string representation using the specified encoding.
132131
/// </summary>
133-
/// <returns></returns>
134132
public IEnumerable<string> AsStrings(Encoding encoding)
135133
{
136134
IList<BString> bstrings = this.AsType<BString>();
137135
return bstrings.Select(x => x.ToString(encoding));
138136
}
139137

138+
/// <summary>
139+
/// Assumes all elements are <see cref="BNumber"/>
140+
/// and returns an enumerable of their <c>long</c> value.
141+
/// </summary>
142+
public IEnumerable<long> AsNumbers()
143+
{
144+
IList<BNumber> bnumbers = this.AsType<BNumber>();
145+
return bnumbers.Select(x => x.Value);
146+
}
147+
140148
/// <summary>
141149
/// Attempts to cast all elements to the specified type.
142150
/// </summary>
@@ -145,7 +153,7 @@ public IEnumerable<string> AsStrings(Encoding encoding)
145153
/// <exception cref="InvalidCastException">
146154
/// An element is not of type <typeparamref name="T"/>.
147155
/// </exception>
148-
public BList<T> AsType<T>() where T : class,IBObject
156+
public BList<T> AsType<T>() where T : class, IBObject
149157
{
150158
try
151159
{

BencodeNET/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "http://json.schemastore.org/project",
3-
"version": "2.2.1-*",
3+
"version": "2.2.2-*",
44
"title": "BencodeNET",
55
"authors": [ "Søren Kruse" ],
66
"description": "A library for encoding and decoding bencode (e.g. torrent files)",

0 commit comments

Comments
 (0)