Skip to content

Commit

Permalink
adapt test syntax to newer NUnit versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ssg committed Sep 19, 2024
1 parent d8d6b74 commit 78096d3
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 57 deletions.
54 changes: 36 additions & 18 deletions test/Base16/Base16Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void Decode_Stream(Base16 encoder, byte[] expectedOutput, string input)
using var memoryStream = new MemoryStream();
using var reader = new StringReader(input);
encoder.Decode(reader, memoryStream);
CollectionAssert.AreEqual(expectedOutput, memoryStream.ToArray());
Assert.That(memoryStream.ToArray(), Is.EqualTo(expectedOutput));
}

[Test]
Expand All @@ -72,7 +72,7 @@ public async Task DecodeAsync_Stream(Base16 encoder, byte[] expectedOutput, stri
using var memoryStream = new MemoryStream();
using var reader = new StringReader(input);
await encoder.DecodeAsync(reader, memoryStream);
CollectionAssert.AreEqual(expectedOutput, memoryStream.ToArray());
Assert.That(memoryStream.ToArray(), Is.EqualTo(expectedOutput));
}

[Test]
Expand All @@ -98,9 +98,12 @@ public void Encode(Base16 encoder, byte[] input, string expectedOutput)
public void TryEncode_RegularInput_Succeeds(Base16 encoder, byte[] input, string expectedOutput)
{
var output = new char[input.Length * 2];
Assert.That(encoder.TryEncode(input, output, out int numCharsWritten), Is.True);
Assert.That(numCharsWritten, Is.EqualTo(output.Length));
Assert.That(new string(output), Is.EqualTo(expectedOutput));
Assert.Multiple(() =>
{
Assert.That(encoder.TryEncode(input, output, out int numCharsWritten), Is.True);
Assert.That(numCharsWritten, Is.EqualTo(output.Length));
Assert.That(new string(output), Is.EqualTo(expectedOutput));
});
}

[Test]
Expand All @@ -109,34 +112,43 @@ public void TryEncode_SmallerOutput_Fails(Base16 encoder)
{
var input = new byte[4];
var output = Array.Empty<char>();
Assert.That(encoder.TryEncode(input, output, out int numCharsWritten), Is.False);
Assert.That(numCharsWritten, Is.EqualTo(0));
Assert.Multiple(() =>
{
Assert.That(encoder.TryEncode(input, output, out int numCharsWritten), Is.False);
Assert.That(numCharsWritten, Is.EqualTo(0));
});
}

[Test]
[TestCaseSource(nameof(testData))]
public void Decode(Base16 encoder, byte[] expectedOutput, string input)
{
var result = encoder.Decode(input);
CollectionAssert.AreEqual(expectedOutput, result.ToArray());
Assert.That(result.ToArray(), Is.EqualTo(expectedOutput));
}

[Test]
[TestCaseSource(nameof(testData))]
public void TryDecode_RegularInput_Succeeds(Base16 encoder, byte[] expectedOutput, string input)
{
var output = new byte[expectedOutput.Length];
Assert.That(encoder.TryDecode(input, output, out int numBytesWritten), Is.True);
Assert.That(numBytesWritten, Is.EqualTo(output.Length));
CollectionAssert.AreEqual(expectedOutput, output);
Assert.Multiple(() =>
{
Assert.That(encoder.TryDecode(input, output, out int numBytesWritten), Is.True);
Assert.That(numBytesWritten, Is.EqualTo(output.Length));
Assert.That(output, Is.EqualTo(expectedOutput));
});
}

[Test]
public void TryDecode_InvalidChar_ReturnsFalse()
{
var output = new byte[3];
Assert.That(Base16.UpperCase.TryDecode("1234ZB", output, out int numWritten), Is.False);
Assert.That(numWritten, Is.EqualTo(2));
Assert.Multiple(() =>
{
Assert.That(Base16.UpperCase.TryDecode("1234ZB", output, out int numWritten), Is.False);
Assert.That(numWritten, Is.EqualTo(2));
});
}

[Test]
Expand All @@ -145,8 +157,11 @@ public void TryDecode_SmallOutputBuffer_Fails(Base16 encoder)
{
var input = "1234";
var output = new byte[1];
Assert.That(encoder.TryDecode(input, output, out int numBytesWritten), Is.False);
Assert.That(numBytesWritten, Is.EqualTo(0));
Assert.Multiple(() =>
{
Assert.That(encoder.TryDecode(input, output, out int numBytesWritten), Is.False);
Assert.That(numBytesWritten, Is.EqualTo(0));
});
}

[Test]
Expand All @@ -155,16 +170,19 @@ public void TryDecode_UnevenInputBuffer_Fails(Base16 encoder)
{
var input = "123";
var output = new byte[1];
Assert.That(encoder.TryDecode(input, output, out int numBytesWritten), Is.False);
Assert.That(numBytesWritten, Is.EqualTo(0));
Assert.Multiple(() =>
{
Assert.That(encoder.TryDecode(input, output, out int numBytesWritten), Is.False);
Assert.That(numBytesWritten, Is.EqualTo(0));
});
}

[Test]
[TestCaseSource(nameof(testData))]
public void Decode_OtherCase_StillPasses(Base16 encoder, byte[] expectedOutput, string input)
{
var result = encoder.Decode(input.ToUpperInvariant());
CollectionAssert.AreEqual(expectedOutput, result.ToArray());
Assert.That(result.ToArray(), Is.EqualTo(expectedOutput));
}

[Test]
Expand Down
7 changes: 5 additions & 2 deletions test/Base16/LegacyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class LegacyTest
public void Decode_DecodesBothLowerAndUpperCase()
{
var expectedResult = new byte[] { 0xAB, 0xCD, 0xEF, 0xF0 };
Assert.That(Base16.Decode("ABCDEFF0").ToArray(), Is.EqualTo(expectedResult));
Assert.That(Base16.Decode("abcdeff0").ToArray(), Is.EqualTo(expectedResult));
Assert.Multiple(() =>
{
Assert.That(Base16.Decode("ABCDEFF0").ToArray(), Is.EqualTo(expectedResult));
Assert.That(Base16.Decode("abcdeff0").ToArray(), Is.EqualTo(expectedResult));
});
}
}
14 changes: 10 additions & 4 deletions test/Base32/Base32AlphabetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ public void ctorWithPaddingChar_Works()
{
// alphabet characters are unimportant here
var alpha = new Base32Alphabet("0123456789abcdef0123456789abcdef", '!', PaddingPosition.Start);
Assert.That(alpha.PaddingChar, Is.EqualTo('!'));
Assert.That(alpha.PaddingPosition, Is.EqualTo(PaddingPosition.Start));
Assert.Multiple(() =>
{
Assert.That(alpha.PaddingChar, Is.EqualTo('!'));
Assert.That(alpha.PaddingPosition, Is.EqualTo(PaddingPosition.Start));
});
}

[Test]
public void GetSafeByteCountForDecoding_Works()
{
Assert.That(Base32.Crockford.GetSafeByteCountForDecoding("12345"), Is.EqualTo(3));
Assert.That(Base32.Crockford.GetSafeByteCountForDecoding(""), Is.EqualTo(0));
Assert.Multiple(() =>
{
Assert.That(Base32.Crockford.GetSafeByteCountForDecoding("12345"), Is.EqualTo(3));
Assert.That(Base32.Crockford.GetSafeByteCountForDecoding(""), Is.EqualTo(0));
});
}
}
16 changes: 11 additions & 5 deletions test/Base32/CrockfordTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void Decode_SampleInterface_Compiles()
// do not edit/refactor the code below
string myText = "CSQPYRK1E8"; // any buffer will do
Span<byte> result = Base32.Crockford.Decode(myText);
Assert.That(result.Length > 0, Is.True);
Assert.That(result.Length, Is.GreaterThan(0));
}

[Test]
Expand Down Expand Up @@ -168,8 +168,11 @@ public void TryEncode_Simple_ReturnsExpectedValues(string input, string expected
byte[] bytes = Encoding.ASCII.GetBytes(input);
var output = new char[Base32.Crockford.GetSafeCharCountForEncoding(bytes)];
bool success = Base32.Crockford.TryEncode(bytes, output, padded, out int numCharsWritten);
Assert.That(success, Is.True);
Assert.That(output[..numCharsWritten], Is.EqualTo(expectedOutput));
Assert.Multiple(() =>
{
Assert.That(success, Is.True);
Assert.That(output[..numCharsWritten], Is.EqualTo(expectedOutput));
});
}

[Test]
Expand Down Expand Up @@ -204,8 +207,11 @@ public void TryDecode_ReturnsExpectedValues(string expectedOutput, string input,
public void TryDecode_ZeroBuffer_ReturnsFalse()
{
var success = Base32.Crockford.TryDecode("test", [], out int numBytesWritten);
Assert.That(success, Is.False);
Assert.That(numBytesWritten, Is.EqualTo(0));
Assert.Multiple(() =>
{
Assert.That(success, Is.False);
Assert.That(numBytesWritten, Is.EqualTo(0));
});
}

[Test]
Expand Down
7 changes: 5 additions & 2 deletions test/Base58/Base58CheckTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ public void TryDecodeCheck_ValidInput_ReturnsExpectedResult(int expectedVersion,
{
Span<byte> outputBuffer = new byte[256];
bool result = Base58.Bitcoin.TryDecodeCheck(input, outputBuffer, out byte actualVersion, out int numBytesWritten);
Assert.IsTrue(result);
Assert.That(actualVersion, Is.EqualTo(expectedVersion));
Assert.Multiple(() =>
{
Assert.That(result, Is.True);
Assert.That(actualVersion, Is.EqualTo(expectedVersion));
});
string output = Encoding.ASCII.GetString(outputBuffer[..numBytesWritten]);
Assert.That(output, Is.EqualTo(expectedOutput));
}
Expand Down
16 changes: 11 additions & 5 deletions test/Base58/BitcoinTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ public void TryEncode_Bitcoin_ReturnsExpectedResults(string input, string expect
{
var inputBuffer = Base16.UpperCase.Decode(input);
var outputBuffer = new char[Base58.Bitcoin.GetSafeCharCountForEncoding(inputBuffer)];
Assert.That(Base58.Bitcoin.TryEncode(inputBuffer, outputBuffer, out int numWritten), Is.True);
Assert.That(outputBuffer[..numWritten], Is.EqualTo(expectedOutput));
Assert.Multiple(() =>
{
Assert.That(Base58.Bitcoin.TryEncode(inputBuffer, outputBuffer, out int numWritten), Is.True);
Assert.That(outputBuffer[..numWritten], Is.EqualTo(expectedOutput));
});
}

[Test]
Expand All @@ -75,15 +78,18 @@ public void Encode_EmptyBuffer_ReturnsEmptyString()
public void Decode_EmptyString_ReturnsEmptyBuffer()
{
var result = Base58.Bitcoin.Decode(String.Empty);
Assert.That(result.Length, Is.EqualTo(0));
Assert.That(result, Is.Empty);
}

[Test]
public void TryDecode_EmptyString_ReturnsEmptyBuffer()
{
var result = Base58.Bitcoin.TryDecode(String.Empty, new byte[1], out int numBytesWritten);
Assert.That(result, Is.True);
Assert.That(numBytesWritten, Is.EqualTo(0));
Assert.Multiple(() =>
{
Assert.That(result, Is.True);
Assert.That(numBytesWritten, Is.EqualTo(0));
});
}

[Test]
Expand Down
11 changes: 7 additions & 4 deletions test/Base58/Cb58Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ public void EncodeCheck_ValidInput_ReturnsExpectedResult(byte[] bytes, string ex
[TestCaseSource(nameof(testData))]
public void TryDecodeCheck_ValidInput_ReturnsExpectedResult(byte[] expectedOutput, string input)
{
Span<byte> outputBuffer = new byte[256];
bool result = Base58.Bitcoin.TryDecodeCb58(input, outputBuffer, out int numBytesWritten);
Assert.IsTrue(result);
Assert.That(outputBuffer[..numBytesWritten].ToArray(), Is.EqualTo(expectedOutput));
byte[] outputBuffer = new byte[256];
bool result = Base58.Bitcoin.TryDecodeCb58(input, outputBuffer.AsSpan(), out int numBytesWritten);
Assert.Multiple(() =>
{
Assert.That(result, Is.True);
Assert.That(outputBuffer[..numBytesWritten], Is.EqualTo(expectedOutput));
});
}

}
9 changes: 6 additions & 3 deletions test/Base58/FlickrTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ public void TryEncode_Flickr_ReturnsExpectedResults(string input, string expecte
{
var inputBuffer = Base16.UpperCase.Decode(input);
var outputBuffer = new char[Base58.Flickr.GetSafeCharCountForEncoding(inputBuffer)];
Assert.That(Base58.Flickr.TryEncode(inputBuffer, outputBuffer, out int numWritten), Is.True);
Assert.That(outputBuffer[..numWritten], Is.EqualTo(expectedOutput));
Assert.Multiple(() =>
{
Assert.That(Base58.Flickr.TryEncode(inputBuffer, outputBuffer, out int numWritten), Is.True);
Assert.That(outputBuffer[..numWritten], Is.EqualTo(expectedOutput));
});
}

[Test]
Expand All @@ -72,7 +75,7 @@ public void Encode_EmptyBuffer_ReturnsEmptyString()
public void Decode_EmptyString_ReturnsEmptyBuffer()
{
var result = Base58.Flickr.Decode(String.Empty);
Assert.That(result.Length, Is.EqualTo(0));
Assert.That(result, Is.Empty);
}

[Test]
Expand Down
9 changes: 6 additions & 3 deletions test/Base58/RippleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ public void TryEncode_Ripple_ReturnsExpectedResults(string input, string expecte
{
var inputBuffer = Base16.UpperCase.Decode(input);
var outputBuffer = new char[Base58.Ripple.GetSafeCharCountForEncoding(inputBuffer)];
Assert.That(Base58.Ripple.TryEncode(inputBuffer, outputBuffer, out int numWritten), Is.True);
Assert.That(outputBuffer[..numWritten], Is.EqualTo(expectedOutput));
Assert.Multiple(() =>
{
Assert.That(Base58.Ripple.TryEncode(inputBuffer, outputBuffer, out int numWritten), Is.True);
Assert.That(outputBuffer[..numWritten], Is.EqualTo(expectedOutput));
});
}

[Test]
Expand All @@ -72,7 +75,7 @@ public void Encode_EmptyBuffer_ReturnsEmptyString()
public void Decode_EmptyString_ReturnsEmptyBuffer()
{
var result = Base58.Ripple.Decode(String.Empty);
Assert.That(result.Length, Is.EqualTo(0));
Assert.That(result, Is.Empty);
}

[Test]
Expand Down
22 changes: 14 additions & 8 deletions test/Base85/Ascii85Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Decode_Whitespace_IsIgnored(byte[] expectedOutput, string input)
}
actualInput += " ";
var result = Base85.Ascii85.Decode(actualInput);
CollectionAssert.AreEqual(expectedOutput, result);
Assert.That(result, Is.EqualTo(expectedOutput));
}

[Test]
Expand Down Expand Up @@ -80,8 +80,11 @@ public void Encode_TestVectors_ShouldEncodeCorrectly(byte[] input, string expect
public void TryEncode_TestVectors_ShouldEncodeCorrectly(byte[] input, string expectedOutput)
{
var output = new char[Base85.Ascii85.GetSafeCharCountForEncoding(input)];
Assert.That(Base85.Ascii85.TryEncode(input, output, out int numCharsWritten), Is.True);
Assert.That(new string(output[..numCharsWritten]), Is.EqualTo(expectedOutput));
Assert.Multiple(() =>
{
Assert.That(Base85.Ascii85.TryEncode(input, output, out int numCharsWritten), Is.True);
Assert.That(new string(output[..numCharsWritten]), Is.EqualTo(expectedOutput));
});
}

[Test]
Expand Down Expand Up @@ -109,7 +112,7 @@ public void Decode_TestVectorsWithStream_ShouldDecodeCorrectly(byte[] expectedOu
using var inputStream = new StringReader(input);
using var writer = new MemoryStream();
Base85.Ascii85.Decode(inputStream, writer);
CollectionAssert.AreEqual(expectedOutput, writer.ToArray());
Assert.That(writer.ToArray(), Is.EqualTo(expectedOutput));
}

[Test]
Expand All @@ -119,23 +122,26 @@ public async Task DecodeAsync_TestVectorsWithStream_ShouldDecodeCorrectly(byte[]
using var inputStream = new StringReader(input);
using var writer = new MemoryStream();
await Base85.Ascii85.DecodeAsync(inputStream, writer);
CollectionAssert.AreEqual(expectedOutput, writer.ToArray());
Assert.That(writer.ToArray(), Is.EqualTo(expectedOutput));
}

[Test]
[TestCaseSource(nameof(testVectors))]
public void Decode_TestVectors_ShouldDecodeCorrectly(byte[] expectedOutput, string input)
{
var result = Base85.Ascii85.Decode(input);
CollectionAssert.AreEqual(expectedOutput, result);
Assert.That(result, Is.EqualTo(expectedOutput));
}

[Test]
[TestCaseSource(nameof(testVectors))]
public void TryDecode_TestVectors_ShouldDecodeCorrectly(byte[] expectedOutput, string input)
{
var buffer = new byte[Base85.Ascii85.GetSafeByteCountForDecoding(input)];
Assert.That(Base85.Ascii85.TryDecode(input, buffer, out int numBytesWritten), Is.True);
CollectionAssert.AreEqual(expectedOutput, buffer[..numBytesWritten]);
Assert.Multiple(() =>
{
Assert.That(Base85.Ascii85.TryDecode(input, buffer, out int numBytesWritten), Is.True);
Assert.That(buffer[..numBytesWritten], Is.EqualTo(expectedOutput));
});
}
}
7 changes: 5 additions & 2 deletions test/Base85/Base85AlphabetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public void GetSafeCharCountForEncoding_Length_Works(int inputLen, int expectedS
[Test]
public void HasShortcut()
{
Assert.That(Base85.Ascii85.Alphabet.HasShortcut, Is.True);
Assert.That(Base85.Z85.Alphabet.HasShortcut, Is.False);
Assert.Multiple(() =>
{
Assert.That(Base85.Ascii85.Alphabet.HasShortcut, Is.True);
Assert.That(Base85.Z85.Alphabet.HasShortcut, Is.False);
});
}
}
2 changes: 1 addition & 1 deletion test/Base85/Z85Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public void Encode_NullBuffer_ReturnsEmptyString()
public void Decode_TestVectors_ShouldDecodeCorrectly(byte[] expectedOutput, string input)
{
var result = Base85.Z85.Decode(input);
CollectionAssert.AreEqual(expectedOutput, result);
Assert.That(result, Is.EqualTo(expectedOutput));
}
}
Loading

0 comments on commit 78096d3

Please sign in to comment.