From 373496575765c92a67d42fcb64944c1b4f9dc236 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Fri, 31 Mar 2023 20:11:03 +0100 Subject: [PATCH] test: bring coverage to 94% for RuneExtensions --- X10D.Tests/src/Text/RuneTests.cs | 37 ++++++++++++++++---------------- X10D/src/Text/RuneExtensions.cs | 18 ++++++++++++---- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/X10D.Tests/src/Text/RuneTests.cs b/X10D.Tests/src/Text/RuneTests.cs index 6382fab..38f2fae 100644 --- a/X10D.Tests/src/Text/RuneTests.cs +++ b/X10D.Tests/src/Text/RuneTests.cs @@ -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(() => 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(() => new Rune('a').Repeat(-1)); - } } #endif diff --git a/X10D/src/Text/RuneExtensions.cs b/X10D/src/Text/RuneExtensions.cs index 8261096..e336580 100644 --- a/X10D/src/Text/RuneExtensions.cs +++ b/X10D/src/Text/RuneExtensions.cs @@ -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; } } }