Skip to content

Commit

Permalink
EpubReader unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vers-one committed Sep 17, 2022
1 parent cccd1c4 commit 76a1492
Show file tree
Hide file tree
Showing 11 changed files with 1,359 additions and 45 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

[![Build](https://github.com/vers-one/EpubReader/actions/workflows/build.yml/badge.svg)](https://github.com/vers-one/EpubReader/actions/workflows/build.yml)
[![Tests](https://github.com/vers-one/EpubReader/actions/workflows/test.yml/badge.svg)](https://github.com/vers-one/EpubReader/actions/workflows/test.yml)
[![Test coverage](https://codecov.io/gh/vers-one/EpubReader/branch/master/graph/badge.svg?token=19RoYjPa56)](https://codecov.io/gh/vers-one/EpubReader)
[![NuGet](https://img.shields.io/nuget/v/VersOne.Epub.svg)](https://www.nuget.org/packages/VersOne.Epub)

Supported EPUB standards:
Expand Down
21 changes: 14 additions & 7 deletions Source/VersOne.Epub.Test/Unit/Comparers/Epub2NcxComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ internal static class Epub2NcxComparer
{
public static void CompareEpub2Ncxes(Epub2Ncx expected, Epub2Ncx actual)
{
Assert.NotNull(actual);
CompareEpub2NcxHeads(expected.Head, actual.Head);
Assert.Equal(expected.DocTitle, actual.DocTitle);
Assert.Equal(expected.DocAuthors, actual.DocAuthors);
CompareEpub2NcxNavigationMaps(expected.NavMap, actual.NavMap);
CompareEpub2NcxPageLists(expected.PageList, actual.PageList);
AssertUtils.CollectionsEqual(expected.NavLists, actual.NavLists, CompareEpub2NcxNavigationLists);
if (expected == null)
{
Assert.Null(actual);
}
else
{
Assert.NotNull(actual);
CompareEpub2NcxHeads(expected.Head, actual.Head);
Assert.Equal(expected.DocTitle, actual.DocTitle);
Assert.Equal(expected.DocAuthors, actual.DocAuthors);
CompareEpub2NcxNavigationMaps(expected.NavMap, actual.NavMap);
CompareEpub2NcxPageLists(expected.PageList, actual.PageList);
AssertUtils.CollectionsEqual(expected.NavLists, actual.NavLists, CompareEpub2NcxNavigationLists);
}
}

private static void CompareEpub2NcxHeads(Epub2NcxHead expected, Epub2NcxHead actual)
Expand Down
22 changes: 22 additions & 0 deletions Source/VersOne.Epub.Test/Unit/Comparers/EpubBookComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using VersOne.Epub.Test.Unit.TestUtils;

namespace VersOne.Epub.Test.Unit.Comparers
{
internal static class EpubBookComparer
{
public static void CompareEpubBooks(EpubBook expected, EpubBook actual)
{
Assert.NotNull(actual);
Assert.Equal(expected.FilePath, actual.FilePath);
Assert.Equal(expected.Title, actual.Title);
Assert.Equal(expected.Author, actual.Author);
Assert.Equal(expected.AuthorList, actual.AuthorList);
Assert.Equal(expected.Description, actual.Description);
Assert.Equal(expected.CoverImage, actual.CoverImage);
AssertUtils.CollectionsEqual(expected.ReadingOrder, actual.ReadingOrder, EpubContentComparer.CompareEpubTextContentFiles);
EpubNavigationItemComparer.CompareNavigationItemLists(expected.Navigation, actual.Navigation);
EpubContentComparer.CompareEpubContents(expected.Content, actual.Content);
EpubSchemaComparer.CompareEpubSchemas(expected.Schema, actual.Schema);
}
}
}
18 changes: 18 additions & 0 deletions Source/VersOne.Epub.Test/Unit/Comparers/EpubBookRefComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace VersOne.Epub.Test.Unit.Comparers
{
internal static class EpubBookRefComparer
{
public static void CompareEpubBookRefs(EpubBookRef expected, EpubBookRef actual)
{
Assert.NotNull(actual);
Assert.Equal(expected.FilePath, actual.FilePath);
Assert.Equal(expected.Title, actual.Title);
Assert.Equal(expected.Author, actual.Author);
Assert.Equal(expected.AuthorList, actual.AuthorList);
Assert.Equal(expected.Description, actual.Description);
EpubSchemaComparer.CompareEpubSchemas(expected.Schema, actual.Schema);
EpubContentRefComparer.CompareEpubContentRefs(expected.Content, actual.Content);
Assert.Equal(expected.EpubFile, actual.EpubFile);
}
}
}
58 changes: 58 additions & 0 deletions Source/VersOne.Epub.Test/Unit/Comparers/EpubContentComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using VersOne.Epub.Test.Unit.TestUtils;

namespace VersOne.Epub.Test.Unit.Comparers
{
internal static class EpubContentComparer
{
public static void CompareEpubContents(EpubContent expected, EpubContent actual)
{
CompareEpubContentFiles(expected.Cover, actual.Cover);
CompareEpubContentFiles(expected.NavigationHtmlFile, actual.NavigationHtmlFile);
AssertUtils.DictionariesEqual(expected.Html, actual.Html, CompareEpubTextContentFiles);
AssertUtils.DictionariesEqual(expected.Css, actual.Css, CompareEpubTextContentFiles);
AssertUtils.DictionariesEqual(expected.Images, actual.Images, CompareEpubByteContentFiles);
AssertUtils.DictionariesEqual(expected.Fonts, actual.Fonts, CompareEpubByteContentFiles);
AssertUtils.DictionariesEqual(expected.AllFiles, actual.AllFiles, CompareEpubContentFilesWithContent);
}

public static void CompareEpubTextContentFiles(EpubTextContentFile expected, EpubTextContentFile actual)
{
CompareEpubContentFiles(expected, actual);
Assert.Equal(expected.Content, actual.Content);
}

public static void CompareEpubByteContentFiles(EpubByteContentFile expected, EpubByteContentFile actual)
{
CompareEpubContentFiles(expected, actual);
Assert.Equal(expected.Content, actual.Content);
}

private static void CompareEpubContentFilesWithContent(EpubContentFile expected, EpubContentFile actual)
{
if (expected is EpubTextContentFile)
{
CompareEpubTextContentFiles(expected as EpubTextContentFile, actual as EpubTextContentFile);
}
else if (expected is EpubByteContentFile)
{
CompareEpubByteContentFiles(expected as EpubByteContentFile, actual as EpubByteContentFile);
}
}

private static void CompareEpubContentFiles(EpubContentFile expected, EpubContentFile actual)
{
if (expected == null)
{
Assert.Null(actual);
}
else
{
Assert.NotNull(actual);
Assert.Equal(expected.FileName, actual.FileName);
Assert.Equal(expected.FilePathInEpubArchive, actual.FilePathInEpubArchive);
Assert.Equal(expected.ContentType, actual.ContentType);
Assert.Equal(expected.ContentMimeType, actual.ContentMimeType);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using VersOne.Epub.Test.Unit.TestUtils;

namespace VersOne.Epub.Test.Unit.Comparers
{
internal static class EpubNavigationItemComparer
{
public static void CompareNavigationItemLists(List<EpubNavigationItem> expected, List<EpubNavigationItem> actual)
{
if (expected == null)
{
Assert.Null(actual);
}
else
{
Assert.NotNull(actual);
AssertUtils.CollectionsEqual(expected, actual, CompareNavigationItems);
}
}

public static void CompareNavigationItemLinks(EpubNavigationItemLink expected, EpubNavigationItemLink actual)
{
if (expected == null)
{
Assert.Null(actual);
}
else
{
Assert.NotNull(actual);
Assert.Equal(expected.ContentFileName, actual.ContentFileName);
Assert.Equal(expected.ContentFilePathInEpubArchive, actual.ContentFilePathInEpubArchive);
Assert.Equal(expected.Anchor, actual.Anchor);
}
}

private static void CompareNavigationItems(EpubNavigationItem expected, EpubNavigationItem actual)
{
Assert.NotNull(actual);
Assert.Equal(expected.Type, actual.Type);
Assert.Equal(expected.Title, actual.Title);
CompareNavigationItemLinks(expected.Link, actual.Link);
EpubContentComparer.CompareEpubTextContentFiles(expected.HtmlContentFile, actual.HtmlContentFile);
CompareNavigationItemLists(expected.NestedItems, actual.NestedItems);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,9 @@ private static void CompareNavigationItemRefs(EpubNavigationItemRef expected, Ep
Assert.NotNull(actual);
Assert.Equal(expected.Type, actual.Type);
Assert.Equal(expected.Title, actual.Title);
CompareNavigationItemLinks(expected.Link, actual.Link);
EpubNavigationItemComparer.CompareNavigationItemLinks(expected.Link, actual.Link);
Assert.Equal(expected.HtmlContentFileRef, actual.HtmlContentFileRef);
CompareNavigationItemRefLists(expected.NestedItems, actual.NestedItems);
}

private static void CompareNavigationItemLinks(EpubNavigationItemLink expected, EpubNavigationItemLink actual)
{
if (expected == null)
{
Assert.Null(actual);
}
else
{
Assert.NotNull(actual);
Assert.Equal(expected.ContentFileName, actual.ContentFileName);
Assert.Equal(expected.ContentFilePathInEpubArchive, actual.ContentFilePathInEpubArchive);
Assert.Equal(expected.Anchor, actual.Anchor);
}
}
}
}
41 changes: 41 additions & 0 deletions Source/VersOne.Epub.Test/Unit/Mocks/TestFileSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using VersOne.Epub.Environment;

namespace VersOne.Epub.Test.Unit.Mocks
{
internal class TestFileSystem : IFileSystem
{
private readonly Dictionary<string, TestZipFile> testZipFilesByPath;
private readonly Dictionary<Stream, TestZipFile> testZipFilesByStream;

public TestFileSystem()
{
testZipFilesByPath = new Dictionary<string, TestZipFile>();
testZipFilesByStream = new Dictionary<Stream, TestZipFile>();
}

public void AddTestZipFile(string path, TestZipFile testZipFile)
{
testZipFilesByPath.Add(path, testZipFile);
}

public void AddTestZipFile(Stream stream, TestZipFile testZipFile)
{
testZipFilesByStream.Add(stream, testZipFile);
}

public bool FileExists(string path)
{
return testZipFilesByPath.ContainsKey(path);
}

public IZipFile OpenZipFile(string path)
{
return testZipFilesByPath[path];
}

public IZipFile OpenZipFile(Stream stream)
{
return testZipFilesByStream[stream];
}
}
}
Loading

0 comments on commit 76a1492

Please sign in to comment.