Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
greymistcube committed Oct 18, 2023
1 parent 27ccf30 commit ed63d79
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Bencodex.Tests/Misc/ImplicitConversionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public void Binary()
{
var binary = new Binary(new byte[] { 0x01, 0x02, 0x03 });

ImmutableArray<byte> immutable = binary;
ImmutableArray<byte> immutable = (ImmutableArray<byte>)binary;
Assert.Equal(immutable, binary.ByteArray);

byte[] mutable = binary;
byte[] mutable = (byte[])binary;
Assert.Equal(mutable, binary.ToByteArray());
}

Expand Down
37 changes: 36 additions & 1 deletion Bencodex.Tests/Types/BinaryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,41 @@ public void DefaultConstructor()
Assert.Empty(default(Binary).ToByteArray());
}

[Fact]
public void Equality()
{
byte[] b = new byte[] { 0, 1 };
ImmutableArray<byte> i = ImmutableArray.Create(b);
Binary x = new Binary(i);
object ob = (object)b;
object oi = (object)i;
object ox = (object)x;

#pragma warning disable CS1718 // Comparison made to same variable
Assert.True(x == x);
Assert.True(x.Equals(x));
Assert.True(x.Equals(ox));
Assert.True(ox.Equals(x));
Assert.True(ox.Equals(ox));
#pragma warning restore CS1718

// Unlike Integer and Text, implicit conversion is not supported.
Assert.False(b.Equals(x));
Assert.False(i.Equals(x));
Assert.False(x.Equals(b));
Assert.False(x.Equals(i));

Assert.False(b.Equals(ox));
Assert.False(i.Equals(ox));
Assert.False(ox.Equals(b));
Assert.False(ox.Equals(i));

Assert.False(ob.Equals(ox));
Assert.False(oi.Equals(ox));
Assert.False(ox.Equals(ob));
Assert.False(ox.Equals(oi));
}

[Fact]
public void ConstructorTakingImmutableByteArray()
{
Expand Down Expand Up @@ -141,7 +176,7 @@ public void ToByteArray()
}

[Fact]
public void Equality()
public void Equality2()
{
Assert.Equal(_empty, new Binary(new byte[0]));
Assert.Equal<IValue>(_empty, new Binary(new byte[0]));
Expand Down
22 changes: 7 additions & 15 deletions Bencodex/Types/Binary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,21 @@ public Fingerprint Fingerprint
[Obsolete("Deprecated in favour of " + nameof(Inspect) + "() method.")]
public string Inspection => Inspect(true);

public static implicit operator Binary(ImmutableArray<byte> bytes) =>
public static explicit operator Binary(ImmutableArray<byte> bytes) =>
new Binary(bytes);

public static implicit operator ImmutableArray<byte>(Binary binary) =>
public static explicit operator ImmutableArray<byte>(Binary binary) =>
binary.ByteArray;

public static implicit operator Binary(byte[] bytes)
{
return new Binary(bytes);
}
public static explicit operator Binary(byte[] bytes) =>
new Binary(bytes);

public static implicit operator byte[](Binary binary) =>
public static explicit operator byte[](Binary binary) =>
binary.ToByteArray();

public static bool operator ==(Binary left, Binary right)
{
return left.Equals(right);
}
public static bool operator ==(Binary left, Binary right) => left.Equals(right);

public static bool operator !=(Binary left, Binary right)
{
return !left.Equals(right);
}
public static bool operator !=(Binary left, Binary right) => !left.Equals(right);

/// <summary>
/// Creates a new <see cref="Binary"/> instance from a binary turned into
Expand Down

0 comments on commit ed63d79

Please sign in to comment.