// A shared PDF read for what it says about itself (an EPUB is the core's to // read: cotton-core's book_file tests). Every book here is made in the test — // an invented title and author in a small PDF — never a real one. import CoreGraphics @testable import CottonKit import Foundation import XCTest final class BookFileTests: XCTestCase { private var folder: URL! override func setUpWithError() throws { folder = FileManager.default.temporaryDirectory.appendingPathComponent("book-file-\(UUID().uuidString)") try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) } override func tearDownWithError() throws { try? FileManager.default.removeItem(at: folder) } // MARK: PDF /// A PDF's own title and author, its page count and page 1 drawn as a /// JPEG cover. func testAPdfSaysItsTitleAuthorPagesAndCover() throws { let url = try pdf("scan.pdf", title: "A Small Grammar of Rain", author: "Mirela Osk", pages: 3) let facts = try BookFile.read(url, fileName: "scan.pdf") XCTAssertEqual(facts.title, "A Small Grammar of Rain") XCTAssertEqual(facts.author, "Mirela Osk") XCTAssertEqual(facts.pages, 3) XCTAssertEqual(facts.coverExtension, "jpg") XCTAssertEqual(facts.cover?.prefix(2), Data([0xFF, 0xD8]), "a JPEG") } /// A PDF with no title is named by its file. func testAPdfWithNoTitleIsNamedByItsFile() throws { let url = try pdf("untitled.pdf", title: nil, author: nil, pages: 1) let facts = try BookFile.read(url, fileName: "Salt Roads of Kemmerin.pdf") XCTAssertEqual(facts.title, "Salt Roads of Kemmerin") XCTAssertNil(facts.author) } // MARK: Makers private func pdf(_ name: String, title: String?, author: String?, pages: Int) throws -> URL { let url = folder.appendingPathComponent(name) var info: [CFString: Any] = [:] if let title { info[kCGPDFContextTitle] = title } if let author { info[kCGPDFContextAuthor] = author } var box = CGRect(x: 0, y: 0, width: 300, height: 450) let context = try XCTUnwrap(CGContext(url as CFURL, mediaBox: &box, info as CFDictionary)) for page in 0..