Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unit tests assertion parameters for expected and actual values #642

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public async Task Test_TaskExtensions_ResultOrDefault_FromAsyncTaskMethodBuilder

_ = await taskFromBuilder;

Assert.AreEqual(((Task)taskFromBuilder).GetResultOrDefault(), "Test");
Assert.AreEqual(taskFromBuilder.GetResultOrDefault(), "Test");
Assert.AreEqual("Test", ((Task)taskFromBuilder).GetResultOrDefault());
Assert.AreEqual("Test", taskFromBuilder.GetResultOrDefault());
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public class Test_ValueTypeExtensions
[TestMethod]
public void Test_ValueTypeExtensions_ToHexString()
{
Assert.AreEqual(((byte)0).ToHexString(), "0x00");
Assert.AreEqual(((byte)127).ToHexString(), "0x7F");
Assert.AreEqual(((byte)255).ToHexString(), "0xFF");
Assert.AreEqual(((ushort)6458).ToHexString(), "0x193A");
Assert.AreEqual(6458.ToHexString(), "0x0000193A");
Assert.AreEqual((-1).ToHexString(), "0xFFFFFFFF");
Assert.AreEqual(true.ToHexString(), "0x01");
Assert.AreEqual("0x00", ((byte)0).ToHexString());
Assert.AreEqual("0x7F", ((byte)127).ToHexString());
Assert.AreEqual("0xFF", ((byte)255).ToHexString());
Assert.AreEqual("0x193A", ((ushort)6458).ToHexString());
Assert.AreEqual("0x0000193A", 6458.ToHexString());
Assert.AreEqual("0xFFFFFFFF", (-1).ToHexString());
Assert.AreEqual("0x01", true.ToHexString());
}
}
6 changes: 3 additions & 3 deletions tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ public void Test_Guard_CallerArgumentExpression_1()
}
catch (ArgumentNullException e)
{
Assert.AreEqual(e.ParamName, nameof(thisStringShouldNotBeNull));
Assert.AreEqual(nameof(thisStringShouldNotBeNull), e.ParamName);

return;
}
Expand All @@ -775,7 +775,7 @@ public void Test_Guard_CallerArgumentExpression_2()
}
catch (ArgumentOutOfRangeException e)
{
Assert.AreEqual(e.ParamName, nameof(thisIndexIsOutOfRange));
Assert.AreEqual(nameof(thisIndexIsOutOfRange), e.ParamName);

return;
}
Expand All @@ -795,7 +795,7 @@ public void Test_Guard_CallerArgumentExpression_3()
}
catch (ArgumentException e)
{
Assert.AreEqual(e.ParamName, nameof(thisArrayShouldNotBeShorterThan10));
Assert.AreEqual(nameof(thisArrayShouldNotBeShorterThan10), e.ParamName);

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ public void Test_ArrayPoolBufferWriterOfT_BufferSize(int request, int expected)

byte[] array = (byte[])arrayFieldInfo!.GetValue(writer)!;

Assert.AreEqual(array.Length, expected);
Assert.AreEqual(expected, array.Length);
}

[TestMethod]
public void Test_ArrayPoolBufferWriterOfT_AllocateAndGetMemoryAndSpan()
{
ArrayPoolBufferWriter<byte>? writer = new();

Assert.AreEqual(writer.Capacity, 256);
Assert.AreEqual(writer.FreeCapacity, 256);
Assert.AreEqual(writer.WrittenCount, 0);
Assert.AreEqual(256, writer.Capacity);
Assert.AreEqual(256, writer.FreeCapacity);
Assert.AreEqual(0, writer.WrittenCount);
Assert.IsTrue(writer.WrittenMemory.IsEmpty);
Assert.IsTrue(writer.WrittenSpan.IsEmpty);

Expand All @@ -72,11 +72,11 @@ public void Test_ArrayPoolBufferWriterOfT_AllocateAndGetMemoryAndSpan()

writer.Advance(43);

Assert.AreEqual(writer.Capacity, 256);
Assert.AreEqual(writer.FreeCapacity, 256 - 43);
Assert.AreEqual(writer.WrittenCount, 43);
Assert.AreEqual(writer.WrittenMemory.Length, 43);
Assert.AreEqual(writer.WrittenSpan.Length, 43);
Assert.AreEqual(256, writer.Capacity);
Assert.AreEqual(256 - 43, writer.FreeCapacity);
Assert.AreEqual(43, writer.WrittenCount);
Assert.AreEqual(43, writer.WrittenMemory.Length);
Assert.AreEqual(43, writer.WrittenSpan.Length);

_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => writer.Advance(-1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => writer.GetMemory(-1));
Expand All @@ -99,11 +99,11 @@ public void Test_ArrayPoolBufferWriterOfT_AllocateFromCustomPoolAndGetMemoryAndS

using (ArrayPoolBufferWriter<byte>? writer = new(pool))
{
Assert.AreEqual(pool.RentedArrays.Count, 1);
Assert.AreEqual(1, pool.RentedArrays.Count);

Assert.AreEqual(writer.Capacity, 256);
Assert.AreEqual(writer.FreeCapacity, 256);
Assert.AreEqual(writer.WrittenCount, 0);
Assert.AreEqual(256, writer.Capacity);
Assert.AreEqual(256, writer.FreeCapacity);
Assert.AreEqual(0, writer.WrittenCount);
Assert.IsTrue(writer.WrittenMemory.IsEmpty);
Assert.IsTrue(writer.WrittenSpan.IsEmpty);

Expand All @@ -113,11 +113,11 @@ public void Test_ArrayPoolBufferWriterOfT_AllocateFromCustomPoolAndGetMemoryAndS

writer.Advance(43);

Assert.AreEqual(writer.Capacity, 256);
Assert.AreEqual(writer.FreeCapacity, 256 - 43);
Assert.AreEqual(writer.WrittenCount, 43);
Assert.AreEqual(writer.WrittenMemory.Length, 43);
Assert.AreEqual(writer.WrittenSpan.Length, 43);
Assert.AreEqual(256, writer.Capacity);
Assert.AreEqual(256 - 43, writer.FreeCapacity);
Assert.AreEqual(43, writer.WrittenCount);
Assert.AreEqual(43, writer.WrittenMemory.Length);
Assert.AreEqual(43, writer.WrittenSpan.Length);

_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => writer.Advance(-1));
_ = Assert.ThrowsException<ArgumentOutOfRangeException>(() => writer.GetMemory(-1));
Expand All @@ -133,7 +133,7 @@ public void Test_ArrayPoolBufferWriterOfT_AllocateFromCustomPoolAndGetMemoryAndS
_ = Assert.ThrowsException<ObjectDisposedException>(() => writer.Advance(1));
}

Assert.AreEqual(pool.RentedArrays.Count, 0);
Assert.AreEqual(0, pool.RentedArrays.Count);
}

[TestMethod]
Expand All @@ -158,12 +158,12 @@ public void Test_ArrayPoolBufferWriterOfT_Clear()

writer.Advance(4);

Assert.AreEqual(writer.WrittenCount, 4);
Assert.AreEqual(4, writer.WrittenCount);
Assert.IsTrue(span.SequenceEqual(data));

writer.Clear();

Assert.AreEqual(writer.WrittenCount, 0);
Assert.AreEqual(0, writer.WrittenCount);
Assert.IsTrue(span.ToArray().All(b => b == 0));
}

Expand Down Expand Up @@ -194,13 +194,13 @@ public void Test_ArrayPoolBufferWriterOfT_AsStream()
writeStream.Write(guid);
}

Assert.AreEqual(writer.WrittenCount, GuidSize);
Assert.AreEqual(GuidSize, writer.WrittenCount);

// Here we get a readable stream instead, and read from it to ensure
// the previous data was written correctly from the writeable stream.
using (Stream stream = writer.WrittenMemory.AsStream())
{
Assert.AreEqual(stream.Length, GuidSize);
Assert.AreEqual(GuidSize, stream.Length);

byte[] result = new byte[GuidSize];

Expand Down Expand Up @@ -234,13 +234,13 @@ public void Test_ArrayPoolBufferWriterOfT_AllocateAndGetArray()

Assert.IsNotNull(segment.Array);
Assert.IsTrue(segment.Array.Length >= bufferWriter.WrittenSpan.Length);
Assert.AreEqual(segment.Offset, 0);
Assert.AreEqual(segment.Count, bufferWriter.WrittenSpan.Length);
Assert.AreEqual(0, segment.Offset);
Assert.AreEqual(bufferWriter.WrittenSpan.Length, segment.Count);

_ = MemoryMarshal.TryGetArray(bufferWriter.WrittenMemory, out ArraySegment<int> writtenSegment);

// The array is the same one as the one from the written span
Assert.AreSame(segment.Array, writtenSegment.Array);
Assert.AreSame(writtenSegment.Array, segment.Array);

bufferWriter.Dispose();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ public void Test_MemoryBufferWriterOfT_AllocateAndGetMemoryAndSpan()

MemoryBufferWriter<byte>? writer = new(memory);

Assert.AreEqual(writer.Capacity, 256);
Assert.AreEqual(writer.FreeCapacity, 256);
Assert.AreEqual(writer.WrittenCount, 0);
Assert.AreEqual(256, writer.Capacity);
Assert.AreEqual(256, writer.FreeCapacity);
Assert.AreEqual(0, writer.WrittenCount);
Assert.IsTrue(writer.WrittenMemory.IsEmpty);
Assert.IsTrue(writer.WrittenSpan.IsEmpty);

Span<byte> span = writer.GetSpan(43);

Assert.AreEqual(span.Length, memory.Length);
Assert.AreEqual(memory.Length, span.Length);

writer.Advance(43);

Assert.AreEqual(writer.Capacity, 256);
Assert.AreEqual(writer.FreeCapacity, 256 - 43);
Assert.AreEqual(writer.WrittenCount, 43);
Assert.AreEqual(writer.WrittenMemory.Length, 43);
Assert.AreEqual(writer.WrittenSpan.Length, 43);
Assert.AreEqual(256, writer.Capacity);
Assert.AreEqual(256 - 43, writer.FreeCapacity);
Assert.AreEqual(43, writer.WrittenCount);
Assert.AreEqual(43, writer.WrittenMemory.Length);
Assert.AreEqual(43, writer.WrittenSpan.Length);

Assert.AreEqual(memory.Length - 43, writer.GetSpan().Length);
Assert.AreEqual(memory.Length - 43, writer.GetMemory().Length);
Expand Down Expand Up @@ -64,12 +64,12 @@ public void Test_MemoryBufferWriterOfT_Clear()

writer.Advance(4);

Assert.AreEqual(writer.WrittenCount, 4);
Assert.AreEqual(4, writer.WrittenCount);
Assert.IsTrue(span.SequenceEqual(data));

writer.Clear();

Assert.AreEqual(writer.WrittenCount, 0);
Assert.AreEqual(0, writer.WrittenCount);
Assert.IsTrue(span.ToArray().All(b => b == 0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void Test_MemoryOwnerOfT_AllocateFromCustomPoolAndGetMemoryAndSpan()

using (MemoryOwner<int>? buffer = MemoryOwner<int>.Allocate(127, pool))
{
Assert.AreEqual(pool.RentedArrays.Count, 1);
Assert.AreEqual(1, pool.RentedArrays.Count);

Assert.IsTrue(buffer.Length == 127);
Assert.IsTrue(buffer.Memory.Length == 127);
Expand All @@ -47,7 +47,7 @@ public void Test_MemoryOwnerOfT_AllocateFromCustomPoolAndGetMemoryAndSpan()
Assert.IsTrue(buffer.Span.ToArray().All(i => i == 42));
}

Assert.AreEqual(pool.RentedArrays.Count, 0);
Assert.AreEqual(0, pool.RentedArrays.Count);
}

[TestMethod]
Expand Down Expand Up @@ -130,8 +130,8 @@ public void Test_MemoryOwnerOfT_AllocateAndGetArray()

Assert.IsNotNull(segment.Array);
Assert.IsTrue(segment.Array.Length >= buffer.Length);
Assert.AreEqual(segment.Offset, 0);
Assert.AreEqual(segment.Count, buffer.Length);
Assert.AreEqual(0, segment.Offset);
Assert.AreEqual(buffer.Length, segment.Count);

MemoryOwner<int>? second = buffer.Slice(10, 80);

Expand All @@ -145,8 +145,8 @@ public void Test_MemoryOwnerOfT_AllocateAndGetArray()
// Same as before, but we now also verify the initial offset != 0, as we used Slice
Assert.IsNotNull(segment.Array);
Assert.IsTrue(segment.Array.Length >= second.Length);
Assert.AreEqual(segment.Offset, 10);
Assert.AreEqual(segment.Count, second.Length);
Assert.AreEqual(10, segment.Offset);
Assert.AreEqual(second.Length, segment.Count);

second.Dispose();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Test_SpanOwnerOfT_AllocateFromCustomPoolAndGetMemoryAndSpan()

using (SpanOwner<int> buffer = SpanOwner<int>.Allocate(127, pool))
{
Assert.AreEqual(pool.RentedArrays.Count, 1);
Assert.AreEqual(1, pool.RentedArrays.Count);

Assert.IsTrue(buffer.Length == 127);
Assert.IsTrue(buffer.Span.Length == 127);
Expand All @@ -43,7 +43,7 @@ public void Test_SpanOwnerOfT_AllocateFromCustomPoolAndGetMemoryAndSpan()
Assert.IsTrue(buffer.Span.ToArray().All(i => i == 42));
}

Assert.AreEqual(pool.RentedArrays.Count, 0);
Assert.AreEqual(0, pool.RentedArrays.Count);
}

[TestMethod]
Expand Down Expand Up @@ -87,7 +87,7 @@ public void Test_SpanOwnerOfT_AllocateAndGetArray()
// is all documented in the XML docs for the SpanOwner<T> type.
Assert.IsNotNull(segment.Array);
Assert.IsTrue(segment.Array.Length >= buffer.Length);
Assert.AreEqual(segment.Offset, 0);
Assert.AreEqual(segment.Count, buffer.Length);
Assert.AreEqual(0, segment.Offset);
Assert.AreEqual(buffer.Length, segment.Count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void Test_ReadOnlyRefEnumerable_DangerousCreate_Ok(int length, int step,

int[] result = enumerable.ToArray();

CollectionAssert.AreEqual(result, values);
CollectionAssert.AreEqual(values, result);
}

[TestMethod]
Expand Down Expand Up @@ -64,7 +64,7 @@ public void Test_ReadOnlyRefEnumerable_Indexer(int step, int[] values)

for (int i = 0; i < enumerable.Length; i++)
{
Assert.AreEqual(enumerable[i], values[i]);
Assert.AreEqual(values[i], enumerable[i]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void Test_RefEnumerable_DangerousCreate_Ok(int length, int step, int[] va

int[] result = enumerable.ToArray();

CollectionAssert.AreEqual(result, values);
CollectionAssert.AreEqual(values, result);
}

[TestMethod]
Expand Down Expand Up @@ -64,7 +64,7 @@ public void Test_RefEnumerable_Indexer(int step, int[] values)

for (int i = 0; i < enumerable.Length; i++)
{
Assert.AreEqual(enumerable[i], values[i]);
Assert.AreEqual(values[i], enumerable[i]);
}
}

Expand Down
Loading