Skip to content

Commit

Permalink
Merge pull request #10 from OUCC/feat/#4
Browse files Browse the repository at this point in the history
ScrapingHelper.SplitBraceの条件式の修正とテストの作成
  • Loading branch information
TakenPt authored Mar 3, 2024
2 parents d2b3d40 + 1307ed9 commit c5d4802
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
12 changes: 4 additions & 8 deletions Epub/KoeBook.Epub/Utility/ScrapingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static class ScrapingHelper
{
public static List<string> SplitBrace(string text)
{
if (text.Length == 1 && text != "" && text != "")
if (text.Length == 1 && (text == "" || text == ""))
return [text];

var bracket = 0;
Expand All @@ -23,25 +23,21 @@ public static List<string> 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;
}
Expand Down
44 changes: 44 additions & 0 deletions KoeBook.Test/Epub/ScrapingHelperTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using KoeBook.Epub.Utility;

namespace KoeBook.Test.Epub;

public class ScrapingHelperTest
{
public static object[][] TestCases()
{
(string, List<string>)[] 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<string> expected)
{
Assert.Equal(expected, ScrapingHelper.SplitBrace(text));

}
}
10 changes: 0 additions & 10 deletions KoeBook.Test/UnitTest1.cs

This file was deleted.

0 comments on commit c5d4802

Please sign in to comment.