Skip to content

Commit

Permalink
#23 テストの作成
Browse files Browse the repository at this point in the history
  • Loading branch information
TakenPt committed Apr 8, 2024
1 parent 703851c commit 214bbc8
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 19 deletions.
15 changes: 15 additions & 0 deletions Epub/KoeBook.Epub/Services/ScrapingAozoraService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,21 @@ private static string GetCardUrl(string url)
return UrlBookToCard().Replace(url, "$1card$2$3");
}

private SplittedLineBuilder ParagraphLineBuilder = new SplittedLineBuilder();
private SplittedLineBuilder ScriptLineLineBuilder = new SplittedLineBuilder();



/// <summary>
/// ある要素のChildrenに応じた処理を行います。
/// </summary>
/// <param name="element">処理を行う要素</param>
internal void ProcessChildren(IElement element)
{

}


[System.Text.RegularExpressions.GeneratedRegex(@"(https://www\.aozora\.gr\.jp/cards/\d{6}/)files/(\d{1,})_\d{1,}(\.html)")]
private static partial System.Text.RegularExpressions.Regex UrlBookToCard();

Expand Down
95 changes: 76 additions & 19 deletions KoeBook.Test/Epub/ScrapingAozoraServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,91 @@
using KoeBook.Epub.Services;
using System.Runtime.CompilerServices;
using System.Linq;
using System.Net.Http;

namespace KoeBook.Test.Epub;

public class ScrapingAozoraServiceTest
{
private static readonly EpubDocument EmptySingleParagraph = new EpubDocument("", "", "", Guid.NewGuid()) { Chapters = [new Chapter() { Sections = [new Section("") { Elements = [new Paragraph()] }] }] };
private static readonly EpubDocument EmptySingleParagraph = new EpubDocument("", "", "", Guid.NewGuid()) { Chapters = [new Chapter() { Sections = [new Section("") { Elements = [new Paragraph()] }] }] };

public static object[][] TestCases()
public static object[][] ProcessChildrenTestCases()
{
(string, EpubDocument, EpubDocument)[] cases = [
// レイアウト
// 1.1 改丁
(ToMainText(@"<span class=""notes"">[#改丁]</span>"), EmptySingleParagraph , new EpubDocument("", "", "", Guid.NewGuid()) { Chapters = [new Chapter() { Sections = [new Section("") { Elements = [new Paragraph() { Text = "[#改丁]", ScriptLine = new Core.Models.ScriptLine("", "", "") }] }] }] }),
];
return cases.Select(c => new object[] { c.Item1, c.Item2 }).ToArray();
}

/// <summary>
/// を"<div class = \"main_text\"></div>"で囲む
/// </summary>
/// <param name="text">divタグで囲むhtmlの要素</param>
/// <returns>divタグで囲まれた<paramref name="text"/></returns>
private static string ToMainText(string text)
{
return @$"<div class = ""main_text"">{text}</div>";
}

[Theory]
[MemberData(nameof(ProcessChildrenTestCases))]
public async void ProcessChildrenTest(string html, EpubDocument initial, EpubDocument expexted)
{
var config = Configuration.Default.WithDefaultLoader();
using var context = BrowsingContext.New(config);
var doc = await context.OpenAsync(request => request.Content(html));
var mainText = doc.QuerySelector(".main_text");
var scraper = new ScrapingAozoraService(new SplitBraceService(), new ScrapingClientService(new httpClientFactory(), TimeProvider.System));
scraper._document() = initial;

scraper.ProcessChildren(mainText);

Assert.True(HaveSmaeText(scraper._document(), expexted));
}

/// <summary>
/// 2つのEpubdocumentの内容(Guidを除く)内容が一致するかを判定する。
/// </summary>
/// <param name="document">比較するEpubdocument</param>
/// <param name="comparison">比較するEpubdocument</param>
/// <returns></returns>
private static bool HaveSmaeText(EpubDocument document, EpubDocument comparison)
{
bool same = true;

same = (document.Title == comparison.Title);
same = (document.Author == comparison.Author);
same = (document.CssClasses == comparison.CssClasses);

foreach ((Chapter selfChapter, Chapter comparisonChapter) in document.Chapters.Zip(comparison.Chapters))
{
(string, EpubDocument, EpubDocument)[] cases = [
// レイアウト
// 1.1 改丁
(ToMainText(@"<span class=""notes"">[#改丁]</span>"), EmptySingleParagraph , new EpubDocument("", "", "", Guid.NewGuid()) { Chapters = [new Chapter() { Sections = [new Section("") { Elements = [new Paragraph() { Text = "[#改丁]", ScriptLine = new Core.Models.ScriptLine("", "", "") }] }] }] }),
];
return cases.Select(c => new object[] { c.Item1, c.Item2 }).ToArray();
same = (selfChapter.Title == comparisonChapter.Title);

foreach ((Section selfSection, Section comparisonSection) in selfChapter.Sections.Zip(comparisonChapter.Sections))
{
same = (selfSection.Title == comparisonSection.Title);

same = selfSection.Elements.Equals(comparisonSection.Elements);
}
}

/// <summary>
/// を"<div class = \"main_text\"></div>"で囲む
/// </summary>
/// <param name="text">divタグで囲むhtmlの要素</param>
/// <returns>divタグで囲まれた<paramref name="text"/></returns>
private static string ToMainText(string text)
return same;
}

internal class httpClientFactory : IHttpClientFactory
{
public HttpClient CreateClient(string name)
{
var builder = new StringBuilder();
builder.Append(@"<div class = ""main_text"">");
builder.Append(text);
builder.Append("</div>");
return builder.ToString();
return httpClient;
}

private static readonly HttpClient httpClient = new HttpClient();

}


[Theory]
[InlineData("", "")]
public async Task TextProcess(string input, string expected)
Expand Down Expand Up @@ -87,4 +141,7 @@ file static class ScrapingAozora

[UnsafeAccessor(UnsafeAccessorKind.StaticMethod)]
public static extern (List<int> contentsIds, bool hasChapter, bool hasSection) LoadToc(ScrapingAozoraService? _, IDocument doc, EpubDocument epubDocument);

[UnsafeAccessor(UnsafeAccessorKind.Field)]
public static extern ref EpubDocument _document(this ScrapingAozoraService scraper);
}

0 comments on commit 214bbc8

Please sign in to comment.