Skip to content

Commit

Permalink
Add casting operators for int? and long? (#35)
Browse files Browse the repository at this point in the history
+semver: feature
  • Loading branch information
Carlos authored and Krusen committed Feb 11, 2019
1 parent 177d418 commit 867e120
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
96 changes: 96 additions & 0 deletions BencodeNET.Tests/Objects/BNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,102 @@ public void ToString_WithFormat_SameAsLong()
str1.Should().Be(str2);
}

[Fact]
public void CanCastFromInt()
{
int number = 12345;
var bnumber = (BNumber)number;
bnumber.Should().Be(12345);
}

[Fact]
public void CanCastFromLong()
{
long number = 12345;
var bnumber = (BNumber)number;
bnumber.Should().Be(12345);
}

[Fact]
public void CanCastFromNullableInt_WhenNull()
{
int? number = null;
var bnumber = (BNumber)number;
bnumber.Should().BeNull();
}

[Fact]
public void CanCastFromNullableLong_WhenNull()
{
long? number = null;
var bnumber = (BNumber)number;
bnumber.Should().BeNull();
}

[Fact]
public void CanCastFromNullableInt_WhenNotNull()
{
int? number = 12345;
var bnumber = (BNumber)number;
bnumber.Should().Be(12345);
}

[Fact]
public void CanCastFromNullableLong_WhenNotNull()
{
long? number = 12345;
var bnumber = (BNumber)number;
bnumber.Should().Be(12345);
}

[Fact]
public void CanCastToInt()
{
BNumber bnumber = new BNumber(12345);
int number = (int)bnumber;
number.Should().Be(12345);
}

[Fact]
public void CanCastToLong()
{
BNumber bnumber = new BNumber(12345);
long number = (long)bnumber;
number.Should().Be(12345);
}

[Fact]
public void CanCastToNullableInt_WhenNull()
{
BNumber bnumber = null;
var number = (int?)bnumber;
number.Should().BeNull();
}

[Fact]
public void CanCastToNullableLong_WhenNull()
{
BNumber bnumber = null;
var number = (long?)bnumber;
number.Should().BeNull();
}

[Fact]
public void CanCastToNullableInt_WhenNotNull()
{
BNumber bnumber = 12345;
var number = (int?)bnumber;
number.Should().Be(12345);
}

[Fact]
public void CanCastToNullableLong_WhenNotNull()
{
BNumber bnumber = 12345;
var number = (long?)bnumber;
number.Should().Be(12345);
}

[Fact]
public void CanCastFromDateTime()
{
Expand Down
12 changes: 12 additions & 0 deletions BencodeNET/Objects/BNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ protected override void EncodeObject(BencodeStream stream)
stream.Write(Value);
stream.Write('e');
}

public static implicit operator int?(BNumber bint)
{
if (bint == null) return null;
return (int)bint.Value;
}

public static implicit operator long?(BNumber bint)
{
if (bint == null) return null;
return bint.Value;
}

public static implicit operator int(BNumber bint)
{
Expand Down

0 comments on commit 867e120

Please sign in to comment.