Skip to content

Commit

Permalink
Decouple EpubContentFileRef from EpubBookRef, part 2 (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
vers-one authored Oct 25, 2022
1 parent a700f39 commit 0b0993e
Show file tree
Hide file tree
Showing 16 changed files with 266 additions and 225 deletions.
6 changes: 5 additions & 1 deletion Source/VersOne.Epub.Test/Unit/Mocks/TestZipFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ namespace VersOne.Epub.Test.Unit.Mocks
{
internal class TestZipFile : IZipFile
{
readonly Dictionary<string, IZipFileEntry> entries;
private readonly Dictionary<string, IZipFileEntry> entries;

public TestZipFile()
{
entries = new Dictionary<string, IZipFileEntry>();
IsDisposed = false;
}

public bool IsDisposed { get; private set; }

public void AddEntry(string entryName, IZipFileEntry entry)
{
entries.Add(entryName, entry);
Expand All @@ -33,6 +36,7 @@ public IZipFileEntry GetEntry(string entryName)

public void Dispose()
{
IsDisposed = true;
}
}
}
4 changes: 2 additions & 2 deletions Source/VersOne.Epub.Test/Unit/Readers/BookCoverReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ private EpubBookRef CreateEmptyEpubBookRef(EpubVersion epubVersion)
};
}

private EpubByteContentFileRef CreateTestImageFileRef()
private EpubByteContentFileRef CreateTestImageFileRef(TestZipFile testZipFile = null)
{
return new("cover.jpg", EpubContentLocation.LOCAL, EpubContentType.IMAGE_JPEG, "image/jpeg", String.Empty);
return new("cover.jpg", EpubContentLocation.LOCAL, EpubContentType.IMAGE_JPEG, "image/jpeg", testZipFile ?? new TestZipFile(), String.Empty);
}

private Dictionary<string, EpubByteContentFileRef> CreateImageContentRefs(EpubByteContentFileRef imageFileRef)
Expand Down
57 changes: 29 additions & 28 deletions Source/VersOne.Epub.Test/Unit/Readers/ContentReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ContentReaderTests
[Fact(DisplayName = "Parsing content map from a minimal EPUB book ref should succeed")]
public void ParseContentMapWithMinimalEpubBookRefTest()
{
EpubBookRef epubBookRef = CreateEmptyEpubBookRef();
EpubBookRef epubBookRef = CreateEmptyEpubBookRef(new TestZipFile());
EpubContentRef expectedContentMap = new()
{
Html = new Dictionary<string, EpubTextContentFileRef>(),
Expand All @@ -29,7 +29,8 @@ public void ParseContentMapWithMinimalEpubBookRefTest()
[Fact(DisplayName = "Parsing content map from a full EPUB book ref should succeed")]
public void ParseContentMapWithFullEpubBookRefTest()
{
EpubBookRef epubBookRef = CreateEmptyEpubBookRef();
TestZipFile testZipFile = new();
EpubBookRef epubBookRef = CreateEmptyEpubBookRef(testZipFile);
epubBookRef.Schema.Package.Manifest = new EpubManifest()
{
Items = new List<EpubManifestItem>()
Expand Down Expand Up @@ -164,28 +165,28 @@ public void ParseContentMapWithFullEpubBookRefTest()
}
}
};
EpubTextContentFileRef expectedFileRef1 = CreateLocalTextFileRef("text.html", EpubContentType.XHTML_1_1, "application/xhtml+xml");
EpubTextContentFileRef expectedFileRef2 = CreateLocalTextFileRef("doc.dtb", EpubContentType.DTBOOK, "application/x-dtbook+xml");
EpubTextContentFileRef expectedFileRef3 = CreateLocalTextFileRef("toc.ncx", EpubContentType.DTBOOK_NCX, "application/x-dtbncx+xml");
EpubTextContentFileRef expectedFileRef4 = CreateLocalTextFileRef("oeb.html", EpubContentType.OEB1_DOCUMENT, "text/x-oeb1-document");
EpubTextContentFileRef expectedFileRef5 = CreateLocalTextFileRef("file.xml", EpubContentType.XML, "application/xml");
EpubTextContentFileRef expectedFileRef6 = CreateLocalTextFileRef("styles.css", EpubContentType.CSS, "text/css");
EpubTextContentFileRef expectedFileRef7 = CreateLocalTextFileRef("oeb.css", EpubContentType.OEB1_CSS, "text/x-oeb1-css");
EpubByteContentFileRef expectedFileRef8 = CreateLocalByteFileRef("image1.gif", EpubContentType.IMAGE_GIF, "image/gif");
EpubByteContentFileRef expectedFileRef9 = CreateLocalByteFileRef("image2.jpg", EpubContentType.IMAGE_JPEG, "image/jpeg");
EpubByteContentFileRef expectedFileRef10 = CreateLocalByteFileRef("image3.png", EpubContentType.IMAGE_PNG, "image/png");
EpubByteContentFileRef expectedFileRef11 = CreateLocalByteFileRef("image4.svg", EpubContentType.IMAGE_SVG, "image/svg+xml");
EpubByteContentFileRef expectedFileRef12 = CreateLocalByteFileRef("font1.ttf", EpubContentType.FONT_TRUETYPE, "font/truetype");
EpubByteContentFileRef expectedFileRef13 = CreateLocalByteFileRef("font2.ttf", EpubContentType.FONT_TRUETYPE, "application/x-font-truetype");
EpubByteContentFileRef expectedFileRef14 = CreateLocalByteFileRef("font3.otf", EpubContentType.FONT_OPENTYPE, "font/opentype");
EpubByteContentFileRef expectedFileRef15 = CreateLocalByteFileRef("font4.otf", EpubContentType.FONT_OPENTYPE, "application/vnd.ms-opentype");
EpubByteContentFileRef expectedFileRef16 = CreateLocalByteFileRef("video.mp4", EpubContentType.OTHER, "video/mp4");
EpubByteContentFileRef expectedFileRef17 = CreateLocalByteFileRef("cover.jpg", EpubContentType.IMAGE_JPEG, "image/jpeg");
EpubTextContentFileRef expectedFileRef18 = CreateLocalTextFileRef("toc.html", EpubContentType.XHTML_1_1, "application/xhtml+xml");
EpubTextContentFileRef expectedFileRef1 = CreateLocalTextFileRef(testZipFile, "text.html", EpubContentType.XHTML_1_1, "application/xhtml+xml");
EpubTextContentFileRef expectedFileRef2 = CreateLocalTextFileRef(testZipFile, "doc.dtb", EpubContentType.DTBOOK, "application/x-dtbook+xml");
EpubTextContentFileRef expectedFileRef3 = CreateLocalTextFileRef(testZipFile, "toc.ncx", EpubContentType.DTBOOK_NCX, "application/x-dtbncx+xml");
EpubTextContentFileRef expectedFileRef4 = CreateLocalTextFileRef(testZipFile, "oeb.html", EpubContentType.OEB1_DOCUMENT, "text/x-oeb1-document");
EpubTextContentFileRef expectedFileRef5 = CreateLocalTextFileRef(testZipFile, "file.xml", EpubContentType.XML, "application/xml");
EpubTextContentFileRef expectedFileRef6 = CreateLocalTextFileRef(testZipFile, "styles.css", EpubContentType.CSS, "text/css");
EpubTextContentFileRef expectedFileRef7 = CreateLocalTextFileRef(testZipFile, "oeb.css", EpubContentType.OEB1_CSS, "text/x-oeb1-css");
EpubByteContentFileRef expectedFileRef8 = CreateLocalByteFileRef(testZipFile, "image1.gif", EpubContentType.IMAGE_GIF, "image/gif");
EpubByteContentFileRef expectedFileRef9 = CreateLocalByteFileRef(testZipFile, "image2.jpg", EpubContentType.IMAGE_JPEG, "image/jpeg");
EpubByteContentFileRef expectedFileRef10 = CreateLocalByteFileRef(testZipFile, "image3.png", EpubContentType.IMAGE_PNG, "image/png");
EpubByteContentFileRef expectedFileRef11 = CreateLocalByteFileRef(testZipFile, "image4.svg", EpubContentType.IMAGE_SVG, "image/svg+xml");
EpubByteContentFileRef expectedFileRef12 = CreateLocalByteFileRef(testZipFile, "font1.ttf", EpubContentType.FONT_TRUETYPE, "font/truetype");
EpubByteContentFileRef expectedFileRef13 = CreateLocalByteFileRef(testZipFile, "font2.ttf", EpubContentType.FONT_TRUETYPE, "application/x-font-truetype");
EpubByteContentFileRef expectedFileRef14 = CreateLocalByteFileRef(testZipFile, "font3.otf", EpubContentType.FONT_OPENTYPE, "font/opentype");
EpubByteContentFileRef expectedFileRef15 = CreateLocalByteFileRef(testZipFile, "font4.otf", EpubContentType.FONT_OPENTYPE, "application/vnd.ms-opentype");
EpubByteContentFileRef expectedFileRef16 = CreateLocalByteFileRef(testZipFile, "video.mp4", EpubContentType.OTHER, "video/mp4");
EpubByteContentFileRef expectedFileRef17 = CreateLocalByteFileRef(testZipFile, "cover.jpg", EpubContentType.IMAGE_JPEG, "image/jpeg");
EpubTextContentFileRef expectedFileRef18 = CreateLocalTextFileRef(testZipFile, "toc.html", EpubContentType.XHTML_1_1, "application/xhtml+xml");
EpubTextContentFileRef expectedFileRef19 =
new("https://example.com/books/123/test.html", EpubContentLocation.REMOTE, EpubContentType.XHTML_1_1, "application/xhtml+xml", String.Empty);
new("https://example.com/books/123/test.html", EpubContentLocation.REMOTE, EpubContentType.XHTML_1_1, "application/xhtml+xml", testZipFile, String.Empty);
EpubByteContentFileRef expectedFileRef20 =
new("https://example.com/books/123/image.jpg", EpubContentLocation.REMOTE, EpubContentType.IMAGE_JPEG, "image/jpeg", String.Empty);
new("https://example.com/books/123/image.jpg", EpubContentLocation.REMOTE, EpubContentType.IMAGE_JPEG, "image/jpeg", testZipFile, String.Empty);
EpubContentRef expectedContentMap = new()
{
Html = new Dictionary<string, EpubTextContentFileRef>()
Expand Down Expand Up @@ -346,9 +347,9 @@ public void ParseContentMapWithFullEpubBookRefTest()
EpubContentRefComparer.CompareEpubContentRefs(expectedContentMap, actualContentMap);
}

private EpubBookRef CreateEmptyEpubBookRef()
private EpubBookRef CreateEmptyEpubBookRef(TestZipFile testZipFile)
{
return new(new TestZipFile())
return new(testZipFile)
{
Schema = new EpubSchema()
{
Expand Down Expand Up @@ -384,14 +385,14 @@ private EpubBookRef CreateEmptyEpubBookRef()
};
}

private EpubTextContentFileRef CreateLocalTextFileRef(string href, EpubContentType contentType, string contentMimeType)
private EpubTextContentFileRef CreateLocalTextFileRef(TestZipFile testZipFile, string href, EpubContentType contentType, string contentMimeType)
{
return new(href, EpubContentLocation.LOCAL, contentType, contentMimeType, String.Empty);
return new(href, EpubContentLocation.LOCAL, contentType, contentMimeType, testZipFile, String.Empty);
}

private EpubByteContentFileRef CreateLocalByteFileRef(string href, EpubContentType contentType, string contentMimeType)
private EpubByteContentFileRef CreateLocalByteFileRef(TestZipFile testZipFile, string href, EpubContentType contentType, string contentMimeType)
{
return new(href, EpubContentLocation.LOCAL, contentType, contentMimeType, String.Empty);
return new(href, EpubContentLocation.LOCAL, contentType, contentMimeType, testZipFile, String.Empty);
}
}
}
28 changes: 14 additions & 14 deletions Source/VersOne.Epub.Test/Unit/Readers/NavigationReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void GetNavigationItemsForEpub2WithFullNcxTest()
}
}
};
EpubTextContentFileRef testTextContentFileRef = CreateTestHtmlFile("chapter1.html");
EpubTextContentFileRef testTextContentFileRef = CreateTestHtmlFile(testZipFile, "chapter1.html");
epubBookRef.Content = CreateContentRef(null, testTextContentFileRef);
EpubNavigationItemRef expectedNavigationItem1 = CreateNavigationLink("Test label 1", "chapter1.html", testTextContentFileRef);
EpubNavigationItemRef expectedNavigationItem2 = CreateNavigationLink("Test label 3", "chapter1.html#section-1", testTextContentFileRef);
Expand Down Expand Up @@ -229,9 +229,9 @@ public void GetNavigationItemsForEpub3WithFullNavTest()
}
}
};
EpubTextContentFileRef testNavigationHtmlFileRef = CreateTestNavigationFile();
EpubTextContentFileRef testTextContentFileRef1 = CreateTestHtmlFile("chapter1.html");
EpubTextContentFileRef testTextContentFileRef2 = CreateTestHtmlFile("chapter2.html");
EpubTextContentFileRef testNavigationHtmlFileRef = CreateTestNavigationFile(testZipFile);
EpubTextContentFileRef testTextContentFileRef1 = CreateTestHtmlFile(testZipFile, "chapter1.html");
EpubTextContentFileRef testTextContentFileRef2 = CreateTestHtmlFile(testZipFile, "chapter2.html");
epubBookRef.Content = CreateContentRef(testNavigationHtmlFileRef, testTextContentFileRef1, testTextContentFileRef2);
EpubNavigationItemRef expectedNavigationItem1 = CreateNavigationHeader("Test header");
EpubNavigationItemRef expectedNavigationItem2 = CreateNavigationLink("Test text 1", "chapter1.html", testTextContentFileRef1);
Expand Down Expand Up @@ -289,8 +289,8 @@ public void GetNavigationItemsForEpub3NavWithoutHeaderTest()
}
}
};
EpubTextContentFileRef testNavigationHtmlFileRef = CreateTestNavigationFile();
EpubTextContentFileRef testTextContentFileRef = CreateTestHtmlFile("chapter1.html");
EpubTextContentFileRef testNavigationHtmlFileRef = CreateTestNavigationFile(testZipFile);
EpubTextContentFileRef testTextContentFileRef = CreateTestHtmlFile(testZipFile, "chapter1.html");
epubBookRef.Content = CreateContentRef(testNavigationHtmlFileRef, testTextContentFileRef);
List<EpubNavigationItemRef> expectedNavigationItems = new()
{
Expand Down Expand Up @@ -336,7 +336,7 @@ public void GetNavigationItemsForEpub3NavWithNullOrEmptyLisTest()
}
}
};
EpubTextContentFileRef testNavigationHtmlFileRef = CreateTestNavigationFile();
EpubTextContentFileRef testNavigationHtmlFileRef = CreateTestNavigationFile(testZipFile);
epubBookRef.Content = CreateContentRef(testNavigationHtmlFileRef);
List<EpubNavigationItemRef> expectedNavigationItems = new();
List<EpubNavigationItemRef> actualNavigationItems = NavigationReader.GetNavigationItems(epubBookRef);
Expand Down Expand Up @@ -391,7 +391,7 @@ public void GetNavigationItemsForEpub3NavWithNullOrNonExistentAnchorHrefsTest()
}
}
};
EpubTextContentFileRef testNavigationHtmlFileRef = CreateTestNavigationFile();
EpubTextContentFileRef testNavigationHtmlFileRef = CreateTestNavigationFile(testZipFile);
epubBookRef.Content = CreateContentRef(testNavigationHtmlFileRef);
List<EpubNavigationItemRef> expectedNavigationItems = new()
{
Expand Down Expand Up @@ -544,8 +544,8 @@ public void GetNavigationItemsForEpub3NavWithNullOrEmptyTitlesTest()
}
}
};
EpubTextContentFileRef testNavigationHtmlFileRef = CreateTestNavigationFile();
EpubTextContentFileRef testTextContentFileRef = CreateTestHtmlFile("chapter1.html");
EpubTextContentFileRef testNavigationHtmlFileRef = CreateTestNavigationFile(testZipFile);
EpubTextContentFileRef testTextContentFileRef = CreateTestHtmlFile(testZipFile, "chapter1.html");
epubBookRef.Content = CreateContentRef(testNavigationHtmlFileRef, testTextContentFileRef);
List<EpubNavigationItemRef> expectedNavigationItems = new()
{
Expand All @@ -566,14 +566,14 @@ public void GetNavigationItemsForEpub3NavWithNullOrEmptyTitlesTest()
EpubNavigationItemRefComparer.CompareNavigationItemRefLists(expectedNavigationItems, actualNavigationItems);
}

private EpubTextContentFileRef CreateTestNavigationFile()
private EpubTextContentFileRef CreateTestNavigationFile(TestZipFile testZipFile)
{
return CreateTestHtmlFile("toc.html");
return CreateTestHtmlFile(testZipFile, "toc.html");
}

private EpubTextContentFileRef CreateTestHtmlFile(string htmlFileName)
private EpubTextContentFileRef CreateTestHtmlFile(TestZipFile testZipFile, string htmlFileName)
{
return new(htmlFileName, EpubContentLocation.LOCAL, EpubContentType.XHTML_1_1, "application/xhtml+xml", CONTENT_DIRECTORY_PATH);
return new(htmlFileName, EpubContentLocation.LOCAL, EpubContentType.XHTML_1_1, "application/xhtml+xml", testZipFile, CONTENT_DIRECTORY_PATH);
}

private EpubNavigationItemRef CreateNavigationLink(string title, string htmlFileUrl, EpubTextContentFileRef htmlFileRef)
Expand Down
21 changes: 11 additions & 10 deletions Source/VersOne.Epub.Test/Unit/Readers/SpineReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class SpineReaderTests
[Fact(DisplayName = "Getting reading order for a minimal EPUB spine should succeed")]
public void GetReadingOrderForMinimalSpineTest()
{
EpubBookRef epubBookRef = CreateEmptyEpubBookRef();
EpubBookRef epubBookRef = CreateEmptyEpubBookRef(new TestZipFile());
List<EpubTextContentFileRef> expectedReadingOrder = new();
List<EpubTextContentFileRef> actualReadingOrder = SpineReader.GetReadingOrder(epubBookRef);
Assert.Equal(expectedReadingOrder, actualReadingOrder);
Expand All @@ -18,7 +18,8 @@ public void GetReadingOrderForMinimalSpineTest()
[Fact(DisplayName = "Getting reading order for a typical EPUB spine should succeed")]
public void GetReadingOrderForTypicalSpineTest()
{
EpubBookRef epubBookRef = CreateEmptyEpubBookRef();
TestZipFile testZipFile = new();
EpubBookRef epubBookRef = CreateEmptyEpubBookRef(testZipFile);
epubBookRef.Schema.Package.Spine = new EpubSpine()
{
Items = new List<EpubSpineItemRef>()
Expand Down Expand Up @@ -51,8 +52,8 @@ public void GetReadingOrderForTypicalSpineTest()
}
}
};
EpubTextContentFileRef expectedHtmlFileRef1 = CreateTestHtmlFileRef("chapter1.html");
EpubTextContentFileRef expectedHtmlFileRef2 = CreateTestHtmlFileRef("chapter2.html");
EpubTextContentFileRef expectedHtmlFileRef1 = CreateTestHtmlFileRef(testZipFile, "chapter1.html");
EpubTextContentFileRef expectedHtmlFileRef2 = CreateTestHtmlFileRef(testZipFile, "chapter2.html");
epubBookRef.Content.Html = new Dictionary<string, EpubTextContentFileRef>()
{
{
Expand All @@ -76,7 +77,7 @@ public void GetReadingOrderForTypicalSpineTest()
[Fact(DisplayName = "GetReadingOrder should throw EpubPackageException if there is no manifest item with ID matching to the ID ref of a spine item")]
public void GetReadingOrderWithMissingManifestItemTest()
{
EpubBookRef epubBookRef = CreateEmptyEpubBookRef();
EpubBookRef epubBookRef = CreateEmptyEpubBookRef(new TestZipFile());
epubBookRef.Schema.Package.Spine = new EpubSpine()
{
Items = new List<EpubSpineItemRef>()
Expand Down Expand Up @@ -105,7 +106,7 @@ public void GetReadingOrderWithMissingManifestItemTest()
[Fact(DisplayName = "GetReadingOrder should throw EpubPackageException if there is no HTML content file referenced by a manifest item")]
public void GetReadingOrderWithMissingHtmlContentFileTest()
{
EpubBookRef epubBookRef = CreateEmptyEpubBookRef();
EpubBookRef epubBookRef = CreateEmptyEpubBookRef(new TestZipFile());
epubBookRef.Schema.Package.Spine = new EpubSpine()
{
Items = new List<EpubSpineItemRef>()
Expand All @@ -132,9 +133,9 @@ public void GetReadingOrderWithMissingHtmlContentFileTest()
Assert.Throws<EpubPackageException>(() => SpineReader.GetReadingOrder(epubBookRef));
}

private EpubBookRef CreateEmptyEpubBookRef()
private EpubBookRef CreateEmptyEpubBookRef(TestZipFile testZipFile)
{
return new(new TestZipFile())
return new(testZipFile)
{
Schema = new EpubSchema()
{
Expand Down Expand Up @@ -174,9 +175,9 @@ private EpubBookRef CreateEmptyEpubBookRef()
};
}

private EpubTextContentFileRef CreateTestHtmlFileRef(string fileName)
private EpubTextContentFileRef CreateTestHtmlFileRef(TestZipFile testZipFile, string fileName)
{
return new(fileName, EpubContentLocation.LOCAL, EpubContentType.XHTML_1_1, "application/xhtml+xml", String.Empty);
return new(fileName, EpubContentLocation.LOCAL, EpubContentType.XHTML_1_1, "application/xhtml+xml", testZipFile, String.Empty);
}
}
}
Loading

0 comments on commit 0b0993e

Please sign in to comment.