From e391e3b0c9276022b2e9c1d6cceb22b0666332c1 Mon Sep 17 00:00:00 2001 From: kentah <158997636+KentaHizume@users.noreply.github.com> Date: Fri, 27 Dec 2024 14:03:11 +0900 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=A1=8C=E6=96=87=E5=AD=97=E3=82=92?= =?UTF-8?q?=E5=8F=96=E3=82=8A=E9=99=A4=E3=81=8Fstring=E3=81=AE=E6=8B=A1?= =?UTF-8?q?=E5=BC=B5=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=82=92=E5=AE=9A?= =?UTF-8?q?=E7=BE=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AssetApplicationService.cs | 2 +- .../Dressca.SystemCommon/StringExtentions.cs | 21 ++++++++++++ .../StringExtentionsTest.cs | 34 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 samples/Dressca/dressca-backend/src/Dressca.SystemCommon/StringExtentions.cs create mode 100644 samples/Dressca/dressca-backend/tests/Dressca.UnitTests.SystemCommon/StringExtentionsTest.cs diff --git a/samples/Dressca/dressca-backend/src/Dressca.ApplicationCore/ApplicationService/AssetApplicationService.cs b/samples/Dressca/dressca-backend/src/Dressca.ApplicationCore/ApplicationService/AssetApplicationService.cs index eb6e8eebe..197701d8b 100644 --- a/samples/Dressca/dressca-backend/src/Dressca.ApplicationCore/ApplicationService/AssetApplicationService.cs +++ b/samples/Dressca/dressca-backend/src/Dressca.ApplicationCore/ApplicationService/AssetApplicationService.cs @@ -47,7 +47,7 @@ public AssetApplicationService( public async Task GetAssetStreamInfoAsync(string assetCode) { // ログインジェクションを防ぐために改行文字を取り除きます。 - var sanitizedAssetCode = assetCode.Replace(Environment.NewLine, string.Empty).Replace("\n", string.Empty).Replace("\r", string.Empty); + var sanitizedAssetCode = assetCode.RemoveNewLines(); this.logger.LogDebug(Events.DebugEvent, LogMessages.AssetApplicationService_GetAssetStreamInfoStart, sanitizedAssetCode); Asset? asset; diff --git a/samples/Dressca/dressca-backend/src/Dressca.SystemCommon/StringExtentions.cs b/samples/Dressca/dressca-backend/src/Dressca.SystemCommon/StringExtentions.cs new file mode 100644 index 000000000..1f7e63077 --- /dev/null +++ b/samples/Dressca/dressca-backend/src/Dressca.SystemCommon/StringExtentions.cs @@ -0,0 +1,21 @@ +namespace System; + +/// +/// クラスの拡張メソッドを提供します。 +/// +public static class StringExtentions +{ + /// + /// 対象の文字列から改行文字(\r、\n)を取り除きます。 + /// + /// 対象の文字列。 + /// 元の文字列から改行文字を取り除いた文字列。 + /// + /// です。 + /// + public static string RemoveNewLines(this string? target) + { + ArgumentNullException.ThrowIfNull(target); + return target.Replace("\n", string.Empty).Replace("\r", string.Empty); + } +} diff --git a/samples/Dressca/dressca-backend/tests/Dressca.UnitTests.SystemCommon/StringExtentionsTest.cs b/samples/Dressca/dressca-backend/tests/Dressca.UnitTests.SystemCommon/StringExtentionsTest.cs new file mode 100644 index 000000000..4b59108f7 --- /dev/null +++ b/samples/Dressca/dressca-backend/tests/Dressca.UnitTests.SystemCommon/StringExtentionsTest.cs @@ -0,0 +1,34 @@ +namespace Dressca.UnitTests.SystemCommon; + +public class StringExtentionsTest +{ + [Fact] + public void RemoveNewLines_null_ArgumentNullExceptionが発生する() + { + // Arrange + string? target = null; + + // Act + var action = () => StringExtentions.RemoveNewLines(target); + + // Assert + Assert.Throws("target", action); + } + + [Theory] + [InlineData("Line1\r\nLine2", "Line1Line2")] // CRとLFを含む場合 + [InlineData("Line1\rLine2", "Line1Line2")] // CRのみを含む場合 + [InlineData("Line1\nLine2", "Line1Line2")] // LFのみを含む場合 + [InlineData("", "")] // 空文字の場合 + [InlineData("Line1Line2", "Line1Line2")] // 改行文字を含まない場合 + public void RemoveNewLines_改行文字があれば取り除かれる(string input, string expected) + { + // Arrange + + // Act + var actual = input.RemoveNewLines(); + + // Assert + Assert.Equal(expected, actual); + } +}