// 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.importCoreGraphics@testable importCottonKitimportFoundationimport XCTestfinalclassBookFileTests: XCTestCase {privatevar folder: URL!overridefunc setUpWithError() throws { folder = FileManager.default.temporaryDirectory.appendingPathComponent("book-file-\(UUID().uuidString)")tryFileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) }overridefunc 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 = tryBookFile.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 = tryBookFile.read(url, fileName: "Salt Roads of Kemmerin.pdf") XCTAssertEqual(facts.title, "Salt Roads of Kemmerin") XCTAssertNil(facts.author) }// MARK: Makersprivatefunc pdf(_ name: String, title: String?, author: String?, pages: Int) throws -> URL {let url = folder.appendingPathComponent(name)var info: [CFString: Any] = [:]iflet title { info[kCGPDFContextTitle] = title }iflet 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 in0..<pages { context.beginPDFPage(nil) context.setFillColor(CGColor(red: 0.4, green: 0.1, blue: CGFloat(page) / 4, alpha: 1)) context.fill(CGRect(x: 40, y: 40, width: 220, height: 370)) context.endPDFPage() } context.closePDF()return url }}