diff --git a/PowerKit.Tests/CharExtensionsTests.cs b/PowerKit.Tests/CharExtensionsTests.cs new file mode 100644 index 0000000..5a2331e --- /dev/null +++ b/PowerKit.Tests/CharExtensionsTests.cs @@ -0,0 +1,25 @@ +using FluentAssertions; +using PowerKit.Extensions; +using Xunit; + +namespace PowerKit.Tests; + +public class CharExtensionsTests +{ + [Fact] + public void Repeat_Test() + { + // Act & assert + 'a'.Repeat(3).Should().Be("aaa"); + 'x'.Repeat(1).Should().Be("x"); + 'z'.Repeat(0).Should().Be(""); + } + + [Fact] + public void AsString_Test() + { + // Act & assert + 'a'.AsString().Should().Be("a"); + ' '.AsString().Should().Be(" "); + } +} diff --git a/PowerKit/Extensions/CharExtensions.cs b/PowerKit/Extensions/CharExtensions.cs new file mode 100644 index 0000000..07afdbb --- /dev/null +++ b/PowerKit/Extensions/CharExtensions.cs @@ -0,0 +1,23 @@ +#nullable enable +using System.Diagnostics.CodeAnalysis; + +namespace PowerKit.Extensions; + +#if !POWERKIT_INCLUDE_COVERAGE +[ExcludeFromCodeCoverage] +#endif +internal static class CharExtensions +{ + extension(char c) + { + /// + /// Returns a string that contains the character repeated the specified number of times. + /// + public string Repeat(int count) => new(c, count); + + /// + /// Returns a string that contains only this character. + /// + public string AsString() => c.Repeat(1); + } +}