Skip to content

Commit

Permalink
Add AsNumbers() method
Browse files Browse the repository at this point in the history
Closes #23
  • Loading branch information
Krusen committed Feb 7, 2017
1 parent 1d1122d commit 77f695b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
20 changes: 17 additions & 3 deletions BencodeNET.Tests/Objects/BListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ public void AsType_ConvertsToListOfType()
public void AsType_ContainingWrongType_ThrowsInvalidCastException()
{
var blist = new BList {1, "2", 3};

Action action = () => blist.AsType<BNumber>();

action.ShouldThrow<InvalidCastException>();
}

Expand All @@ -174,9 +172,25 @@ public void AsStrings_ConvertsToListOfStrings()
public void AsStrings_ContainingNonBStringType_ThrowsInvalidCastException()
{
var blist = new BList {"a", "b", 3};

Action action = () => blist.AsStrings();
action.ShouldThrow<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.ShouldThrow<InvalidCastException>();
}
}
Expand Down
18 changes: 13 additions & 5 deletions BencodeNET/Objects/BList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,9 @@ public T Get<T>(int index) where T : class, IBObject
}

/// <summary>
/// Assumes all elements are <see cref="BString"/> and returns
/// an enumerable of their string representation.
/// Assumes all elements are <see cref="BString"/>
/// and returns an enumerable of their string representation.
/// </summary>
/// <returns></returns>
public IEnumerable<string> AsStrings()
{
return AsStrings(Encoding.UTF8);
Expand All @@ -130,13 +129,22 @@ public IEnumerable<string> AsStrings()
/// Assumes all elements are <see cref="BString"/> and returns
/// an enumerable of their string representation using the specified encoding.
/// </summary>
/// <returns></returns>
public IEnumerable<string> AsStrings(Encoding encoding)
{
IList<BString> bstrings = this.AsType<BString>();
return bstrings.Select(x => x.ToString(encoding));
}

/// <summary>
/// Assumes all elements are <see cref="BNumber"/>
/// and returns an enumerable of their <c>long</c> value.
/// </summary>
public IEnumerable<long> AsNumbers()
{
IList<BNumber> bnumbers = this.AsType<BNumber>();
return bnumbers.Select(x => x.Value);
}

/// <summary>
/// Attempts to cast all elements to the specified type.
/// </summary>
Expand All @@ -145,7 +153,7 @@ public IEnumerable<string> AsStrings(Encoding encoding)
/// <exception cref="InvalidCastException">
/// An element is not of type <typeparamref name="T"/>.
/// </exception>
public BList<T> AsType<T>() where T : class,IBObject
public BList<T> AsType<T>() where T : class, IBObject
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion BencodeNET/project.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://json.schemastore.org/project",
"version": "2.2.1-*",
"version": "2.2.2-*",
"title": "BencodeNET",
"authors": [ "Søren Kruse" ],
"description": "A library for encoding and decoding bencode (e.g. torrent files)",
Expand Down

0 comments on commit 77f695b

Please sign in to comment.