Skip to content

Commit

Permalink
#1-3 テストを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
miyaji255 committed Apr 3, 2024
1 parent 659eaff commit 52e607f
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions KoeBook.Test/Epub/ScrapingAozoraServiceTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Runtime.CompilerServices;
using AngleSharp;
using AngleSharp.Dom;
using KoeBook.Epub.Models;
using KoeBook.Epub.Services;

namespace KoeBook.Test.Epub;
Expand All @@ -11,20 +12,51 @@ public class ScrapingAozoraServiceTest
[InlineData("", "")]
public async Task TextProcess(string input, string expected)
{
using var context = BrowsingContext.New(Configuration.Default.WithDefaultLoader());
using var context = BrowsingContext.New(Configuration.Default);
using var doc = await context.OpenAsync(req => req.Content(input));

Assert.NotNull(doc.ParentElement);
var result = ScrapingAozora.TextProcess(doc.ParentElement!);

var result = ScrapingAozora.TextProcess(null, doc.ParentElement!);

Assert.Equal(expected, result);
}

[Theory]
[InlineData("", new[] { "" })]
public async Task AddParagraphs1(string input, string[] expected)
{
using var context = BrowsingContext.New(Configuration.Default);
using var doc = await context.OpenAsync(req => req.Content(input));
Assert.NotNull(doc.ParentElement);
var epubDocument = new EpubDocument("title", "author", "", default)
{
Chapters = [new() { Sections = [new("section title") { Elements = [new Paragraph() { Text = "test" }] }] }]
};

Assert.Equal(expected.Length, epubDocument.Chapters[0].Sections[0].Elements.Count);
Assert.All(epubDocument.Chapters[0].Sections[0].Elements.Zip(expected), v =>
{
var (element, expected) = v;
var paragraph = Assert.IsType<Paragraph>(element);
Assert.Equal(expected, paragraph.Text);
});
}
}

file static class ScrapingAozora
{
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod)]
private static extern string TextProcess(ScrapingAozoraService? _, IElement element);
public static extern string TextProcess(ScrapingAozoraService? _, IElement element);

[UnsafeAccessor(UnsafeAccessorKind.Method)]
public static extern void AddParagraphs(ScrapingAozoraService service, List<KoeBook.Epub.Models.Element> focusElements, IElement element, bool lastEmpty);

[UnsafeAccessor(UnsafeAccessorKind.Method)]
public static extern void AddParagraphs(ScrapingAozoraService service, List<KoeBook.Epub.Models.Element> focusElements, string input, bool lastEmpty);

public static string TextProcess(IElement element) => TextProcess(null, element);
[UnsafeAccessor(UnsafeAccessorKind.StaticMethod)]
public static extern string TextReplace(ScrapingAozoraService? _, string text);

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

0 comments on commit 52e607f

Please sign in to comment.