diff --git a/Epub/KoeBook.Epub/Utility/ScrapingHelper.cs b/Epub/KoeBook.Epub/Utility/ScrapingHelper.cs index ba8ff41..7b7a337 100644 --- a/Epub/KoeBook.Epub/Utility/ScrapingHelper.cs +++ b/Epub/KoeBook.Epub/Utility/ScrapingHelper.cs @@ -4,7 +4,7 @@ public static class ScrapingHelper { public static List SplitBrace(string text) { - if (text.Length == 1 && text != "「" && text != "」") + if (text.Length == 1 && (text == "「" || text == "」")) return [text]; var bracket = 0; @@ -23,25 +23,21 @@ public static List SplitBrace(string text) for (var i = 0; i < brackets.Length; i++) { brackets[i] -= mn; - if (text[i] == '「' && brackets[i] == 1 && i != 0) + if (text[i] == '「' && brackets[i] == 1 && i != 0 && startIdx != i) { result.Add(text[startIdx..i]); startIdx = i; } - if (text[i] == '」' && brackets[i] == 0 && i != 0) + if (text[i] == '」' && brackets[i] == 0) { result.Add(text[startIdx..(i + 1)]); startIdx = i + 1; } } - if (startIdx != text.Length - 1) + if (startIdx != text.Length) { result.Add(text[startIdx..]); } - if (result[^1] == "") - { - result.RemoveAt(result.Count - 1); - } return result; } diff --git a/KoeBook.Test/Epub/ScrapingHelperTest.cs b/KoeBook.Test/Epub/ScrapingHelperTest.cs new file mode 100644 index 0000000..394315e --- /dev/null +++ b/KoeBook.Test/Epub/ScrapingHelperTest.cs @@ -0,0 +1,44 @@ +using KoeBook.Epub.Utility; + +namespace KoeBook.Test.Epub; + +public class ScrapingHelperTest +{ + public static object[][] TestCases() + { + (string, List)[] cases = [ + ("「", ["「"]), + ("」", ["」"]), + ("a", ["a"]), + ("abc「abc」abc", ["abc", "「abc」", "abc"]), + ("abc「abc」", ["abc", "「abc」"]), + ("「abc」abc", ["「abc」", "abc",]), + ("abc「abc」", ["abc", "「abc」"]), + ("「abc」", ["「abc」",]), + ("abc「abc", ["abc", "「abc"]), + ("abc「", ["abc", "「"]), + ("「abc", ["「abc"]), + ("abc」abc", ["abc」", "abc"]), + ("abc」", ["abc」"]), + ("」abc", ["」", "abc"]), + ("abc「abc」abc「abc」abc", ["abc", "「abc」", "abc", "「abc」", "abc"]), + ("「abc」abc「abc」abc", ["「abc」", "abc", "「abc」", "abc"]), + ("abc「abc」「abc」abc", ["abc", "「abc」", "「abc」", "abc"]), + ("abc「abc」abc「abc」", ["abc", "「abc」", "abc", "「abc」"]), + ("abc「abc「abc」abc」abc", ["abc", "「abc「abc」abc」", "abc"]), + ("abc「abc「abc」abc", ["abc", "「abc「abc」abc"]), + ("abc「abc」abc」abc", ["abc「abc」abc」", "abc"]), + ("abc「abc「abc", ["abc", "「abc「abc"]), + ("abc」abc」abc", ["abc」abc」", "abc"]) + ]; + return cases.Select(c => new object[] { c.Item1, c.Item2 }).ToArray(); + } + + [Theory] + [MemberData(nameof(TestCases))] + public void SplitBraceTest(string text, List expected) + { + Assert.Equal(expected, ScrapingHelper.SplitBrace(text)); + + } +} diff --git a/KoeBook.Test/UnitTest1.cs b/KoeBook.Test/UnitTest1.cs deleted file mode 100644 index 217ca16..0000000 --- a/KoeBook.Test/UnitTest1.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace KoeBook.Test; - -public class UnitTest1 -{ - [Fact] - public void Test1() - { - - } -}