Skip to content

Commit

Permalink
#13 テストの追加と名前の修正
Browse files Browse the repository at this point in the history
  • Loading branch information
miyaji255 committed Mar 30, 2024
1 parent d1cd9d7 commit 3c68caa
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 110 deletions.
14 changes: 7 additions & 7 deletions Epub/KoeBook.Epub/Services/ScrapingNaroService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using AngleSharp.Html.Dom;
using AngleSharp.Io;
using KoeBook.Core;
using KoeBook.Core.Utility;
using KoeBook.Core.Utilities;
using KoeBook.Epub.Contracts.Services;
using KoeBook.Epub.Models;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -138,7 +138,7 @@ private record SectionWithChapterTitle(string? title, Section section);

private async ValueTask<SectionWithChapterTitle> ReadPageAsync(string url, bool isRensai, string imageDirectory, CancellationToken ct)
{
var store = new StringStoreBuilder();
var store = new SplittedLineBuilder();

var config = Configuration.Default.WithDefaultLoader();
using var context = BrowsingContext.New(config);
Expand Down Expand Up @@ -186,7 +186,7 @@ private async ValueTask<SectionWithChapterTitle> ReadPageAsync(string url, bool
{
if (!string.IsNullOrWhiteSpace(item.InnerHtml))
{
store.Store(item.InnerHtml);
store.Append(item.InnerHtml);
}
}
else if (item.ChildElementCount == 1)
Expand Down Expand Up @@ -220,12 +220,12 @@ private async ValueTask<SectionWithChapterTitle> ReadPageAsync(string url, bool
{
if (!string.IsNullOrWhiteSpace(item.InnerHtml))
{
store.Store(item.InnerHtml);
store.Append(item.InnerHtml);
}
}
else if (item.Children[0] is IHtmlBreakRowElement)
{
foreach (var split in _splitBraceService.SplitBrace(store.Release()))
foreach (var split in _splitBraceService.SplitBrace(store.ToLinesAndClear()))
{
section.Elements.Add(new Paragraph() { Text = split });
}
Expand All @@ -250,10 +250,10 @@ private async ValueTask<SectionWithChapterTitle> ReadPageAsync(string url, bool

if (!string.IsNullOrWhiteSpace(item.InnerHtml))
{
store.Store(item.InnerHtml);
store.Append(item.InnerHtml);
}
}
foreach (var split in _splitBraceService.SplitBrace(store.Release()))
foreach (var split in _splitBraceService.SplitBrace(store.ToLinesAndClear()))
{
section.Elements.Add(new Paragraph() { Text = split });
}
Expand Down
23 changes: 23 additions & 0 deletions KoeBook.Core/Utilities/EnumerableEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace KoeBook.Core.Utilities;

public static class EnumerableEx
{
public static IEnumerable<(TSource value, bool isFirst, bool isLast)> WithPosition<TSource>(this IEnumerable<TSource> source)
{
using var enumerator = source.GetEnumerator();

var hasNext = enumerator.MoveNext();
if (!hasNext)
yield break;
var current = enumerator.Current;
hasNext = enumerator.MoveNext();
yield return (current, true, !hasNext);

while (hasNext)
{
current = enumerator.Current;
hasNext = enumerator.MoveNext();
yield return (current, false, !hasNext);
}
}
}
49 changes: 49 additions & 0 deletions KoeBook.Core/Utilities/SplittedLineBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Text;

namespace KoeBook.Core.Utilities;

public class SplittedLineBuilder
{
private readonly StringBuilder _stringBuilder = new();
private readonly List<string> _texts = [];

public void Append(string text)
{
_stringBuilder.Append(text);
}

/// <summary>
/// すでに分割済みの行を追加します
/// </summary>
/// <param name="lines"></param>
public void Append(IEnumerable<string> lines)
{
foreach (var (value, isFirst, isLast) in lines.WithPosition())
{
if (isLast)
{
_stringBuilder.Append(value);

}
else if (isFirst)
{
_stringBuilder.Append(value);
_texts.Add(_stringBuilder.ToString());
_stringBuilder.Clear();
}
else
{
_texts.Add(value);
}
}
}

public string[] ToLinesAndClear()
{
var result = _stringBuilder.Length == 0 ? _texts.ToArray() : [.. _texts, _stringBuilder.ToString()];

_stringBuilder.Clear();
_texts.Clear();
return result;
}
}
58 changes: 0 additions & 58 deletions KoeBook.Core/Utility/EnumerableEx.cs

This file was deleted.

45 changes: 0 additions & 45 deletions KoeBook.Core/Utility/StringStoreBuilder.cs

This file was deleted.

30 changes: 30 additions & 0 deletions KoeBook.Test/Utilities/EnumerableExTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using KoeBook.Core.Utilities;

namespace KoeBook.Test.Utilities;

public class EnumerableExTest
{
[Theory]
[InlineData(new[] { 0 })]
[InlineData(new[] { 0, 1 })]
[InlineData(new[] { 0, 1, 2 })]
[InlineData(new[] { 0, 1, 2, 3 })]
public void WithPosition(int[] input)
{
var result = input.WithPosition();

Assert.Equal(input, result.Select(v => v.value));
Assert.True(result.First().isFirst);
Assert.All(result.Skip(1), (v) => Assert.False(v.isFirst));
Assert.True(result.Last().isLast);
Assert.All(result.SkipLast(1), (v) => Assert.False(v.isLast));
}

[Fact]
public void WithPosition_Empty()
{
var result = Array.Empty<int>().WithPosition();

Assert.Empty(result);
}
}
76 changes: 76 additions & 0 deletions KoeBook.Test/Utilities/SplittedLineBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Runtime.CompilerServices;
using System.Text;
using KoeBook.Core.Utilities;

namespace KoeBook.Test.Utilities
{
public class SplittedLineBuilderTest
{
[Fact]
public void Append_Single()
{
var builder = new SplittedLineBuilder();

builder.Append("test");

Assert.Equal("test", builder._stringBuilder().ToString());
Assert.Empty(builder._texts());

builder._texts().Add("test1");

builder.Append("test2");

Assert.Equal("testtest2", builder._stringBuilder().ToString());
Assert.Equal(["test1"], builder._texts());
}

[Theory]
[InlineData(new string[] { }, "builder", new string[] { })]
[InlineData(new[] { "1" }, "builder1", new string[] { })]
[InlineData(new[] { "1", "2" }, "2", new[] { "builder1" })]
[InlineData(new[] { "1", "2", "3" }, "3", new[] { "builder1", "2" })]
public void Append_Multiple(string[] input, string expectedBuilder, string[] expectedTexts)
{
var builder = new SplittedLineBuilder();
builder._stringBuilder().Append("builder");

builder.Append(input);

Assert.Equal(expectedBuilder, builder._stringBuilder().ToString());
Assert.Equal(expectedTexts, builder._texts());
}

[Theory]
[InlineData("", new string[] { }, new string[] { })]
[InlineData("", new[] { "test1" }, new[] { "test1" })]
[InlineData("", new[] { "test1", "test2" }, new[] { "test1", "test2" })]
[InlineData("", new[] { "test1", "test2", "test3" }, new[] { "test1", "test2", "test3" })]
[InlineData("test0", new string[] { }, new[] { "test0" })]
[InlineData("test0", new[] { "test1" }, new[] { "test1", "test0" })]
[InlineData("test0", new[] { "test1", "test2" }, new[] { "test1", "test2", "test0" })]
[InlineData("test0", new[] { "test1", "test2", "test3" }, new[] { "test1", "test2", "test3", "test0" })]
public void ToLinesAndClear(string builderStr, string[] texts, string[] expected)
{
var builder = new SplittedLineBuilder();
builder._stringBuilder().Append(builderStr);
builder._texts().AddRange(texts);

var result = builder.ToLinesAndClear();

// 空になったかの判定はenumerateする前に行う
Assert.Empty(builder._texts());
Assert.Equal(0, builder._stringBuilder().Length);
Assert.Equal(expected, result);
}
}
}


file static class Proxy
{
[UnsafeAccessor(UnsafeAccessorKind.Field)]
public static extern ref StringBuilder _stringBuilder(this SplittedLineBuilder builder);

[UnsafeAccessor(UnsafeAccessorKind.Field)]
public static extern ref List<string> _texts(this SplittedLineBuilder builder);
}

0 comments on commit 3c68caa

Please sign in to comment.