NetworkReader.ToString improved to be more readable: "[B2-C3-D4 @ 0/3]"

This commit is contained in:
vis2k 2022-09-25 13:18:04 +07:00
parent 27398721a2
commit 6fa5a65cd3
2 changed files with 24 additions and 1 deletions

View File

@ -182,7 +182,7 @@ public ArraySegment<byte> ReadBytesSegment(int count)
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public override string ToString() => public override string ToString() =>
$"NetworkReader pos={Position} capacity={Capacity} buffer={BitConverter.ToString(buffer.Array, buffer.Offset, buffer.Count)}"; $"[{buffer.ToHexString()} @ {Position}/{Capacity}]";
/// <summary>Reads any data type that mirror supports. Uses weaver populated Reader(T).read</summary> /// <summary>Reads any data type that mirror supports. Uses weaver populated Reader(T).read</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@ -102,5 +102,28 @@ public void ReadBytesSegment_CountNegative()
ArraySegment<byte> result = reader.ReadBytesSegment(-1); ArraySegment<byte> result = reader.ReadBytesSegment(-1);
}); });
} }
// NetworkReader.ToString actually contained a bug once.
// ensure this never happens again.
[Test]
public void ToStringTest()
{
// byte[] with offset and count that's smaller than total length
byte[] data = {0xA1, 0xB2, 0xC3, 0xD4, 0xE5};
ArraySegment<byte> segment = new ArraySegment<byte>(data, 1, 3);
NetworkReader reader = new NetworkReader(segment);
Assert.That(reader.ToString(), Is.EqualTo("[B2-C3-D4 @ 0/3]"));
// different position
reader.Position = 1;
Assert.That(reader.ToString(), Is.EqualTo("[B2-C3-D4 @ 1/3]"));
// byte[] with no offset and exact count
NetworkReader reader2 = new NetworkReader(data);
Assert.That(reader2.ToString(), Is.EqualTo("[A1-B2-C3-D4-E5 @ 0/5]"));
// different position
reader2.Position = 1;
Assert.That(reader2.ToString(), Is.EqualTo("[A1-B2-C3-D4-E5 @ 1/5]"));
}
} }
} }