test: bring coverage to 94% for RuneExtensions

This commit is contained in:
Oliver Booth 2023-03-31 20:11:03 +01:00
parent e852726b66
commit 3734965757
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
2 changed files with 33 additions and 22 deletions

View File

@ -28,7 +28,7 @@ public class RuneTests
}
[TestMethod]
public void RepeatShouldBeCorrect()
public void Repeat_ShouldRepeatRune_GivenValidCount()
{
const string expected = "aaaaaaaaaa";
var rune = new Rune('a');
@ -38,7 +38,7 @@ public class RuneTests
}
[TestMethod]
public void RepeatOneCountShouldBeLength1String()
public void Repeat_ShouldReturnStringOfLength1_GivenCountOf1()
{
string repeated = new Rune('a').Repeat(1);
Assert.AreEqual(1, repeated.Length);
@ -46,7 +46,20 @@ public class RuneTests
}
[TestMethod]
public void RepeatCodepoint_0000_007F_ShouldCorrect()
public void Repeat_ShouldThrowArgumentOutOfRangeException_GivenNegativeCount()
{
var rune = new Rune('a');
Assert.ThrowsException<ArgumentOutOfRangeException>(() => rune.Repeat(-1));
}
[TestMethod]
public void Repeat_ShouldReturnEmptyString_GivenCountOf0()
{
Assert.AreEqual(string.Empty, new Rune('a').Repeat(0));
}
[TestMethod]
public void RepeatCodepoint_0000_007F_ShouldReturnString()
{
string repeated = new Rune(69).Repeat(16);
Assert.AreEqual(16, repeated.Length);
@ -54,7 +67,7 @@ public class RuneTests
}
[TestMethod]
public void RepeatCodepoint_0080_07FF_ShouldCorrect()
public void RepeatCodepoint_0080_07FF_ShouldReturnString()
{
string repeated = new Rune(192).Repeat(8);
Assert.AreEqual(8, repeated.Length);
@ -62,7 +75,7 @@ public class RuneTests
}
[TestMethod]
public void RepeatCodepoint_0800_FFFF_ShouldCorrect()
public void RepeatCodepoint_0800_FFFF_ShouldReturnString()
{
string repeated = new Rune(0x0800).Repeat(5);
Assert.AreEqual(5, repeated.Length);
@ -70,23 +83,11 @@ public class RuneTests
}
[TestMethod]
public void RepeatCodepointBeyondU10000ShouldCorrect()
public void RepeatCodepointBeyondU10000ShouldReturnString()
{
string repeated = new Rune('\uD800', '\uDC00').Repeat(6);
Assert.AreEqual(12, repeated.Length);
Assert.AreEqual("𐀀𐀀𐀀𐀀𐀀𐀀", repeated);
}
[TestMethod]
public void RepeatZeroCountShouldBeEmpty()
{
Assert.AreEqual(string.Empty, new Rune('a').Repeat(0));
}
[TestMethod]
public void RepeatNegativeCountShouldThrow()
{
Assert.ThrowsException<ArgumentOutOfRangeException>(() => new Rune('a').Repeat(-1));
}
}
#endif

View File

@ -1,5 +1,6 @@
#if NETCOREAPP3_0_OR_GREATER
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
@ -82,7 +83,7 @@ public static class RuneExtensions
// Codepoint 0x10000 and beyond will takes **only** 2 UTF-16 character.
case 4:
{
return string.Create(count * 2, value, (span, rune) =>
return string.Create(count * 2, value, (span, _) =>
{
unsafe
{
@ -98,12 +99,21 @@ public static class RuneExtensions
}
default:
var msg = string.Format(CultureInfo.CurrentCulture, ExceptionMessages.UnexpectedRuneUtf8SequenceLength, length);
return Default();
}
[ExcludeFromCodeCoverage, DoesNotReturn]
string Default()
{
string exceptionFormat = ExceptionMessages.UnexpectedRuneUtf8SequenceLength;
string message = string.Format(CultureInfo.CurrentCulture, exceptionFormat, length);
#if NET7_0_OR_GREATER
throw new UnreachableException(msg);
throw new UnreachableException(message);
#else
throw new InvalidOperationException(msg);
throw new InvalidOperationException(message);
#endif
return default;
}
}
}