Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#5-1 EpubDocumentのテストを作成中 #9

Merged
merged 4 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Epub/KoeBook.Epub/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using System.Runtime.CompilerServices;

// PrivateProxyが外部プロジェクトに対応していないため
[assembly: InternalsVisibleTo("KoeBook.Test")]
4 changes: 4 additions & 0 deletions KoeBook.Core/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using System.Runtime.CompilerServices;

// PrivateProxyが外部プロジェクトに対応していないため
[assembly: InternalsVisibleTo("KoeBook.Test")]
141 changes: 141 additions & 0 deletions KoeBook.Test/Epub/EpubDocumentTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using KoeBook.Epub.Models;

namespace KoeBook.Test.Epub;

public class EpubDocumentTest
{
#region Ensure List
[Fact]
public void EnsureChapter()
{
var document = new EpubDocument("title", "author", "cover", default);

Assert.Empty(document.Chapters);

// 空のときは追加
document.EnsureChapter();

var chapter = Assert.Single(document.Chapters);
Assert.Null(chapter.Title);
Assert.Empty(chapter.Sections);

// 空でないときは無視
document.EnsureChapter();

var chapter2 = Assert.Single(document.Chapters);
Assert.Same(chapter, chapter2);
}

[Fact]
public void EnsureSection()
{
var document = new EpubDocument("title", "author", "cover", default);

Assert.Empty(document.Chapters);

// 空のときは追加される
document.EnsureSection(0);

var chapter = Assert.Single(document.Chapters);
Assert.Null(chapter.Title);
var section = Assert.Single(chapter.Sections);
Assert.Equal("title", section.Title);
Assert.Empty(section.Elements);

// 空でないときは無視
document.Chapters = [
new()
{
Title = "chapter1",
Sections = [
new("section1"),
new("section2"),
new("section3"),
],
},
new()
{
Title = "chapter2",
Sections = [],
},
];

document.EnsureSection(0);

Assert.Equal(3, document.Chapters[0].Sections.Count);

document.EnsureSection(1);

Assert.Equal("chapter2", document.Chapters[1].Sections[0].Title);

// インデックスは正しく指定する必要がある
var exception = Record.Exception(() => document.EnsureSection(5));

Assert.IsType<ArgumentOutOfRangeException>(exception);
}

[Fact]
public void EnsureParagraph()
{
var document = new EpubDocument("title", "author", "cover", default);

Assert.Empty(document.Chapters);

// 空のときは追加される
document.EnsureParagraph(0, 0);

var chapter = Assert.Single(document.Chapters);
var section = Assert.Single(chapter.Sections);
var element = Assert.Single(section.Elements);
var paragraph = Assert.IsType<Paragraph>(element);
Assert.Null(paragraph.Audio);
Assert.Null(paragraph.Text);
Assert.Null(paragraph.ClassName);

// 空でないときは無視
document.Chapters = [
new()
{
Title = "chapter1",
Sections = [
new("section1")
{
Elements = [
new Paragraph()
{
Text = "paragraph1",
},
]
},
new("section1")
{
Elements = []
},
],
},
];

document.EnsureParagraph(0, 0);

chapter = Assert.Single(document.Chapters);
Assert.Equal(2, chapter.Sections.Count);
section = chapter.Sections[0];
element = Assert.Single(section.Elements);
paragraph = Assert.IsType<Paragraph>(element);
Assert.Equal("paragraph1", paragraph.Text);

document.EnsureParagraph(0, 1);

element = Assert.Single(document.Chapters[0].Sections[1].Elements);
paragraph = Assert.IsType<Paragraph>(element);
Assert.Null(paragraph.Audio);
Assert.Null(paragraph.Text);
Assert.Null(paragraph.ClassName);

// インデックスは正しく指定する必要がある
var exception = Record.Exception(() => document.EnsureParagraph(0, 5));

Assert.IsType<ArgumentOutOfRangeException>(exception);
}
#endregion
}
Loading