diff --git a/Package.swift b/Package.swift index 2f262976..fe91a713 100644 --- a/Package.swift +++ b/Package.swift @@ -2,30 +2,18 @@ import PackageDescription -var testingDependencies: [Package.Dependency] = [] var testingTargets: [Target] = [] let isTestingEnabled = /*TESTING_FLAG*/false/*TESTING_FLAG*/ if isTestingEnabled { - testingDependencies = [ - .package(url: "https://github.com/Quick/Quick", exact: "3.1.2"), - .package(url: "https://github.com/Quick/Nimble", exact: "9.2.1"), - ] testingTargets = [ - .testTarget(name: "TPPDFTests", dependencies: [ - "TPPDF", - "Quick", - "Nimble", - ], resources: [ + .testTarget(name: "TPPDFTests", dependencies: ["TPPDF"], resources: [ .copy("resources/graphics-fixture.gif"), .copy("resources/sample.pdf"), .copy("resources/image-fixture.jpg"), + .copy("resources/table-content-fixture.png"), ]), - .testTarget(name: "TPPDFIntegrationTests", dependencies: [ - "TPPDF", - "Quick", - "Nimble", - ], resources: [ + .testTarget(name: "TPPDFIntegrationTests", dependencies: ["TPPDF"], resources: [ .copy("resources/50-pages.pdf"), ]), ] @@ -44,7 +32,6 @@ let package = Package( products: [ .library(name: "TPPDF", targets: ["TPPDF"]), ], - dependencies: testingDependencies, targets: [ .target(name: "TPPDF", path: "Source"), ] + testingTargets diff --git a/Tests/TPPDFTests/API/Table/Sections/PDFTableRowSpec.swift b/Tests/TPPDFTests/API/Table/Sections/PDFTableRowSpec.swift deleted file mode 100644 index 2e045810..00000000 --- a/Tests/TPPDFTests/API/Table/Sections/PDFTableRowSpec.swift +++ /dev/null @@ -1,29 +0,0 @@ -// -// PDFTableRowSpec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 23.09.2020. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFTableRowSpec: QuickSpec { - override func spec() { - describe("PDFTableRow") { - describe("alignment") { - it("should set alignment for each row the same") { - let table = PDFTable(rows: 3, columns: 3) - table.rows.allRowsAlignment = [.left, .center, .right] - expect(table.rows.alignment) == [ - [.left, .center, .right], - [.left, .center, .right], - [.left, .center, .right], - ] - } - } - } - } -} diff --git a/Tests/TPPDFTests/API/Table/Sections/PDFTableRowSubscriptsTests.swift b/Tests/TPPDFTests/API/Table/Sections/PDFTableRowSubscriptsTests.swift new file mode 100644 index 00000000..9b4473f9 --- /dev/null +++ b/Tests/TPPDFTests/API/Table/Sections/PDFTableRowSubscriptsTests.swift @@ -0,0 +1,24 @@ +// +// PDFTableRowSubscriptsTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 23.09.2020. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFTableRowSubscriptsTests { + @Test func rowSubscriptReturnsCellsAtRequestedRowIndex() { + // Arrange + let table = PDFTable(rows: 3, columns: 3) + let expectedCells = table.cells[1] + + // Act + let cells = table[row: 1].cells + + // Assert + #expect(cells == expectedCells) + } +} diff --git a/Tests/TPPDFTests/API/Table/Sections/PDFTableRowTests.swift b/Tests/TPPDFTests/API/Table/Sections/PDFTableRowTests.swift new file mode 100644 index 00000000..01e6e565 --- /dev/null +++ b/Tests/TPPDFTests/API/Table/Sections/PDFTableRowTests.swift @@ -0,0 +1,25 @@ +// +// PDFTableRowTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 23.09.2020. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFTableRowTests { + @Test func settingAllRowsAlignmentAppliesSameAlignmentToEveryRow() { + // Arrange + let table = PDFTable(rows: 3, columns: 3) + let alignment: [PDFTableCellAlignment] = [.left, .center, .right] + + // Act + table.rows.allRowsAlignment = alignment + let result = table.rows.alignment + + // Assert + #expect(result == [alignment, alignment, alignment]) + } +} diff --git a/Tests/TPPDFTests/API/Table/Sections/PDFTable_RowSubscriptsSpec.swift b/Tests/TPPDFTests/API/Table/Sections/PDFTable_RowSubscriptsSpec.swift deleted file mode 100644 index 5ea67ae4..00000000 --- a/Tests/TPPDFTests/API/Table/Sections/PDFTable_RowSubscriptsSpec.swift +++ /dev/null @@ -1,24 +0,0 @@ -// -// PDFTable_RowSubscriptsSpec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 23.09.2020. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFTable_RowSubscriptsSpec: QuickSpec { - override func spec() { - describe("PDFTable") { - describe("Row Subscripts") { - it("should return row by index") { - let table = PDFTable(rows: 3, columns: 3) - expect(table[row: 1].cells) == table.cells[1] - } - } - } - } -} diff --git a/Tests/TPPDFTests/Layout/PDFContainerTests.swift b/Tests/TPPDFTests/Layout/PDFContainerTests.swift new file mode 100644 index 00000000..f6b237a6 --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFContainerTests.swift @@ -0,0 +1,55 @@ +// +// PDFContainerTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.09.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFContainerTests { + @Test(arguments: [ + (0, false, false, false, false), + (1, true, false, true, false), (2, true, false, false, false), (3, true, false, false, true), + (4, false, false, true, false), (5, false, false, false, false), (6, false, false, false, true), + (7, false, true, true, false), (8, false, true, false, false), (9, false, true, false, true), + ]) + func containerIdentifiesItsRegionAndHorizontalAlignment( + index: Int, expectedHeader: Bool, expectedFooter: Bool, expectedLeft: Bool, expectedRight: Bool + ) { + // Arrange + let containers: [PDFContainer] = [ + .none, .headerLeft, .headerCenter, .headerRight, .contentLeft, .contentCenter, .contentRight, + .footerLeft, .footerCenter, .footerRight, + ] + let container = containers[index] + + // Act + let header = container.isHeader + let footer = container.isFooter + let left = container.isLeft + let right = container.isRight + + // Assert + #expect(header == expectedHeader) + #expect(footer == expectedFooter) + #expect(left == expectedLeft) + #expect(right == expectedRight) + } + + @Test func allContainersIncludesEveryPositionInOrderAndExcludesNone() { + // Arrange + let expected: [PDFContainer] = [ + .headerLeft, .headerCenter, .headerRight, .contentLeft, .contentCenter, .contentRight, + .footerLeft, .footerCenter, .footerRight, + ] + + // Act + let containers = PDFContainer.all + + // Assert + #expect(containers == expected) + } +} diff --git a/Tests/TPPDFTests/Layout/PDFContainer_Spec.swift b/Tests/TPPDFTests/Layout/PDFContainer_Spec.swift deleted file mode 100644 index 85c4ce09..00000000 --- a/Tests/TPPDFTests/Layout/PDFContainer_Spec.swift +++ /dev/null @@ -1,107 +0,0 @@ -// -// PDFContainer_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.09.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFContainer_Spec: QuickSpec { - // swiftlint:disable closure_body_length function_body_length - override func spec() { - describe("PDFContainer") { - it("should have enum values") { - expect(PDFContainer.none).toNot(beNil()) - - expect(PDFContainer.headerLeft).toNot(beNil()) - expect(PDFContainer.headerCenter).toNot(beNil()) - expect(PDFContainer.headerRight).toNot(beNil()) - - expect(PDFContainer.contentLeft).toNot(beNil()) - expect(PDFContainer.contentCenter).toNot(beNil()) - expect(PDFContainer.contentRight).toNot(beNil()) - - expect(PDFContainer.footerLeft).toNot(beNil()) - expect(PDFContainer.footerCenter).toNot(beNil()) - expect(PDFContainer.footerRight).toNot(beNil()) - } - - it("can check if container is in header") { - expect(PDFContainer.none.isHeader).to(beFalse()) - - expect(PDFContainer.headerLeft.isHeader).to(beTrue()) - expect(PDFContainer.headerCenter.isHeader).to(beTrue()) - expect(PDFContainer.headerRight.isHeader).to(beTrue()) - - expect(PDFContainer.contentLeft.isHeader).to(beFalse()) - expect(PDFContainer.contentCenter.isHeader).to(beFalse()) - expect(PDFContainer.contentRight.isHeader).to(beFalse()) - - expect(PDFContainer.footerLeft.isHeader).to(beFalse()) - expect(PDFContainer.footerCenter.isHeader).to(beFalse()) - expect(PDFContainer.footerRight.isHeader).to(beFalse()) - } - - it("can check if container is in footer") { - expect(PDFContainer.none.isFooter).to(beFalse()) - - expect(PDFContainer.headerLeft.isFooter).to(beFalse()) - expect(PDFContainer.headerCenter.isFooter).to(beFalse()) - expect(PDFContainer.headerRight.isFooter).to(beFalse()) - - expect(PDFContainer.contentLeft.isFooter).to(beFalse()) - expect(PDFContainer.contentCenter.isFooter).to(beFalse()) - expect(PDFContainer.contentRight.isFooter).to(beFalse()) - - expect(PDFContainer.footerLeft.isFooter).to(beTrue()) - expect(PDFContainer.footerCenter.isFooter).to(beTrue()) - expect(PDFContainer.footerRight.isFooter).to(beTrue()) - } - - it("can check if container is on the left") { - expect(PDFContainer.none.isLeft).to(beFalse()) - - expect(PDFContainer.headerLeft.isLeft).to(beTrue()) - expect(PDFContainer.headerCenter.isLeft).to(beFalse()) - expect(PDFContainer.headerRight.isLeft).to(beFalse()) - - expect(PDFContainer.contentLeft.isLeft).to(beTrue()) - expect(PDFContainer.contentCenter.isLeft).to(beFalse()) - expect(PDFContainer.contentRight.isLeft).to(beFalse()) - - expect(PDFContainer.footerLeft.isLeft).to(beTrue()) - expect(PDFContainer.footerCenter.isLeft).to(beFalse()) - expect(PDFContainer.footerRight.isLeft).to(beFalse()) - } - - it("can check if container is on the right") { - expect(PDFContainer.none.isRight).to(beFalse()) - - expect(PDFContainer.headerLeft.isRight).to(beFalse()) - expect(PDFContainer.headerCenter.isRight).to(beFalse()) - expect(PDFContainer.headerRight.isRight).to(beTrue()) - - expect(PDFContainer.contentLeft.isRight).to(beFalse()) - expect(PDFContainer.contentCenter.isRight).to(beFalse()) - expect(PDFContainer.contentRight.isRight).to(beTrue()) - - expect(PDFContainer.footerLeft.isRight).to(beFalse()) - expect(PDFContainer.footerCenter.isRight).to(beFalse()) - expect(PDFContainer.footerRight.isRight).to(beTrue()) - } - - it("can return an array with all values") { - expect(PDFContainer.all) == [ - PDFContainer.headerLeft, PDFContainer.headerCenter, PDFContainer.headerRight, - PDFContainer.contentLeft, PDFContainer.contentCenter, PDFContainer.contentRight, - PDFContainer.footerLeft, PDFContainer.footerCenter, PDFContainer.footerRight, - ] - } - } - } - // swiftlint:enable closure_body_length function_body_length -} diff --git a/Tests/TPPDFTests/Layout/PDFIndentationObjectEquatableTests.swift b/Tests/TPPDFTests/Layout/PDFIndentationObjectEquatableTests.swift new file mode 100644 index 00000000..d1a55421 --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFIndentationObjectEquatableTests.swift @@ -0,0 +1,26 @@ +// +// PDFIndentationObjectEquatableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.16.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFIndentationObjectEquatableTests { + @Test(arguments: [(0.0, true, true, true), (10.0, true, true, false), (0.0, false, true, false), (0.0, true, false, false)]) + func equalityDependsOnIndentationSideAndSectionScope(indentation: Double, left: Bool, insideSection: Bool, expectedEquality: Bool) { + // Arrange + let object = PDFIndentationObject(indentation: 0, left: true, insideSectionColumn: true) + let otherObject = PDFIndentationObject(indentation: CGFloat(indentation), left: left, insideSectionColumn: insideSection) + + // Act + let isEqual = object == otherObject + + // Assert + #expect(isEqual == expectedEquality) + } +} diff --git a/Tests/TPPDFTests/Layout/PDFIndentationObjectTests.swift b/Tests/TPPDFTests/Layout/PDFIndentationObjectTests.swift new file mode 100644 index 00000000..6481d4f2 --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFIndentationObjectTests.swift @@ -0,0 +1,47 @@ +// +// PDFIndentationObjectTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFIndentationObjectTests { + @Test func initializingPreservesIndentationSideAndSectionScope() { + // Arrange + let indentation: CGFloat = 20 + let left = false + let insideSectionColumn = false + + // Act + let object = PDFIndentationObject(indentation: indentation, left: left, insideSectionColumn: insideSectionColumn) + + // Assert + #expect(object.indentation == indentation) + #expect(object.left == left) + #expect(object.insideSectionColumn == insideSectionColumn) + } + + @Test(arguments: [false, true]) + func calculatingSetsIndentationOnChosenSideAndReturnsObject(left: Bool) throws { + // Arrange + let object = PDFIndentationObject(indentation: 20, left: left, insideSectionColumn: false) + let generator = PDFGenerator(document: PDFDocument(format: .a4)) + let container = PDFContainer.contentLeft + + // Act + let result = try object.calculate(generator: generator, container: container) + let indentation = left ? generator.layout.indentation.leftIn(container: container) + : generator.layout.indentation.rightIn(container: container) + + // Assert + #expect(indentation == object.indentation) + #expect(result.count == 1) + #expect(result.first?.0 == container) + #expect(result.first?.1 as? PDFIndentationObject == object) + } +} diff --git a/Tests/TPPDFTests/Layout/PDFIndentationObject_Equatable_Spec.swift b/Tests/TPPDFTests/Layout/PDFIndentationObject_Equatable_Spec.swift deleted file mode 100644 index 98763a4b..00000000 --- a/Tests/TPPDFTests/Layout/PDFIndentationObject_Equatable_Spec.swift +++ /dev/null @@ -1,41 +0,0 @@ -// -// PDFIndentationObject_Equatable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.16.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFIndentationObject_Equatable_Spec: QuickSpec { - override func spec() { - describe("PDFIndentationObject") { - context("Equatable") { - let object = PDFIndentationObject(indentation: 0, left: true, insideSectionColumn: true) - - it("is equal") { - let otherObject = PDFIndentationObject(indentation: 0, left: true, insideSectionColumn: true) - expect(object) == otherObject - } - - it("is not equal with different indentation") { - let otherObject = PDFIndentationObject(indentation: 10, left: true, insideSectionColumn: true) - expect(object) != otherObject - } - - it("is not equal with different left") { - let otherObject = PDFIndentationObject(indentation: 0, left: false, insideSectionColumn: true) - expect(object) != otherObject - } - - it("is not equal with different insideSectionColumn") { - let otherObject = PDFIndentationObject(indentation: 0, left: true, insideSectionColumn: false) - expect(object) != otherObject - } - } - } - } -} diff --git a/Tests/TPPDFTests/Layout/PDFIndentationObject_Spec.swift b/Tests/TPPDFTests/Layout/PDFIndentationObject_Spec.swift deleted file mode 100644 index 5044bc56..00000000 --- a/Tests/TPPDFTests/Layout/PDFIndentationObject_Spec.swift +++ /dev/null @@ -1,81 +0,0 @@ -// -// PDFIndentationObject_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Nimble -import Quick -@testable import TPPDF - -class PDFIndentationObject_Spec: QuickSpec { - // swiftlint:disable closure_body_length - override func spec() { - describe("PDFIndentationObject") { - var object: PDFIndentationObject! - let indentation: CGFloat = 20.0 - let left = false - let insideSectionColumn = false - - beforeEach { - object = PDFIndentationObject(indentation: indentation, left: left, insideSectionColumn: insideSectionColumn) - } - - describe("variables") { - it("has an indentation") { - expect(object.indentation) == indentation - } - - it("has a left") { - expect(object.left) == left - } - - it("has a insideSectionColumn") { - expect(object.insideSectionColumn) == insideSectionColumn - } - } - - describe("calculation") { - let document = PDFDocument(format: .a4) - var generator: PDFGenerator! - - let container = PDFContainer.contentLeft - var result: [PDFLocatedRenderObject]! - - beforeEach { - generator = PDFGenerator(document: document) - result = [] - } - - it("can set indentation from left") { - object.left = true - - expect { - result = try object.calculate(generator: generator, container: container) - }.toNot(throwError()) - expect(generator.layout.indentation.leftIn(container: PDFContainer.contentLeft)).toEventually(equal(object.indentation)) - - expect(result).toEventually(haveCount(1)) - expect(result.first?.0).toEventually(equal(container)) - expect(result.first?.1 as? PDFIndentationObject).toEventually(equal(object)) - } - - it("can set indentation from right") { - object.left = false - expect { - result = try object.calculate(generator: generator, container: PDFContainer.contentLeft) - }.toNot(throwError()) - expect(generator.layout.indentation.rightIn(container: PDFContainer.contentLeft)).toEventually(equal(object.indentation)) - - expect(result).toEventually(haveCount(1)) - expect(result.first?.0).toEventually(equal(container)) - expect(result.first?.1 as? PDFIndentationObject).toEventually(equal(object)) - } - } - } - } - // swiftlint:enable closure_body_length -} diff --git a/Tests/TPPDFTests/Layout/PDFLayout+Equatable_Spec.swift b/Tests/TPPDFTests/Layout/PDFLayout+Equatable_Spec.swift deleted file mode 100644 index 2434b7a7..00000000 --- a/Tests/TPPDFTests/Layout/PDFLayout+Equatable_Spec.swift +++ /dev/null @@ -1,40 +0,0 @@ -// -// PDFLayout+Equatable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFLayout_Equatable_Spec: QuickSpec { - override func spec() { - describe("PDFLayout") { - context("Equatable") { - let layout = PDFLayout() - - it("is equal") { - let otherLayout = PDFLayout() - expect(layout) == otherLayout - } - - it("is not equal with different heights") { - let otherLayout = PDFLayout() - otherLayout.heights.content = 123 - - expect(layout) != otherLayout - } - - it("is not equal with different indentation") { - let otherLayout = PDFLayout() - otherLayout.indentation.header.left = 100 - - expect(layout) != otherLayout - } - } - } - } -} diff --git a/Tests/TPPDFTests/Layout/PDFLayoutEquatableTests.swift b/Tests/TPPDFTests/Layout/PDFLayoutEquatableTests.swift new file mode 100644 index 00000000..cb312b1c --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFLayoutEquatableTests.swift @@ -0,0 +1,30 @@ +// +// PDFLayoutEquatableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFLayoutEquatableTests { + @Test(arguments: ["same", "heights", "indentation"]) + func equalityDependsOnHeightsAndIndentations(property: String) { + // Arrange + let layout = PDFLayout() + let otherLayout = PDFLayout() + switch property { + case "heights": otherLayout.heights.content = 123 + case "indentation": otherLayout.indentation.header.left = 100 + default: break + } + + // Act + let isEqual = layout == otherLayout + + // Assert + #expect(isEqual == (property == "same")) + } +} diff --git a/Tests/TPPDFTests/Layout/PDFLayoutHeights+Equatable_Spec.swift b/Tests/TPPDFTests/Layout/PDFLayoutHeights+Equatable_Spec.swift deleted file mode 100644 index db6a4dcb..00000000 --- a/Tests/TPPDFTests/Layout/PDFLayoutHeights+Equatable_Spec.swift +++ /dev/null @@ -1,44 +0,0 @@ -// -// PDFLayoutHeights+Equatable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFLayoutHeights_Equatable_Spec: QuickSpec { - override func spec() { - describe("PDFLayoutHeights") { - context("Equatable") { - let heights = PDFLayoutHeights() - - it("is equal") { - let otherHeights = PDFLayoutHeights() - expect(heights) == otherHeights - } - - it("is not equal with different header") { - var otherHeights = PDFLayoutHeights() - otherHeights.header = [PDFContainer.headerLeft: 10] - expect(heights) != otherHeights - } - - it("is not equal with different footer") { - var otherHeights = PDFLayoutHeights() - otherHeights.footer = [PDFContainer.footerLeft: 10] - expect(heights) != otherHeights - } - - it("is not equal with different content value") { - var otherHeights = PDFLayoutHeights() - otherHeights.content = 123 - expect(heights) != otherHeights - } - } - } - } -} diff --git a/Tests/TPPDFTests/Layout/PDFLayoutHeightsEquatableTests.swift b/Tests/TPPDFTests/Layout/PDFLayoutHeightsEquatableTests.swift new file mode 100644 index 00000000..41bc7a92 --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFLayoutHeightsEquatableTests.swift @@ -0,0 +1,31 @@ +// +// PDFLayoutHeightsEquatableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFLayoutHeightsEquatableTests { + @Test(arguments: ["same", "header", "footer", "content"]) + func equalityDependsOnHeightsForEachRegion(region: String) { + // Arrange + let heights = PDFLayoutHeights() + var otherHeights = PDFLayoutHeights() + switch region { + case "header": otherHeights.header = [.headerLeft: 10] + case "footer": otherHeights.footer = [.footerLeft: 10] + case "content": otherHeights.content = 123 + default: break + } + + // Act + let isEqual = heights == otherHeights + + // Assert + #expect(isEqual == (region == "same")) + } +} diff --git a/Tests/TPPDFTests/Layout/PDFLayoutHeightsTests.swift b/Tests/TPPDFTests/Layout/PDFLayoutHeightsTests.swift new file mode 100644 index 00000000..a906cd19 --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFLayoutHeightsTests.swift @@ -0,0 +1,94 @@ +// +// PDFLayoutHeightsTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.05.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFLayoutHeightsTests { + @Test func initializingHeightsUsesZeroForEveryRegion() { + // Arrange + let expectedHeader: [PDFContainer: CGFloat] = [.headerLeft: 0, .headerCenter: 0, .headerRight: 0] + let expectedFooter: [PDFContainer: CGFloat] = [.footerLeft: 0, .footerCenter: 0, .footerRight: 0] + + // Act + let heights = PDFLayoutHeights() + + // Assert + #expect(heights.header == expectedHeader) + #expect(heights.footer == expectedFooter) + #expect(heights.content == 0) + } + + @Test func maximumHeaderHeightUsesLargestContainer() { + // Arrange + var heights = PDFLayoutHeights() + heights.header = [.headerLeft: 2, .headerCenter: 3, .headerRight: 4] + + // Act + let maximum = heights.maxHeaderHeight() + + // Assert + #expect(maximum == 4) + } + + @Test func missingHeaderAndFooterHeightsHaveZeroMaximum() { + // Arrange + var heights = PDFLayoutHeights() + heights.header = [:] + heights.footer = [:] + + // Act + let header = heights.maxHeaderHeight() + let footer = heights.maxFooterHeight() + + // Assert + #expect(header == 0) + #expect(footer == 0) + } + + @Test(arguments: [(0, 20.0), (1, 30.0), (2, 40.0), (3, 80.0), (4, 90.0), (5, 100.0), (6, 50.0), (7, 60.0), (8, 70.0)]) + func addingHeightUpdatesContainerAndSharesContentHeight(index: Int, expectedHeight: Double) { + // Arrange + var heights = PDFLayoutHeights() + heights.header = [.headerLeft: 10, .headerCenter: 20, .headerRight: 30] + heights.footer = [.footerLeft: 40, .footerCenter: 50, .footerRight: 60] + heights.content = 70 + let containers = PDFContainer.all + let container = containers[index] + + // Act + if (3...5).contains(index) { + for contentContainer in containers[3...index] { + heights.add(10, to: contentContainer) + } + } else { + heights.add(10, to: container) + } + let result: CGFloat? = container.isHeader ? heights.header[container] + : container.isFooter ? heights.footer[container] : heights.content + + // Assert + #expect(result == CGFloat(expectedHeight)) + } + + @Test func addingHeightToMissingContainersCreatesValues() { + // Arrange + var heights = PDFLayoutHeights() + heights.header = [:] + heights.footer = [:] + + // Act + heights.add(10, to: .headerLeft) + heights.add(20, to: .footerLeft) + + // Assert + #expect(heights.header[.headerLeft] == 10) + #expect(heights.footer[.footerLeft] == 20) + } +} diff --git a/Tests/TPPDFTests/Layout/PDFLayoutHeights_Spec.swift b/Tests/TPPDFTests/Layout/PDFLayoutHeights_Spec.swift deleted file mode 100644 index f9184de3..00000000 --- a/Tests/TPPDFTests/Layout/PDFLayoutHeights_Spec.swift +++ /dev/null @@ -1,120 +0,0 @@ -// -// PDFLayoutHeights_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.05.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Nimble -import Quick -@testable import TPPDF - -class PDFLayoutHeights_Spec: QuickSpec { - // swiftlint:disable closure_body_length function_body_length - override func spec() { - describe("PDFLayoutHeights") { - var heights: PDFLayoutHeights! - - beforeEach { - heights = PDFLayoutHeights() - } - - context("variables") { - it("has default values per header container") { - expect(heights.header) == [ - PDFContainer.headerLeft: 0, - PDFContainer.headerCenter: 0, - PDFContainer.headerRight: 0, - ] - } - - it("has default values per footer container") { - expect(heights.footer) == [ - PDFContainer.footerLeft: 0, - PDFContainer.footerCenter: 0, - PDFContainer.footerRight: 0, - ] - } - - it("has a default content value") { - expect(heights.content) == 0 - } - } - - it("can find max header height") { - let max: CGFloat = 4 - heights.header = [ - PDFContainer.headerLeft: 2, - PDFContainer.headerCenter: 3, - PDFContainer.headerRight: max, - ] - expect(heights.maxHeaderHeight()) == max - } - - it("returns zero for max header when missing values") { - heights.header = [:] - expect(heights.maxHeaderHeight()) == 0 - } - - it("returns zero for max footer when missing values") { - heights.footer = [:] - expect(heights.maxFooterHeight()) == 0 - } - - it("can add value to a container") { - heights.header = [ - PDFContainer.headerLeft: 10, - PDFContainer.headerCenter: 20, - PDFContainer.headerRight: 30, - ] - heights.footer = [ - PDFContainer.footerLeft: 40, - PDFContainer.footerCenter: 50, - PDFContainer.footerRight: 60, - ] - heights.content = 70 - - heights.add(10, to: .headerLeft) - expect(heights.header[PDFContainer.headerLeft]) == 20 - - heights.add(10, to: .headerCenter) - expect(heights.header[PDFContainer.headerCenter]) == 30 - - heights.add(10, to: .headerRight) - expect(heights.header[PDFContainer.headerRight]) == 40 - - heights.add(10, to: .footerLeft) - expect(heights.footer[PDFContainer.footerLeft]) == 50 - - heights.add(10, to: .footerCenter) - expect(heights.footer[PDFContainer.footerCenter]) == 60 - - heights.add(10, to: .footerRight) - expect(heights.footer[PDFContainer.footerRight]) == 70 - - heights.add(10, to: PDFContainer.contentLeft) - expect(heights.content) == 80 - - heights.add(10, to: PDFContainer.contentCenter) - expect(heights.content) == 90 - - heights.add(10, to: PDFContainer.contentRight) - expect(heights.content) == 100 - } - - it("sets value when can not add") { - heights.header = [:] - heights.footer = [:] - - heights.add(10, to: .headerLeft) - expect(heights.header[PDFContainer.headerLeft]) == 10 - - heights.add(20, to: .footerLeft) - expect(heights.footer[PDFContainer.footerLeft]) == 20 - } - } - } - // swiftlint:enable closure_body_length function_body_length -} diff --git a/Tests/TPPDFTests/Layout/PDFLayoutIndentations+Equatable_Spec.swift b/Tests/TPPDFTests/Layout/PDFLayoutIndentations+Equatable_Spec.swift deleted file mode 100644 index 1549f53c..00000000 --- a/Tests/TPPDFTests/Layout/PDFLayoutIndentations+Equatable_Spec.swift +++ /dev/null @@ -1,44 +0,0 @@ -// -// PDFLayoutIndentations+Equatable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFLayoutIndentations_Equatable_Spec: QuickSpec { - override func spec() { - describe("PDFLayoutIndentations") { - context("Equatable") { - let indentation = PDFLayoutIndentations() - - it("is equal") { - let otherIndentation = PDFLayoutIndentations() - expect(indentation) == otherIndentation - } - - it("is not equal with different header") { - var otherIndentation = PDFLayoutIndentations() - otherIndentation.header = (10, 20) - expect(indentation) != otherIndentation - } - - it("is not equal with different footer") { - var otherIndentation = PDFLayoutIndentations() - otherIndentation.content = (10, 20) - expect(indentation) != otherIndentation - } - - it("is not equal with different content value") { - var otherIndentation = PDFLayoutIndentations() - otherIndentation.footer = (10, 20) - expect(indentation) != otherIndentation - } - } - } - } -} diff --git a/Tests/TPPDFTests/Layout/PDFLayoutIndentationsEquatableTests.swift b/Tests/TPPDFTests/Layout/PDFLayoutIndentationsEquatableTests.swift new file mode 100644 index 00000000..ceeadad4 --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFLayoutIndentationsEquatableTests.swift @@ -0,0 +1,31 @@ +// +// PDFLayoutIndentationsEquatableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFLayoutIndentationsEquatableTests { + @Test(arguments: ["same", "header", "content", "footer"]) + func equalityDependsOnIndentationsForEveryRegion(region: String) { + // Arrange + let indentations = PDFLayoutIndentations() + var otherIndentations = PDFLayoutIndentations() + switch region { + case "header": otherIndentations.header = (10, 20) + case "content": otherIndentations.content = (10, 20) + case "footer": otherIndentations.footer = (10, 20) + default: break + } + + // Act + let isEqual = indentations == otherIndentations + + // Assert + #expect(isEqual == (region == "same")) + } +} diff --git a/Tests/TPPDFTests/Layout/PDFLayoutIndentationsTests.swift b/Tests/TPPDFTests/Layout/PDFLayoutIndentationsTests.swift new file mode 100644 index 00000000..927d6fba --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFLayoutIndentationsTests.swift @@ -0,0 +1,72 @@ +// +// PDFLayoutIndentationsTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFLayoutIndentationsTests { + @Test func initializingIndentationsUsesZeroOnBothSidesOfEveryRegion() { + // Arrange + let expected: CGFloat = 0 + + // Act + let indentations = PDFLayoutIndentations() + + // Assert + #expect(indentations.header.left == expected) + #expect(indentations.header.right == expected) + #expect(indentations.content.left == expected) + #expect(indentations.content.right == expected) + #expect(indentations.footer.left == expected) + #expect(indentations.footer.right == expected) + } + + @Test(arguments: 0..<9) + func accessingContainerIndentationUsesItsRegion(index: Int) { + // Arrange + var indentations = PDFLayoutIndentations() + indentations.header = (10, 20) + indentations.content = (30, 40) + indentations.footer = (50, 60) + let container = PDFContainer.all[index] + let expectedLeft: [CGFloat] = [10, 10, 10, 30, 30, 30, 50, 50, 50] + let expectedRight: [CGFloat] = [20, 20, 20, 40, 40, 40, 60, 60, 60] + + // Act + let left = indentations.leftIn(container: container) + let right = indentations.rightIn(container: container) + + // Assert + #expect(left == expectedLeft[index]) + #expect(right == expectedRight[index]) + } + + @Test(arguments: 0..<9, [false, true]) + func settingContainerIndentationReplacesEarlierValuesOnChosenSide(index: Int, right: Bool) { + // Arrange + var indentations = PDFLayoutIndentations() + let containers = PDFContainer.all + let values: [CGFloat] = [10, 20, 30] + let regionStart = index / 3 * 3 + + // Act + for position in regionStart...index { + if right { + indentations.setRight(indentation: values[position % 3], in: containers[position]) + } else { + indentations.setLeft(indentation: values[position % 3], in: containers[position]) + } + } + let result = right ? indentations.rightIn(container: containers[index]) + : indentations.leftIn(container: containers[index]) + + // Assert + #expect(result == values[index % 3]) + } +} diff --git a/Tests/TPPDFTests/Layout/PDFLayoutIndentations_Spec.swift b/Tests/TPPDFTests/Layout/PDFLayoutIndentations_Spec.swift deleted file mode 100644 index 7e64546f..00000000 --- a/Tests/TPPDFTests/Layout/PDFLayoutIndentations_Spec.swift +++ /dev/null @@ -1,162 +0,0 @@ -// -// PDFLayoutIndentations_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFLayoutIndentations_Spec: QuickSpec { - // swiftlint:disable closure_body_length function_body_length - override func spec() { - describe("PDFLayoutHeights") { - var indentations: PDFLayoutIndentations! - - beforeEach { - indentations = PDFLayoutIndentations() - } - - context("variables") { - it("has default header values") { - expect(indentations.header.left) == 0 - expect(indentations.header.right) == 0 - } - - it("has default content values") { - expect(indentations.footer.left) == 0 - expect(indentations.footer.right) == 0 - } - - it("has a default footer values") { - expect(indentations.content.left) == 0 - expect(indentations.content.right) == 0 - } - } - - context("accessors") { - beforeEach { - indentations.header = (10, 20) - indentations.content = (30, 40) - indentations.footer = (50, 60) - } - - context("left") { - it("can get value of header containers") { - expect(indentations.leftIn(container: PDFContainer.headerLeft)) == 10 - expect(indentations.leftIn(container: PDFContainer.headerCenter)) == 10 - expect(indentations.leftIn(container: PDFContainer.headerRight)) == 10 - } - - it("can get value of content containers") { - expect(indentations.leftIn(container: PDFContainer.contentLeft)) == 30 - expect(indentations.leftIn(container: PDFContainer.contentCenter)) == 30 - expect(indentations.leftIn(container: PDFContainer.contentRight)) == 30 - } - - it("can get value of footer containers") { - expect(indentations.leftIn(container: PDFContainer.footerLeft)) == 50 - expect(indentations.leftIn(container: PDFContainer.footerCenter)) == 50 - expect(indentations.leftIn(container: PDFContainer.footerRight)) == 50 - } - } - - context("right") { - it("can get value of header containers") { - expect(indentations.rightIn(container: PDFContainer.headerLeft)) == 20 - expect(indentations.rightIn(container: PDFContainer.headerCenter)) == 20 - expect(indentations.rightIn(container: PDFContainer.headerRight)) == 20 - } - - it("can get value of content containers") { - expect(indentations.rightIn(container: PDFContainer.contentLeft)) == 40 - expect(indentations.rightIn(container: PDFContainer.contentCenter)) == 40 - expect(indentations.rightIn(container: PDFContainer.contentRight)) == 40 - } - - it("can get value of footer containers") { - expect(indentations.rightIn(container: PDFContainer.footerLeft)) == 60 - expect(indentations.rightIn(container: PDFContainer.footerCenter)) == 60 - expect(indentations.rightIn(container: PDFContainer.footerRight)) == 60 - } - } - } - - context("modifiers") { - context("left") { - it("can set a value to a header container") { - indentations.setLeft(indentation: 10, in: PDFContainer.headerLeft) - expect(indentations.leftIn(container: PDFContainer.headerLeft)) == 10 - - indentations.setLeft(indentation: 20, in: PDFContainer.headerCenter) - expect(indentations.leftIn(container: PDFContainer.headerCenter)) == 20 - - indentations.setLeft(indentation: 30, in: PDFContainer.headerRight) - expect(indentations.leftIn(container: PDFContainer.headerRight)) == 30 - } - - it("can set a value to a content container") { - indentations.setLeft(indentation: 10, in: PDFContainer.contentLeft) - expect(indentations.leftIn(container: PDFContainer.contentLeft)) == 10 - - indentations.setLeft(indentation: 20, in: PDFContainer.contentCenter) - expect(indentations.leftIn(container: PDFContainer.contentCenter)) == 20 - - indentations.setLeft(indentation: 30, in: PDFContainer.contentRight) - expect(indentations.leftIn(container: PDFContainer.contentRight)) == 30 - } - - it("can set a value to a footer container") { - indentations.setLeft(indentation: 10, in: PDFContainer.footerLeft) - expect(indentations.leftIn(container: PDFContainer.footerLeft)) == 10 - - indentations.setLeft(indentation: 20, in: PDFContainer.footerCenter) - expect(indentations.leftIn(container: PDFContainer.footerCenter)) == 20 - - indentations.setLeft(indentation: 30, in: PDFContainer.footerRight) - expect(indentations.leftIn(container: PDFContainer.footerRight)) == 30 - } - } - - context("right") { - it("can set a value to a header container") { - indentations.setRight(indentation: 10, in: PDFContainer.headerLeft) - expect(indentations.rightIn(container: PDFContainer.headerLeft)) == 10 - - indentations.setRight(indentation: 20, in: PDFContainer.headerCenter) - expect(indentations.rightIn(container: PDFContainer.headerCenter)) == 20 - - indentations.setRight(indentation: 30, in: PDFContainer.headerRight) - expect(indentations.rightIn(container: PDFContainer.headerRight)) == 30 - } - - it("can set a value to a content container") { - indentations.setRight(indentation: 10, in: PDFContainer.contentLeft) - expect(indentations.rightIn(container: PDFContainer.contentLeft)) == 10 - - indentations.setRight(indentation: 20, in: PDFContainer.contentCenter) - expect(indentations.rightIn(container: PDFContainer.contentCenter)) == 20 - - indentations.setRight(indentation: 30, in: PDFContainer.contentRight) - expect(indentations.rightIn(container: PDFContainer.contentRight)) == 30 - } - - it("can set a value to a footer container") { - indentations.setRight(indentation: 10, in: PDFContainer.footerLeft) - expect(indentations.rightIn(container: PDFContainer.footerLeft)) == 10 - - indentations.setRight(indentation: 20, in: PDFContainer.footerCenter) - expect(indentations.rightIn(container: PDFContainer.footerCenter)) == 20 - - indentations.setRight(indentation: 30, in: PDFContainer.footerRight) - expect(indentations.rightIn(container: PDFContainer.footerRight)) == 30 - } - } - } - } - } - // swiftlint:enable closure_body_length function_body_length -} diff --git a/Tests/TPPDFTests/Layout/PDFLayoutTests.swift b/Tests/TPPDFTests/Layout/PDFLayoutTests.swift new file mode 100644 index 00000000..f819785e --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFLayoutTests.swift @@ -0,0 +1,77 @@ +// +// PDFLayoutTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFLayoutTests { + @Test func assigningHeightsAndIndentationsPreservesInstances() { + // Arrange + let layout = PDFLayout() + let heights = PDFLayoutHeights() + let indentations = PDFLayoutIndentations() + + // Act + layout.heights = heights + layout.indentation = indentations + + // Assert + #expect(layout.heights == heights) + #expect(layout.indentation == indentations) + } + + @Test(arguments: 0..<9) + func contentOffsetUsesContainerHeight(index: Int) { + // Arrange + let layout = PDFLayout() + layout.heights.header = [.headerLeft: 1, .headerCenter: 2, .headerRight: 3] + layout.heights.footer = [.footerLeft: 1, .footerCenter: 2, .footerRight: 3] + layout.heights.content = 123 + let container = PDFContainer.all[index] + let expected: [CGFloat] = [1, 2, 3, 123, 123, 123, 1, 2, 3] + + // Act + let offset = layout.getContentOffset(in: container) + + // Assert + #expect(offset == expected[index]) + } + + @Test(arguments: 0..<9) + func settingContentOffsetUpdatesContainerHeight(index: Int) { + // Arrange + let layout = PDFLayout() + let container = PDFContainer.all[index] + let values: [CGFloat] = [111, 222, 333, 444, 555, 666, 777, 888, 999] + let value = values[index] + + // Act + layout.setContentOffset(in: container, to: value) + let offset = layout.getContentOffset(in: container) + + // Assert + #expect(offset == value) + } + + @Test func resettingLayoutClearsPreviousHeightsAndIndentations() { + // Arrange + let layout = PDFLayout() + layout.setContentOffset(in: .contentCenter, to: 123) + layout.indentation.setLeft(indentation: 123, in: .contentCenter) + let heights = layout.heights + let indentations = layout.indentation + + // Act + layout.reset() + + // Assert + #expect(layout.heights != heights) + #expect(layout.indentation != indentations) + } +} diff --git a/Tests/TPPDFTests/Layout/PDFLayout_Spec.swift b/Tests/TPPDFTests/Layout/PDFLayout_Spec.swift deleted file mode 100644 index 60073c7d..00000000 --- a/Tests/TPPDFTests/Layout/PDFLayout_Spec.swift +++ /dev/null @@ -1,88 +0,0 @@ -// -// PDFLayout_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Nimble -import Quick -@testable import TPPDF - -class PDFLayout_Spec: QuickSpec { - // swiftlint:disable closure_body_length function_body_length - override func spec() { - describe("PDFLayout") { - var layout: PDFLayout! - - beforeEach { - layout = PDFLayout() - } - - context("variables") { - it("has an default heights instance") { - layout.heights = PDFLayoutHeights() - } - - it("has an default indentation instance") { - layout.indentation = PDFLayoutIndentations() - } - } - - it("can get content offset from heights") { - let headerHeights: [PDFContainer: CGFloat] = [ - PDFContainer.headerLeft: 1, - PDFContainer.headerCenter: 2, - PDFContainer.headerRight: 3, - ] - layout.heights.header = headerHeights - - expect(layout.getContentOffset(in: .headerLeft)) == headerHeights[PDFContainer.headerLeft] - expect(layout.getContentOffset(in: .headerCenter)) == headerHeights[PDFContainer.headerCenter] - expect(layout.getContentOffset(in: .headerRight)) == headerHeights[PDFContainer.headerRight] - - let footerHeights: [PDFContainer: CGFloat] = [ - PDFContainer.footerLeft: 1, - PDFContainer.footerCenter: 2, - PDFContainer.footerRight: 3, - ] - layout.heights.footer = footerHeights - - expect(layout.getContentOffset(in: .footerLeft)) == footerHeights[PDFContainer.footerLeft]! - expect(layout.getContentOffset(in: .footerCenter)) == footerHeights[PDFContainer.footerCenter]! - expect(layout.getContentOffset(in: .footerRight)) == footerHeights[PDFContainer.footerRight]! - - let contentHeight: CGFloat = 123 - layout.heights.content = contentHeight - - expect(layout.getContentOffset(in: .contentLeft)) == contentHeight - expect(layout.getContentOffset(in: .contentCenter)) == contentHeight - expect(layout.getContentOffset(in: .contentRight)) == contentHeight - } - - it("can set content offset in heights") { - expect(PDFContainer.all).to(allPass { - let random = CGFloat.random(in: 0...10000) - layout.setContentOffset(in: $0!, to: random) - return layout.getContentOffset(in: $0!) == random - }) - } - - it("can be resetted") { - layout.setContentOffset(in: .contentCenter, to: 123) - layout.indentation.setLeft(indentation: 123, in: .contentCenter) - - let heights = layout.heights - let indentation = layout.indentation - - layout.reset() - - expect(layout.heights) != heights - expect(layout.indentation) != indentation - } - } - } - // swiftlint:enable closure_body_length function_body_length -} diff --git a/Tests/TPPDFTests/Layout/PDFOffsetObject+Equatable_Spec.swift b/Tests/TPPDFTests/Layout/PDFOffsetObject+Equatable_Spec.swift deleted file mode 100644 index 98990bd8..00000000 --- a/Tests/TPPDFTests/Layout/PDFOffsetObject+Equatable_Spec.swift +++ /dev/null @@ -1,31 +0,0 @@ -// -// PDFOffsetObject+Equatable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFOffsetObject_Equatable_Spec: QuickSpec { - override func spec() { - describe("PDFOffsetObject") { - context("Equatable") { - let object = PDFOffsetObject(offset: 10) - - it("is equal") { - let otherObject = PDFOffsetObject(offset: 10) - expect(object) == otherObject - } - - it("is not equal with different offset") { - let otherObject = PDFOffsetObject(offset: 20) - expect(object) != otherObject - } - } - } - } -} diff --git a/Tests/TPPDFTests/Layout/PDFOffsetObjectEquatableTests.swift b/Tests/TPPDFTests/Layout/PDFOffsetObjectEquatableTests.swift new file mode 100644 index 00000000..12d1b237 --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFOffsetObjectEquatableTests.swift @@ -0,0 +1,26 @@ +// +// PDFOffsetObjectEquatableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFOffsetObjectEquatableTests { + @Test(arguments: [(10.0, true), (20.0, false)]) + func equalityDependsOnOffset(offset: Double, expectedEquality: Bool) { + // Arrange + let object = PDFOffsetObject(offset: 10) + let otherObject = PDFOffsetObject(offset: CGFloat(offset)) + + // Act + let isEqual = object == otherObject + + // Assert + #expect(isEqual == expectedEquality) + } +} diff --git a/Tests/TPPDFTests/Layout/PDFOffsetObjectTests.swift b/Tests/TPPDFTests/Layout/PDFOffsetObjectTests.swift new file mode 100644 index 00000000..ac7f494f --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFOffsetObjectTests.swift @@ -0,0 +1,41 @@ +// +// PDFOffsetObjectTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFOffsetObjectTests { + @Test func initializingWithOffsetPreservesOffset() { + // Arrange + let offset: CGFloat = 20 + + // Act + let object = PDFOffsetObject(offset: offset) + + // Assert + #expect(object.offset == offset) + } + + @Test func calculatingSetsContainerOffsetAndReturnsObject() throws { + // Arrange + let object = PDFOffsetObject(offset: 20) + let generator = PDFGenerator(document: PDFDocument(format: .a4)) + let container = PDFContainer.contentLeft + + // Act + let result = try object.calculate(generator: generator, container: container) + let offset = generator.layout.getContentOffset(in: container) + + // Assert + #expect(offset == object.offset) + #expect(result.count == 1) + #expect(result.first?.0 == container) + #expect(result.first?.1 as? PDFOffsetObject == object) + } +} diff --git a/Tests/TPPDFTests/Layout/PDFOffsetObject_Spec.swift b/Tests/TPPDFTests/Layout/PDFOffsetObject_Spec.swift deleted file mode 100644 index 4f1e3c2f..00000000 --- a/Tests/TPPDFTests/Layout/PDFOffsetObject_Spec.swift +++ /dev/null @@ -1,55 +0,0 @@ -// -// PDFOffsetObject_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Nimble -import Quick -@testable import TPPDF - -class PDFOffsetObject_Spec: QuickSpec { - override func spec() { - describe("PDFOffsetObject") { - var object: PDFOffsetObject! - let offset: CGFloat = 20.0 - - beforeEach { - object = PDFOffsetObject(offset: offset) - } - - describe("variables") { - it("has an offset value") { - expect(object.offset) == offset - } - } - - describe("calculation") { - let document = PDFDocument(format: .a4) - var generator: PDFGenerator! - - let container = PDFContainer.contentLeft - var result: [PDFLocatedRenderObject]! - - beforeEach { - generator = PDFGenerator(document: document) - result = [] - } - - it("can set offset") { - expect { - result = try object.calculate(generator: generator, container: container) - }.toNot(throwError()) - expect(generator.layout.getContentOffset(in: container)).toEventually(equal(object.offset)) - - expect(result).toEventually(haveCount(1)) - expect(result.first?.0).toEventually(equal(container)) - expect(result.first?.1 as? PDFOffsetObject).toEventually(equal(object)) - } - } - } - } -} diff --git a/Tests/TPPDFTests/Layout/PDFPageBreakObject+Equatable_Spec.swift b/Tests/TPPDFTests/Layout/PDFPageBreakObject+Equatable_Spec.swift deleted file mode 100644 index af92513c..00000000 --- a/Tests/TPPDFTests/Layout/PDFPageBreakObject+Equatable_Spec.swift +++ /dev/null @@ -1,26 +0,0 @@ -// -// PDFPageBreakObject+Equatable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFPageBreakObject_Equatable_Spec: QuickSpec { - override func spec() { - describe("PDFPageBreakObject") { - context("Equatable") { - let object = PDFPageBreakObject() - - it("is equal") { - let otherObject = PDFPageBreakObject() - expect(object) == otherObject - } - } - } - } -} diff --git a/Tests/TPPDFTests/Layout/PDFPageBreakObjectEquatableTests.swift b/Tests/TPPDFTests/Layout/PDFPageBreakObjectEquatableTests.swift new file mode 100644 index 00000000..4a92dfea --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFPageBreakObjectEquatableTests.swift @@ -0,0 +1,24 @@ +// +// PDFPageBreakObjectEquatableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFPageBreakObjectEquatableTests { + @Test func pageBreakObjectsAreEqual() { + // Arrange + let object = PDFPageBreakObject() + let otherObject = PDFPageBreakObject() + + // Act + let isEqual = object == otherObject + + // Assert + #expect(isEqual) + } +} diff --git a/Tests/TPPDFTests/Layout/PDFPageBreakObjectTests.swift b/Tests/TPPDFTests/Layout/PDFPageBreakObjectTests.swift new file mode 100644 index 00000000..1a195d8f --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFPageBreakObjectTests.swift @@ -0,0 +1,44 @@ +// +// PDFPageBreakObjectTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Foundation +import Testing +@testable import TPPDF + +struct PDFPageBreakObjectTests { + @Test func calculatingPageBreakResetsContentHeightAndReturnsObject() throws { + // Arrange + let object = PDFPageBreakObject() + let generator = PDFGenerator(document: PDFDocument(format: .a4)) + let container = PDFContainer.contentLeft + + // Act + let result = try object.calculate(generator: generator, container: container) + + // Assert + #expect(generator.layout.heights.content == 0) + #expect(result.count == 1) + #expect(result.first?.0 == container) + #expect(result.first?.1 as? PDFPageBreakObject == object) + } + + @Test func drawingPageBreakDoesNotThrow() throws { + // Arrange + let object = PDFPageBreakObject() + let generator = PDFGenerator(document: PDFDocument(format: .a4)) + let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString) + defer { try? FileManager.default.removeItem(at: url) } + let context = try #require(CGContext(url as CFURL, mediaBox: nil, nil)) + + // Act and Assert + #expect(throws: Never.self) { + try object.draw(generator: generator, container: .contentLeft, in: PDFContext(cgContext: context)) + } + } +} diff --git a/Tests/TPPDFTests/Layout/PDFPageBreakObject_Spec.swift b/Tests/TPPDFTests/Layout/PDFPageBreakObject_Spec.swift deleted file mode 100644 index 426fe073..00000000 --- a/Tests/TPPDFTests/Layout/PDFPageBreakObject_Spec.swift +++ /dev/null @@ -1,61 +0,0 @@ -// -// PDFPageBreakObject_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Foundation -import Nimble -import Quick -@testable import TPPDF - -class PDFPageBreakObject_Spec: QuickSpec { - // swiftlint:disable closure_body_length - override func spec() { - describe("PDFPageBreakObject") { - var object: PDFPageBreakObject! - - beforeEach { - object = PDFPageBreakObject() - } - - describe("calculation and drawing") { - let document = PDFDocument(format: .a4) - var generator: PDFGenerator! - var context: CGContext! - - let container = PDFContainer.contentLeft - var result: [PDFLocatedRenderObject]! - - beforeEach { - generator = PDFGenerator(document: document) - result = [] - let url = URL(fileURLWithPath: NSTemporaryDirectory()) - .appendingPathComponent(UUID().uuidString) - context = CGContext(url as CFURL, mediaBox: nil, nil) - } - - it("can set offset") { - expect { - result = try object.calculate(generator: generator, container: container) - }.toNot(throwError()) - expect(generator.layout.heights.content) == 0 - - expect(result).toEventually(haveCount(1)) - expect(result.first?.0).toEventually(equal(container)) - expect(result.first?.1 as? PDFPageBreakObject).toEventually(equal(object)) - } - - it("can be drawn") { - expect { - try object.draw(generator: generator, container: container, in: PDFContext(cgContext: context)) - }.toNot(throwError()) - } - } - } - } - // swiftlint:enable closure_body_length -} diff --git a/Tests/TPPDFTests/Layout/PDFPageLayout+Equatable_Spec.swift b/Tests/TPPDFTests/Layout/PDFPageLayout+Equatable_Spec.swift deleted file mode 100644 index a66cb79b..00000000 --- a/Tests/TPPDFTests/Layout/PDFPageLayout+Equatable_Spec.swift +++ /dev/null @@ -1,58 +0,0 @@ -// -// PDFPageLayout+Equatable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Nimble -import Quick -@testable import TPPDF - -class PDFPageLayout_Equatable_Spec: QuickSpec { - override func spec() { - describe("PDFPageLayout") { - context("Equatable") { - var object: PDFPageLayout! - - beforeEach { - object = PDFPageLayout() - } - - it("is equal") { - let otherObject = PDFPageLayout() - expect(object) == otherObject - } - - it("is not equal with different size") { - var otherObject = PDFPageLayout() - - otherObject.size = CGSize(width: 20, height: 30) - object.size = CGSize(width: 50, height: 10) - - expect(object) != otherObject - } - - it("is not equal with different margin") { - var otherObject = PDFPageLayout() - - object.margin = EdgeInsets(top: 40, left: 30, bottom: 20, right: 10) - otherObject.margin = EdgeInsets(top: 10, left: 20, bottom: 30, right: 40) - - expect(object) != otherObject - } - - it("is not equal with different space") { - var otherObject = PDFPageLayout() - - otherObject.space.footer = 100 - object.space.footer = 150 - - expect(object) != otherObject - } - } - } - } -} diff --git a/Tests/TPPDFTests/Layout/PDFPageLayoutEquatableTests.swift b/Tests/TPPDFTests/Layout/PDFPageLayoutEquatableTests.swift new file mode 100644 index 00000000..53456a2b --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFPageLayoutEquatableTests.swift @@ -0,0 +1,38 @@ +// +// PDFPageLayoutEquatableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFPageLayoutEquatableTests { + @Test(arguments: ["same", "size", "margin", "space"]) + func equalityDependsOnSizeMarginsAndSpacing(property: String) { + // Arrange + var layout = PDFPageLayout() + var otherLayout = PDFPageLayout() + switch property { + case "size": + layout.size = CGSize(width: 50, height: 10) + otherLayout.size = CGSize(width: 20, height: 30) + case "margin": + layout.margin = EdgeInsets(top: 40, left: 30, bottom: 20, right: 10) + otherLayout.margin = EdgeInsets(top: 10, left: 20, bottom: 30, right: 40) + case "space": + layout.space.footer = 150 + otherLayout.space.footer = 100 + default: break + } + + // Act + let isEqual = layout == otherLayout + + // Assert + #expect(isEqual == (property == "same")) + } +} diff --git a/Tests/TPPDFTests/Layout/PDFPageLayoutTests.swift b/Tests/TPPDFTests/Layout/PDFPageLayoutTests.swift new file mode 100644 index 00000000..cbcd47f5 --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFPageLayoutTests.swift @@ -0,0 +1,45 @@ +// +// PDFPageLayoutTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFPageLayoutTests { + @Test func initializingPageLayoutUsesZeroSizeMarginsAndSpacing() { + // Arrange + let expectedSize = CGSize.zero + let expectedMargin = EdgeInsets.zero + + // Act + let layout = PDFPageLayout() + + // Assert + #expect(layout.size == expectedSize) + #expect(layout.margin == expectedMargin) + #expect(layout.space.header == 0) + #expect(layout.space.footer == 0) + } + + @Test func settingSizeUpdatesBoundsWidthAndHeight() { + // Arrange + var layout = PDFPageLayout() + let size = CGSize(width: 10, height: 20) + + // Act + layout.size = size + let bounds = layout.bounds + let width = layout.width + let height = layout.height + + // Assert + #expect(bounds == CGRect(x: 0, y: 0, width: 10, height: 20)) + #expect(width == 10) + #expect(height == 20) + } +} diff --git a/Tests/TPPDFTests/Layout/PDFPageLayout_Spec.swift b/Tests/TPPDFTests/Layout/PDFPageLayout_Spec.swift deleted file mode 100644 index 9c0bca7e..00000000 --- a/Tests/TPPDFTests/Layout/PDFPageLayout_Spec.swift +++ /dev/null @@ -1,56 +0,0 @@ -// -// PDFPageLayout_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Nimble -import Quick -@testable import TPPDF - -class PDFPageLayout_Spec: QuickSpec { - override func spec() { - describe("PDFPageLayout") { - var layout: PDFPageLayout! - - beforeEach { - layout = PDFPageLayout() - } - - context("variables") { - it("has default value size") { - expect(layout.size) == CGSize.zero - } - - it("has default value margin") { - expect(layout.margin) == EdgeInsets.zero - } - - it("has a default value space") { - expect(layout.space.footer) == 0 - expect(layout.space.header) == 0 - } - } - - context("computed variables") { - it("has a value bounds") { - layout.size = CGSize(width: 10, height: 20) - expect(layout.bounds) == CGRect(x: 0, y: 0, width: 10, height: 20) - } - - it("has a value width") { - layout.size = CGSize(width: 10, height: 20) - expect(layout.width) == 10 - } - - it("has a value height") { - layout.size = CGSize(width: 10, height: 20) - expect(layout.height) == 20 - } - } - } - } -} diff --git a/Tests/TPPDFTests/Layout/PDFSpaceObjectTests.swift b/Tests/TPPDFTests/Layout/PDFSpaceObjectTests.swift new file mode 100644 index 00000000..3fe2ec97 --- /dev/null +++ b/Tests/TPPDFTests/Layout/PDFSpaceObjectTests.swift @@ -0,0 +1,42 @@ +// +// PDFSpaceObjectTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFSpaceObjectTests { + @Test func initializingWithSpacePreservesSpace() { + // Arrange + let space: CGFloat = 20 + + // Act + let object = PDFSpaceObject(space: space) + + // Assert + #expect(object.space == space) + } + + @Test func calculatingSpacePositionsObjectAndAdvancesContentHeight() throws { + // Arrange + let object = PDFSpaceObject(space: 20) + let generator = PDFGenerator(document: PDFDocument(format: .a4)) + let container = PDFContainer.contentLeft + let previousHeight = generator.layout.heights.content + + // Act + let result = try object.calculate(generator: generator, container: container) + + // Assert + #expect(generator.layout.heights.content == previousHeight + object.frame.height) + #expect(object.frame == CGRect(x: 60, y: 60, width: 475, height: 20)) + #expect(result.count == 1) + #expect(result.first?.0 == container) + #expect(result.first?.1 as? PDFSpaceObject === object) + } +} diff --git a/Tests/TPPDFTests/Layout/PDFSpaceObject_Spec.swift b/Tests/TPPDFTests/Layout/PDFSpaceObject_Spec.swift deleted file mode 100644 index 35829f99..00000000 --- a/Tests/TPPDFTests/Layout/PDFSpaceObject_Spec.swift +++ /dev/null @@ -1,61 +0,0 @@ -// -// PDFSpaceObject_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Nimble -import Quick -@testable import TPPDF - -class PDFSpaceObject_Spec: QuickSpec { - // swiftlint:disable closure_body_length - override func spec() { - describe("PDFSpaceObject") { - var object: PDFSpaceObject! - let space: CGFloat = 20.0 - - beforeEach { - object = PDFSpaceObject(space: space) - } - - describe("variables") { - it("has an space value") { - expect(object.space) == space - } - } - - context("calculation") { - let document = PDFDocument(format: .a4) - var generator: PDFGenerator! - - let container = PDFContainer.contentLeft - var result: [PDFLocatedRenderObject]! - - beforeEach { - generator = PDFGenerator(document: document) - result = [] - } - - it("can set space") { - let contentHeight = generator.layout.heights.content - - expect { - result = try object.calculate(generator: generator, container: container) - }.toNot(throwError()) - expect(generator.layout.heights.content).toEventually(equal(contentHeight + object.frame.height)) - - expect(object.frame) == CGRect(x: 60, y: 60, width: 475, height: space) - - expect(result).toEventually(haveCount(1)) - expect(result.first?.0).toEventually(equal(container)) - expect(result.first?.1 as? PDFSpaceObject).toEventually(be(object)) - } - } - } - } - // swiftlint:enable closure_body_length -} diff --git a/Tests/TPPDFTests/PDFDocument+Objects_Spec.swift b/Tests/TPPDFTests/PDFDocument+Objects_Spec.swift deleted file mode 100644 index 5aefd014..00000000 --- a/Tests/TPPDFTests/PDFDocument+Objects_Spec.swift +++ /dev/null @@ -1,382 +0,0 @@ -// -// PDFDocument+Objects_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.09.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Foundation -import Nimble -import Quick -@testable import TPPDF - -class PDFDocument_Objects_Spec: QuickSpec { - // swiftlint:disable closure_body_length function_body_length - override func spec() { - describe("PDFDocument") { - context("Objects") { - var document: PDFDocument! - - beforeEach { - document = PDFDocument(format: .a4) - } - - context("spacing") { - let value: CGFloat = 20 - - it("is possible to add into default container") { - document.add(space: value) - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.contentLeft - - let spaceObject = document.objects[0].1 as? PDFSpaceObject - expect(spaceObject?.space) == value - } - - it("is possible to add into specific container") { - document.add(.footerLeft, space: value) - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.footerLeft - - let spaceObject = document.objects[0].1 as? PDFSpaceObject - expect(spaceObject?.space) == value - } - } - - context("line separator") { - let style = PDFLineStyle(type: .dotted, color: .red, width: 10) - - it("is possible to add into default container") { - document.addLineSeparator(style: style) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.contentLeft - - let separatorObject = document.objects[0].1 as? PDFLineSeparatorObject - expect(separatorObject?.style) == style - } - - it("is possible to add into specific container") { - document.addLineSeparator(.headerRight, style: style) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.headerRight - - let separatorObject = document.objects[0].1 as? PDFLineSeparatorObject - expect(separatorObject?.style) == style - } - } - - context("image") { - // swiftlint:disable:next line_length - let base64String = Data("/9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAAaADAAQAAAABAAAAAQAAAAD/7QA4UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAAA4QklNBCUAAAAAABDUHYzZjwCyBOmACZjs+EJ+/8AAEQgAAQABAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/bAEMABgYGBgYGCgYGCg4KCgoOEg4ODg4SFxISEhISFxwXFxcXFxccHBwcHBwcHCIiIiIiIicnJycnLCwsLCwsLCwsLP/bAEMBBwcHCwoLEwoKEy4fGh8uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLi4uLv/dAAQAAf/aAAwDAQACEQMRAD8A6+iiivxY/Sz/2Q==".utf8) - let data = Data(base64Encoded: base64String)! - let image = Image(data: data)! - - let pdfImage = PDFImage(image: image) - - it("is possible to add an image to the default container") { - document.add(image: pdfImage) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.contentLeft - - let separatorObject = document.objects[0].1 as? PDFImageObject - expect(separatorObject?.image) == pdfImage - } - - it("is possible to add an image to a specific container") { - document.add(.headerRight, image: pdfImage) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.headerRight - - let separatorObject = document.objects[0].1 as? PDFImageObject - expect(separatorObject?.image) == pdfImage - } - - context("rows") { - let images = [pdfImage, pdfImage, pdfImage] - let spacing: CGFloat = 10.0 - - it("is possible to add an image row to the default container") { - document.add(image: pdfImage) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.contentLeft - - let separatorObject = document.objects[0].1 as? PDFImageObject - expect(separatorObject?.image) == pdfImage - } - - it("is possible to add an image row with a specific spacing to a specific container") { - document.add(.headerRight, imagesInRow: images, spacing: spacing) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.headerRight - - let separatorObject = document.objects[0].1 as? PDFImageRowObject - expect(separatorObject?.images) == images - expect(separatorObject?.spacing) == spacing - } - } - } - - context("image row") {} - - context("simple text") { - let text = "EXAMPLE" - let spacing: CGFloat = 2.5 - - it("is possible to add a text with default spacing to the default container") { - document.add(text: text) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.contentLeft - - let separatorObject = document.objects[0].1 as? PDFAttributedTextObject - expect(separatorObject?.attributedText).to(beNil()) - expect(separatorObject?.simpleText?.text) == text - } - - it("is possible to add a text with specific spacing to a specific container") { - document.add(.headerRight, text: text, lineSpacing: spacing) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.headerRight - - let separatorObject = document.objects[0].1 as? PDFAttributedTextObject - expect(separatorObject?.attributedText).to(beNil()) - expect(separatorObject?.simpleText?.text) == text - expect(separatorObject?.simpleText?.spacing) == spacing - } - } - - context("attributed text") { - let text = NSAttributedString(string: "EXAMPLE") - - it("is possible to add an attributed text to the default container") { - document.add(attributedText: text) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.contentLeft - - let separatorObject = document.objects[0].1 as? PDFAttributedTextObject - expect(separatorObject?.attributedText?.text) == text - expect(separatorObject?.simpleText).to(beNil()) - } - - it("is possible to add an attributed text to a specific container") { - document.add(.headerRight, attributedText: text) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.headerRight - - let separatorObject = document.objects[0].1 as? PDFAttributedTextObject - expect(separatorObject?.attributedText?.text) == text - expect(separatorObject?.simpleText).to(beNil()) - } - } - - context("text font") { - let font = Font.systemFont(ofSize: 20, weight: .bold) - - it("is possible to set the text color of the default container") { - document.set(font: font) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.contentLeft - - let object = document.objects[0].1 as? PDFFontObject - expect(object?.font) == font - } - - it("is possible to set the text color a specific container") { - document.set(.headerRight, font: font) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.headerRight - - let object = document.objects[0].1 as? PDFFontObject - expect(object?.font) == font - } - - it("is possible to reset the text color of the default container") { - document.resetFont() - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.contentLeft - - let object = document.objects[0].1 as? PDFFontObject - expect(object?.font) == Font.systemFont(ofSize: PDFConstants.defaultFontSize) - } - - it("is possible to reset the text color a specific container") { - document.resetFont(.headerRight) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.headerRight - - let object = document.objects[0].1 as? PDFFontObject - expect(object?.font) == Font.systemFont(ofSize: PDFConstants.defaultFontSize) - } - } - - context("text color") { - let color = Color.orange - - it("is possible to set the text color of the default container") { - document.set(textColor: color) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.contentLeft - - let object = document.objects[0].1 as? PDFTextColorObject - expect(object?.color) == color - } - - it("is possible to set the text color a specific container") { - document.set(.headerRight, textColor: color) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.headerRight - - let object = document.objects[0].1 as? PDFTextColorObject - expect(object?.color) == color - } - - it("is possible to reset the text color of the default container") { - document.resetTextColor() - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.contentLeft - - let object = document.objects[0].1 as? PDFTextColorObject - expect(object?.color) == Color.black - } - - it("is possible to reset the text color a specific container") { - document.resetTextColor(.headerRight) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.headerRight - - let object = document.objects[0].1 as? PDFTextColorObject - expect(object?.color) == Color.black - } - } - - context("table") { - let table = PDFTable(rows: 0, columns: 0) - - it("is possible to add a table into the default container") { - document.add(table: table) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.contentLeft - - let object = document.objects[0].1 as? PDFTableObject - expect(object?.table) == table - } - - it("is possible to add a table into a specific container") { - document.add(.headerRight, table: table) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.headerRight - - let object = document.objects[0].1 as? PDFTableObject - expect(object?.table) == table - } - } - - context("list") { - let indentations: [(pre: CGFloat, past: CGFloat)] = [(pre: 0, past: 10)] - let list = PDFList(indentations: indentations) - - it("is possible to add a list into the default container") { - document.add(list: list) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.contentLeft - - let object = document.objects[0].1 as? PDFListObject - expect(object?.list == list).to(beTrue()) - } - - it("is possible add a list into a specific container") { - document.add(.headerRight, list: list) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.headerRight - - let object = document.objects[0].1 as? PDFListObject - expect(object?.list) == list - } - } - - context("indentation") { - let indentation: CGFloat = 20.0 - let leftSide = true - - it("is possible to set indentation of default container") { - document.set(indent: indentation, left: leftSide) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.contentLeft - - let object = document.objects[0].1 as? PDFIndentationObject - expect(object?.indentation) == indentation - expect(object?.left) == leftSide - } - - it("is possible to set indentation of specific container") { - document.set(.headerRight, indent: indentation, left: leftSide) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.headerRight - - let object = document.objects[0].1 as? PDFIndentationObject - expect(object?.indentation) == indentation - expect(object?.left) == leftSide - } - } - - context("absolute offset") { - let offset: CGFloat = 20.0 - - it("is possible to set absolute offset of default container") { - document.set(absoluteOffset: offset) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.contentLeft - - let object = document.objects[0].1 as? PDFOffsetObject - expect(object?.offset) == offset - } - - it("is possible to set absolute offset of specific container") { - document.set(.headerRight, absoluteOffset: offset) - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.headerRight - - let object = document.objects[0].1 as? PDFOffsetObject - expect(object?.offset) == offset - } - } - - it("is possible to create a new page") { - document.createNewPage() - - expect(document.objects).to(haveCount(1)) - expect(document.objects[0].0) == PDFContainer.contentLeft - expect(document.objects[0].1 as? PDFPageBreakObject).toNot(beNil()) - } - } - } - } - // swiftlint:enable closure_body_length function_body_length -} diff --git a/Tests/TPPDFTests/PDFDocumentObjectsTests.swift b/Tests/TPPDFTests/PDFDocumentObjectsTests.swift new file mode 100644 index 00000000..0cf980b4 --- /dev/null +++ b/Tests/TPPDFTests/PDFDocumentObjectsTests.swift @@ -0,0 +1,230 @@ +// +// PDFDocumentObjectsTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.09.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Foundation +import Testing +@testable import TPPDF + +struct PDFDocumentObjectsTests { + @Test(arguments: [false, true]) + func addingSpaceUsesDefaultOrExplicitContainer(explicit: Bool) throws { + // Arrange + let document = PDFDocument(format: .a4) + let value: CGFloat = 20 + let container: PDFContainer = explicit ? .footerLeft : .contentLeft + + // Act + if explicit { document.add(.footerLeft, space: value) } else { document.add(space: value) } + + // Assert + let object = try onlyObject(PDFSpaceObject.self, in: document, container: container) + #expect(object.space == value) + } + + @Test(arguments: [false, true]) + func addingLineSeparatorPreservesStyleAndContainer(explicit: Bool) throws { + // Arrange + let document = PDFDocument(format: .a4) + let style = PDFLineStyle(type: .dotted, color: .red, width: 10) + let container: PDFContainer = explicit ? .headerRight : .contentLeft + + // Act + if explicit { document.addLineSeparator(.headerRight, style: style) } else { document.addLineSeparator(style: style) } + + // Assert + let object = try onlyObject(PDFLineSeparatorObject.self, in: document, container: container) + #expect(object.style == style) + } + + @Test(arguments: [false, true]) + func addingImagePreservesImageAndContainer(explicit: Bool) throws { + // Arrange + let document = PDFDocument(format: .a4) + let image = try loadImage() + let container: PDFContainer = explicit ? .headerRight : .contentLeft + + // Act + if explicit { document.add(.headerRight, image: image) } else { document.add(image: image) } + + // Assert + let object = try onlyObject(PDFImageObject.self, in: document, container: container) + #expect(object.image == image) + } + + @Test func addingImageRowPreservesImagesSpacingAndExplicitContainer() throws { + // Arrange + let document = PDFDocument(format: .a4) + let image = try loadImage() + let images = [image, image, image] + let spacing: CGFloat = 10 + + // Act + document.add(.headerRight, imagesInRow: images, spacing: spacing) + + // Assert + let object = try onlyObject(PDFImageRowObject.self, in: document, container: .headerRight) + #expect(object.images == images) + #expect(object.spacing == spacing) + } + + @Test(arguments: [false, true]) + func addingSimpleTextPreservesTextAndRequestedSpacing(explicit: Bool) throws { + // Arrange + let document = PDFDocument(format: .a4) + let text = "EXAMPLE" + let spacing: CGFloat = 2.5 + let container: PDFContainer = explicit ? .headerRight : .contentLeft + + // Act + if explicit { document.add(.headerRight, text: text, lineSpacing: spacing) } else { document.add(text: text) } + + // Assert + let object = try onlyObject(PDFAttributedTextObject.self, in: document, container: container) + #expect(object.attributedText == nil) + #expect(object.simpleText?.text == text) + if explicit { #expect(object.simpleText?.spacing == spacing) } + } + + @Test(arguments: [false, true]) + func addingAttributedTextPreservesTextAndContainer(explicit: Bool) throws { + // Arrange + let document = PDFDocument(format: .a4) + let text = NSAttributedString(string: "EXAMPLE") + let container: PDFContainer = explicit ? .headerRight : .contentLeft + + // Act + if explicit { document.add(.headerRight, attributedText: text) } else { document.add(attributedText: text) } + + // Assert + let object = try onlyObject(PDFAttributedTextObject.self, in: document, container: container) + #expect(object.attributedText?.text == text) + #expect(object.simpleText == nil) + } + + @Test(arguments: [false, true], [false, true]) + func settingOrResettingFontRegistersFontInRequestedContainer(explicit: Bool, reset: Bool) throws { + // Arrange + let document = PDFDocument(format: .a4) + let font = reset ? Font.systemFont(ofSize: PDFConstants.defaultFontSize) : Font.systemFont(ofSize: 20, weight: .bold) + let container: PDFContainer = explicit ? .headerRight : .contentLeft + + // Act + if reset { + if explicit { document.resetFont(.headerRight) } else { document.resetFont() } + } else { + if explicit { document.set(.headerRight, font: font) } else { document.set(font: font) } + } + + // Assert + let object = try onlyObject(PDFFontObject.self, in: document, container: container) + #expect(object.font == font) + } + + @Test(arguments: [false, true], [false, true]) + func settingOrResettingTextColorRegistersColorInRequestedContainer(explicit: Bool, reset: Bool) throws { + // Arrange + let document = PDFDocument(format: .a4) + let color = reset ? Color.black : Color.orange + let container: PDFContainer = explicit ? .headerRight : .contentLeft + + // Act + if reset { + if explicit { document.resetTextColor(.headerRight) } else { document.resetTextColor() } + } else { + if explicit { document.set(.headerRight, textColor: color) } else { document.set(textColor: color) } + } + + // Assert + let object = try onlyObject(PDFTextColorObject.self, in: document, container: container) + #expect(object.color == color) + } + + @Test(arguments: [false, true]) + func addingTablePreservesTableAndContainer(explicit: Bool) throws { + // Arrange + let document = PDFDocument(format: .a4) + let table = PDFTable(rows: 0, columns: 0) + let container: PDFContainer = explicit ? .headerRight : .contentLeft + + // Act + if explicit { document.add(.headerRight, table: table) } else { document.add(table: table) } + + // Assert + let object = try onlyObject(PDFTableObject.self, in: document, container: container) + #expect(object.table == table) + } + + @Test(arguments: [false, true]) + func addingListPreservesListAndContainer(explicit: Bool) throws { + // Arrange + let document = PDFDocument(format: .a4) + let list = PDFList(indentations: [(pre: 0, past: 10)]) + let container: PDFContainer = explicit ? .headerRight : .contentLeft + + // Act + if explicit { document.add(.headerRight, list: list) } else { document.add(list: list) } + + // Assert + let object = try onlyObject(PDFListObject.self, in: document, container: container) + #expect(object.list == list) + } + + @Test(arguments: [false, true]) + func settingIndentationPreservesValueSideAndContainer(explicit: Bool) throws { + // Arrange + let document = PDFDocument(format: .a4) + let value: CGFloat = 20 + let container: PDFContainer = explicit ? .headerRight : .contentLeft + + // Act + if explicit { document.set(.headerRight, indent: value, left: true) } else { document.set(indent: value, left: true) } + + // Assert + let object = try onlyObject(PDFIndentationObject.self, in: document, container: container) + #expect(object.indentation == value) + #expect(object.left) + } + + @Test(arguments: [false, true]) + func settingAbsoluteOffsetPreservesValueAndContainer(explicit: Bool) throws { + // Arrange + let document = PDFDocument(format: .a4) + let value: CGFloat = 20 + let container: PDFContainer = explicit ? .headerRight : .contentLeft + + // Act + if explicit { document.set(.headerRight, absoluteOffset: value) } else { document.set(absoluteOffset: value) } + + // Assert + let object = try onlyObject(PDFOffsetObject.self, in: document, container: container) + #expect(object.offset == value) + } + + @Test func creatingNewPageRegistersPageBreakInDefaultContainer() throws { + // Arrange + let document = PDFDocument(format: .a4) + + // Act + document.createNewPage() + + // Assert + _ = try onlyObject(PDFPageBreakObject.self, in: document, container: .contentLeft) + } + + private func onlyObject(_ type: T.Type, in document: PDFDocument, container: PDFContainer) throws -> T { + try #require(document.objects.count == 1) + #expect(document.objects[0].0 == container) + return try #require(document.objects[0].1 as? T) + } + + private func loadImage() throws -> PDFImage { + let url = try #require(Bundle.module.url(forResource: "image-fixture", withExtension: "jpg")) + return PDFImage(image: try #require(Image(data: Data(contentsOf: url)))) + } +} diff --git a/Tests/TPPDFTests/PDFDocumentTests.swift b/Tests/TPPDFTests/PDFDocumentTests.swift new file mode 100644 index 00000000..eed69e2d --- /dev/null +++ b/Tests/TPPDFTests/PDFDocumentTests.swift @@ -0,0 +1,40 @@ +// +// PDFDocumentTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.04.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFDocumentTests { + @Test func initializingWithLayoutPreservesLayoutAndUsesDefaultMetadataPaginationAndNoObjects() { + // Arrange + let layout = PDFPageLayout() + let expectedInfo = PDFInfo() + let expectedPagination = PDFPagination() + + // Act + let document = PDFDocument(layout: layout) + + // Assert + #expect(document.layout == layout) + #expect(document.info == expectedInfo) + #expect(document.pagination == expectedPagination) + #expect(document.objects.isEmpty) + } + + @Test func initializingWithPageFormatUsesItsLayout() { + // Arrange + let format = PDFPageFormat.a4 + let expectedLayout = format.layout + + // Act + let document = PDFDocument(format: format) + + // Assert + #expect(document.layout == expectedLayout) + } +} diff --git a/Tests/TPPDFTests/PDFDocument_Spec.swift b/Tests/TPPDFTests/PDFDocument_Spec.swift deleted file mode 100644 index 1ec92bf9..00000000 --- a/Tests/TPPDFTests/PDFDocument_Spec.swift +++ /dev/null @@ -1,51 +0,0 @@ -// -// PDFDocument_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.04.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFDocument_Spec: QuickSpec { - override func spec() { - describe("PDFDocument") { - let layout = PDFPageLayout() - var document: PDFDocument! - - beforeEach { - document = PDFDocument(layout: layout) - } - - it("should be initialized with a layout") { - expect(document.layout) == layout - } - - it("can be initialized with a page format") { - document = PDFDocument(format: .a4) - expect(document.layout) == PDFPageFormat.a4.layout - } - - context("variables") { - it("should have layout") { - expect(document.layout).toNot(beNil()) - } - - it("should have info with default value") { - expect(document.info) == PDFInfo() - } - - it("should have pagination with default value") { - expect(document.pagination) == PDFPagination() - } - - it("should have array of objects") { - expect(document.objects).to(haveCount(0)) - } - } - } - } -} diff --git a/Tests/TPPDFTests/PDFGenerator+Debug_Spec.swift b/Tests/TPPDFTests/PDFGenerator+Debug_Spec.swift deleted file mode 100644 index dc60a09a..00000000 --- a/Tests/TPPDFTests/PDFGenerator+Debug_Spec.swift +++ /dev/null @@ -1,103 +0,0 @@ -// -// PDFGenerator+Debug_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 23.11.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Nimble -import Quick -@testable import TPPDF - -class PDFGenerator_Debug_Spec: QuickSpec { - override func spec() { - describe("PDFGenerator") { - context("Debug") { -// let document = PDFDocument(layout: PDFPageLayout(size: CGSize(width: 50, height: 50), -// margin: EdgeInsets(top: 10, left: 10, bottom: 10, right: 10), -// space: (header: 5, footer: 5))) -// let generator = PDFGenerator(document: document) -// -// it("should not draw overlay if debug is disabled") { -// -// UIGraphicsBeginImageContext(document.layout.size) -// guard let context = UIGraphicsGetCurrentContext() else { -// fail("Could not get graphics context") -// return -// } -// -// generator.debug = false -// generator.drawDebugPageOverlay(in: context) -// -// let image = UIGraphicsGetImageFromCurrentImageContext() -// UIGraphicsEndImageContext() -// -// let extractor = image?.pixelExtractor -// -// expect(image).toNot(beNil()) -// for x in 0.. is expected to equal <\(expected)>, got <\(result)>") -// -// // Early exit to only fail at first error! -// if result != expected { -// return -// } -// } -// } -// } -// } -// -// it("should draw margin lines") { -// UIGraphicsBeginImageContext(document.layout.size) -// guard let context = UIGraphicsGetCurrentContext() else { -// fail("Could not get graphics context") -// return -// } -// generator.debug = true -// generator.drawDebugPageOverlay(in: context) -// -// let image = UIGraphicsGetImageFromCurrentImageContext() -// UIGraphicsEndImageContext() -// -// let extractor = image?.pixelExtractor -// -// expect(image).toNot(beNil()) -// for x in 0.. is expected to equal <\(expected)>, got <\(result)>") -// -// // Early exit to only fail at first error! -// if result != expected { -// return -// } -// } -// } -// } -// } - } - } - } -} diff --git a/Tests/TPPDFTests/PDFGenerator+Generation_Spec.swift b/Tests/TPPDFTests/PDFGenerator+Generation_Spec.swift deleted file mode 100644 index eaa5b9a3..00000000 --- a/Tests/TPPDFTests/PDFGenerator+Generation_Spec.swift +++ /dev/null @@ -1,336 +0,0 @@ -// -// PDFGenerator+Generation_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 27.12.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Foundation -import Nimble -import Quick -@testable import TPPDF - -class PDFGenerator_Generation_Spec: QuickSpec { - // swiftlint:disable closure_body_length function_body_length - override func spec() { - describe("PDFGenerator") { - context("Generation") { - let document = PDFDocument(format: .a4) - context("URL") { - it("should generate and write to URL with PDF suffix") { - let filename = "FILENAME" - - var url: URL! - expect { - url = try PDFGenerator(document: document).generateURL(filename: filename) - }.toNot(throwError()) - expect(url).toEventuallyNot(beNil()) - - var writtenData: Data! - expect { - writtenData = try Data(contentsOf: url) - }.toNot(throwError()) - expect(writtenData).toEventuallyNot(beNil()) - - // let expectedBase64 = "" - // TODO: implement correct base64 - // expect(writtenData.base64EncodedString()).toEventually(equal(expectedBase64)) - } - - it("should generate and write to URL without PDF suffix") { - let filename = "FILENAME.pdf" - - var url: URL! - expect { - url = try PDFGenerator(document: document).generateURL(filename: filename) - }.toNot(throwError()) - expect(url).toEventuallyNot(beNil()) - - var writtenData: Data! - expect { - writtenData = try Data(contentsOf: url) - }.toNot(throwError()) - expect(writtenData).toEventuallyNot(beNil()) - - // let expectedBase64 = "" - // TODO: implement correct base64 - // expect(writtenData.base64EncodedString()).toEventually(equal(expectedBase64)) - } - - it("should generate with debug and write to URL") { - let filename = "FILENAME" - - var url: URL! - expect { - url = try PDFGenerator(document: document).generateURL(filename: filename) - }.toNot(throwError()) - expect(url).toEventuallyNot(beNil()) - - var writtenData: Data! - expect { - writtenData = try Data(contentsOf: url) - }.toNot(throwError()) - expect(writtenData).toEventuallyNot(beNil()) - - // let expectedBase64 = "" - // TODO: implement correct base64 - // expect(writtenData.base64EncodedString()).toEventually(equal(expectedBase64)) - } - } - - describe("data") { - it("should generate") { - // let filename = "FILENAME" - - var data: Data! - expect { - data = try PDFGenerator(document: document).generateData() - }.toNot(throwError()) - expect(data).toEventuallyNot(beNil()) - // let expectedBase64 = "" - // TODO: implement correct base64 - // expect(data()).toEventually(equal(expectedBase64)) - } - - it("should generate with debug") { - // let filename = "FILENAME" - - var data: Data! - expect { - data = try PDFGenerator(document: document).generateData() - }.toNot(throwError()) - expect(data).toEventuallyNot(beNil()) - // let expectedBase64 = "" - // TODO: implement correct base64 - // expect(data()).toEventually(equal(expectedBase64)) - } - } - - it("should generate pdf context") { - // TODO: test PDFGenerator.generatePDFContext - } - - it("should create render objects") { - // TODO: test PDFGenerator.createRenderObjects - } - - it("should add header footer objects") { - // TODO: test PDFGenerator.addHeaderFooterObjects - } - - it("should render objects") { - // TODO: test PDFGenerator.render(objects:) - } - - it("should render object") { - class CustomObject: PDFRenderObject { - nonisolated(unsafe) static var called = false - - override func draw(generator _: PDFGenerator, container _: PDFContainer, in _: PDFContext) throws { - CustomObject.called = true - } - } - - let obj = CustomObject() - - let document = PDFDocument(format: .a4) - let generator = PDFGenerator(document: document) - let context = PDFContextGraphics.createBitmapContext(size: .init(width: 100, height: 100))! - - expect(CustomObject.called).to(beFalse()) - - try? generator.render(object: obj, in: .headerLeft, in: .init(cgContext: context)) - - expect(CustomObject.called).to(beTrue()) - } - - context("extract") { - let objs: [PDFLocatedRenderObject] = [ - (PDFContainer.headerLeft, PDFSpaceObject(space: 10)), - (PDFContainer.headerLeft, PDFSpaceObject(space: 10)), - (PDFContainer.headerLeft, PDFSpaceObject(space: 10)), - - (PDFContainer.headerCenter, PDFSpaceObject(space: 10)), - (PDFContainer.headerCenter, PDFSpaceObject(space: 10)), - (PDFContainer.headerCenter, PDFSpaceObject(space: 10)), - - (PDFContainer.headerRight, PDFSpaceObject(space: 10)), - (PDFContainer.headerRight, PDFSpaceObject(space: 10)), - (PDFContainer.headerRight, PDFSpaceObject(space: 10)), - - (PDFContainer.contentLeft, PDFSpaceObject(space: 10)), - (PDFContainer.contentLeft, PDFSpaceObject(space: 10)), - - (PDFContainer.contentCenter, PDFSpaceObject(space: 10)), - (PDFContainer.contentCenter, PDFSpaceObject(space: 10)), - - (PDFContainer.contentRight, PDFSpaceObject(space: 10)), - (PDFContainer.contentRight, PDFSpaceObject(space: 10)), - - (PDFContainer.footerLeft, PDFSpaceObject(space: 10)), - (PDFContainer.footerCenter, PDFSpaceObject(space: 10)), - (PDFContainer.footerRight, PDFSpaceObject(space: 10)), - ] - let headerObjs = objs[0...8] - let contentObjs = objs[9...14] - let footerObjs = objs[15...17] - - it("can extract header objects") { - let resultObjs = PDFGenerator.extractHeaderObjects(objects: objs) - - expect(resultObjs.count) == headerObjs.count - } - - it("can extract footer objects") { - let resultObjs = PDFGenerator.extractFooterObjects(objects: objs) - - expect(resultObjs.count) == footerObjs.count - } - - it("can extract content objects") { - let resultObjs = PDFGenerator.extractContentObjects(objects: objs) - - expect(resultObjs.count) == contentObjs.count - } - - it("can extract content objects") { - let resultObjs = PDFGenerator.extractContentObjects(objects: objs) - - expect(resultObjs.count) == contentObjs.count - } - } - - describe("table of content") { - let headingStyle1 = document.add(style: PDFTextStyle(name: "Heading 1", font: Font.systemFont(ofSize: 25), color: Color.green)) - let headingStyle2 = document.add(style: PDFTextStyle(name: "Heading 2", font: Font.systemFont(ofSize: 20), color: Color.red)) - let headingStyle3 = document.add(style: PDFTextStyle(name: "Heading 3", font: Font.systemFont(ofSize: 18), color: Color.blue)) - let bodyStyle = document.add(style: PDFTextStyle(name: "Body", font: Font.systemFont(ofSize: 12), color: Color.orange)) - - let styles = [ - headingStyle1, - headingStyle2, - headingStyle3, - ].map(WeakPDFTextStyleRef.init(value:)) - - let textObjects = [ - PDFSimpleText(text: "Heading 1", style: headingStyle1), - PDFSimpleText(text: "Body", style: bodyStyle), - PDFSimpleText(text: "Body", style: bodyStyle), - - PDFSimpleText(text: "Heading 1.1", style: headingStyle2), - PDFSimpleText(text: "Body", style: bodyStyle), - PDFSimpleText(text: "Body", style: bodyStyle), - - PDFSimpleText(text: "Heading 2", style: headingStyle1), - PDFSimpleText(text: "Body", style: bodyStyle), - PDFSimpleText(text: "Body", style: bodyStyle), - - PDFSimpleText(text: "Heading 2.1", style: headingStyle2), - PDFSimpleText(text: "Body", style: bodyStyle), - PDFSimpleText(text: "Body", style: bodyStyle), - - PDFSimpleText(text: "Heading 2.1.1", style: headingStyle3), - PDFSimpleText(text: "Body", style: bodyStyle), - PDFSimpleText(text: "Body", style: bodyStyle), - - PDFSimpleText(text: "Heading 3", style: headingStyle1), - PDFSimpleText(text: "Body", style: bodyStyle), - PDFSimpleText(text: "Body", style: bodyStyle), - - PDFSimpleText(text: "Heading 3.1.1", style: headingStyle3), - PDFSimpleText(text: "Body", style: bodyStyle), - PDFSimpleText(text: "Body", style: bodyStyle), - ] - let objs: [PDFLocatedRenderObject] = textObjects.map { (PDFContainer.contentLeft, PDFAttributedTextObject(simpleText: $0)) } - - it("should create list based on styles") { - let list = PDFGenerator.createTableOfContentList(objects: objs, styles: styles, symbol: .dot) - let expectedList = PDFList(indentations: [ - (pre: CGFloat(10), past: CGFloat(10)), - (pre: CGFloat(20), past: CGFloat(10)), - (pre: CGFloat(30), past: CGFloat(10)), - ]) - expectedList.addItems([ - PDFListItem(symbol: .dot, content: textObjects[0].text) - .addItem(PDFListItem(symbol: .dot, content: textObjects[3].text)), - PDFListItem(symbol: .dot, content: textObjects[6].text) - .addItem(PDFListItem(symbol: .dot, content: textObjects[9].text) - .addItem(PDFListItem(symbol: .dot, content: textObjects[12].text))), - PDFListItem(symbol: .dot, content: textObjects[15].text) - .addItem(PDFListItem(symbol: .none, content: nil) - .addItem(PDFListItem(symbol: .dot, content: textObjects[18].text))), - ]) - - expect(list).to(equal(expectedList)) - } - } - } - - describe("total page estimation") { - // Test Case: - // Precondition: - // - Document contains an external document - // Expected Result: - // - Same page count as external document - // Notes: - // - none - context("only external document") { - it("should return correct total pages count") { - let document = PDFDocument(format: .a4) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "sample", withExtension: "pdf")!)) - - let generator = PDFGenerator(document: document) - _ = try generator.generateData() - - expect(generator.totalPages) == 4 - } - } - - // Test Case: - // Precondition: - // - Document contains an external document - // - Document contains text after document - // Expected Result: - // - Same page count as external document plus one - // Notes: - // - none - context("external document and text") { - it("should return correct total pages count") { - let document = PDFDocument(format: .a4) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "sample", withExtension: "pdf")!)) - document.add(text: "Some text after document") - - let generator = PDFGenerator(document: document) - _ = try generator.generateData() - - expect(generator.totalPages) == 5 - } - } - - // Test Case: - // Precondition: - // - Document contains text - // - Document contains an external document after text - // Expected Result: - // - Same page count as external document plus one - // Notes: - // - none - context("external document and text") { - it("should return correct total pages count") { - let document = PDFDocument(format: .a4) - document.add(text: "Some text after document") - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "sample", withExtension: "pdf")!)) - - let generator = PDFGenerator(document: document) - _ = try generator.generateData() - - expect(generator.totalPages) == 5 - } - } - } - } - } - // swiftlint:enable closure_body_length function_body_length -} diff --git a/Tests/TPPDFTests/PDFGenerator+Layout_Spec.swift b/Tests/TPPDFTests/PDFGenerator+Layout_Spec.swift deleted file mode 100644 index 974d6699..00000000 --- a/Tests/TPPDFTests/PDFGenerator+Layout_Spec.swift +++ /dev/null @@ -1,34 +0,0 @@ -// -// PDFGenerator+Layout_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.12.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Nimble -import Quick -@testable import TPPDF - -class PDFGenerator_Layout_Spec: QuickSpec { - override func spec() { - describe("PDFGenerator+Layout") { - let document = PDFDocument(format: .a4) - let generator = PDFGenerator(document: document) - - let container = PDFContainer.contentCenter - - it("can get the current content offset of a container") { - expect(generator.getContentOffset(in: container)) == generator.layout.getContentOffset(in: container) - } - - it("can set the current content offset of a container") { - let newValue: CGFloat = 20.0 - - generator.setContentOffset(in: container, to: newValue) - expect(generator.getContentOffset(in: container)) == newValue - } - } - } -} diff --git a/Tests/TPPDFTests/PDFGeneratorGenerationTests.swift b/Tests/TPPDFTests/PDFGeneratorGenerationTests.swift new file mode 100644 index 00000000..a0e35c88 --- /dev/null +++ b/Tests/TPPDFTests/PDFGeneratorGenerationTests.swift @@ -0,0 +1,145 @@ +// +// PDFGeneratorGenerationTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 27.12.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Foundation +import Testing +@testable import TPPDF + +struct PDFGeneratorGenerationTests { + @Test(arguments: [(false, false), (true, false), (false, true)]) + func generatingURLWritesReadablePDFWithOrWithoutFilenameSuffix(suffix: Bool, debug: Bool) throws { + // Arrange + let generator = PDFGenerator(document: PDFDocument(format: .a4)) + generator.debug = debug + let filename = UUID().uuidString + (suffix ? ".pdf" : "") + + // Act + let url = try generator.generateURL(filename: filename) + defer { try? FileManager.default.removeItem(at: url) } + let data = try Data(contentsOf: url) + + // Assert + #expect(!data.isEmpty) + #expect(url.pathExtension == "pdf") + } + + @Test(arguments: [false, true]) + func generatingDataProducesPDFWithOrWithoutDebugOverlay(debug: Bool) throws { + // Arrange + let generator = PDFGenerator(document: PDFDocument(format: .a4)) + generator.debug = debug + + // Act + let data = try generator.generateData() + + // Assert + #expect(!data.isEmpty) + } + + @Test func renderingObjectInvokesItsDrawImplementation() throws { + // Arrange + let object = DrawingObject() + let generator = PDFGenerator(document: PDFDocument(format: .a4)) + let optionalContext = PDFContextGraphics.createBitmapContext(size: CGSize(width: 100, height: 100)) + let context = try #require(optionalContext) + let wasCalled = object.called + + // Act + try generator.render(object: object, in: .headerLeft, in: PDFContext(cgContext: context)) + + // Assert + #expect(!wasCalled) + #expect(object.called) + } + + @Test(arguments: [("header", 9), ("content", 6), ("footer", 3)]) + func extractingRegionReturnsItsObjects(region: String, expectedCount: Int) { + // Arrange + let containers: [PDFContainer] = [ + .headerLeft, .headerLeft, .headerLeft, .headerCenter, .headerCenter, .headerCenter, + .headerRight, .headerRight, .headerRight, .contentLeft, .contentLeft, + .contentCenter, .contentCenter, .contentRight, .contentRight, .footerLeft, .footerCenter, .footerRight, + ] + let objects: [PDFLocatedRenderObject] = containers.map { ($0, PDFSpaceObject(space: 10)) } + + // Act + let extracted: [PDFLocatedRenderObject] + switch region { + case "header": extracted = PDFGenerator.extractHeaderObjects(objects: objects) + case "footer": extracted = PDFGenerator.extractFooterObjects(objects: objects) + default: extracted = PDFGenerator.extractContentObjects(objects: objects) + } + + // Assert + #expect(extracted.count == expectedCount) + } + + @Test func tableOfContentsUsesHeadingStylesAndPreservesNestedLevels() { + // Arrange + let document = PDFDocument(format: .a4) + let heading1 = document.add(style: PDFTextStyle(name: "Heading 1", font: Font.systemFont(ofSize: 25), color: .green)) + let heading2 = document.add(style: PDFTextStyle(name: "Heading 2", font: Font.systemFont(ofSize: 20), color: .red)) + let heading3 = document.add(style: PDFTextStyle(name: "Heading 3", font: Font.systemFont(ofSize: 18), color: .blue)) + let body = document.add(style: PDFTextStyle(name: "Body", font: Font.systemFont(ofSize: 12), color: .orange)) + let styles = [heading1, heading2, heading3].map(WeakPDFTextStyleRef.init(value:)) + let headings = [ + ("Heading 1", heading1), ("Heading 1.1", heading2), ("Heading 2", heading1), + ("Heading 2.1", heading2), ("Heading 2.1.1", heading3), ("Heading 3", heading1), ("Heading 3.1.1", heading3), + ] + let textObjects = headings.flatMap { heading in + [PDFSimpleText(text: heading.0, style: heading.1), PDFSimpleText(text: "Body", style: body), + PDFSimpleText(text: "Body", style: body)] + } + let objects: [PDFLocatedRenderObject] = textObjects.map { (.contentLeft, PDFAttributedTextObject(simpleText: $0)) } + let expectedList = PDFList(indentations: [(pre: 10, past: 10), (pre: 20, past: 10), (pre: 30, past: 10)]) + expectedList.addItems([ + PDFListItem(symbol: .dot, content: "Heading 1") + .addItem(PDFListItem(symbol: .dot, content: "Heading 1.1")), + PDFListItem(symbol: .dot, content: "Heading 2") + .addItem(PDFListItem(symbol: .dot, content: "Heading 2.1") + .addItem(PDFListItem(symbol: .dot, content: "Heading 2.1.1"))), + PDFListItem(symbol: .dot, content: "Heading 3") + .addItem(PDFListItem(symbol: .none, content: nil) + .addItem(PDFListItem(symbol: .dot, content: "Heading 3.1.1"))), + ]) + + // Act + let list = withExtendedLifetime(document) { + PDFGenerator.createTableOfContentList(objects: objects, styles: styles, symbol: .dot) + } + + // Assert + #expect(list == expectedList) + } + + @Test(arguments: [("none", 4), ("after", 5), ("before", 5)]) + func totalPageCountIncludesExternalDocumentAndSurroundingText(position: String, expectedPages: Int) throws { + // Arrange + let document = PDFDocument(format: .a4) + let url = try #require(Bundle.module.url(forResource: "sample", withExtension: "pdf")) + if position == "before" { document.add(text: "Some text after document") } + document.add(externalDocument: .init(url: url)) + if position == "after" { document.add(text: "Some text after document") } + let generator = PDFGenerator(document: document) + + // Act + _ = try generator.generateData() + + // Assert + #expect(generator.totalPages == expectedPages) + } + + private final class DrawingObject: PDFRenderObject { + var called = false + + override func draw(generator _: PDFGenerator, container _: PDFContainer, in _: PDFContext) throws { + called = true + } + } +} diff --git a/Tests/TPPDFTests/PDFGeneratorLayoutTests.swift b/Tests/TPPDFTests/PDFGeneratorLayoutTests.swift new file mode 100644 index 00000000..271588d2 --- /dev/null +++ b/Tests/TPPDFTests/PDFGeneratorLayoutTests.swift @@ -0,0 +1,40 @@ +// +// PDFGeneratorLayoutTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.12.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFGeneratorLayoutTests { + @Test func gettingContentOffsetDelegatesToLayout() { + // Arrange + let generator = PDFGenerator(document: PDFDocument(format: .a4)) + let container = PDFContainer.contentCenter + let expectedOffset = generator.layout.getContentOffset(in: container) + + // Act + let offset = generator.getContentOffset(in: container) + + // Assert + #expect(offset == expectedOffset) + } + + @Test func settingContentOffsetUpdatesContainer() { + // Arrange + let generator = PDFGenerator(document: PDFDocument(format: .a4)) + let container = PDFContainer.contentCenter + let value: CGFloat = 20 + + // Act + generator.setContentOffset(in: container, to: value) + let offset = generator.getContentOffset(in: container) + + // Assert + #expect(offset == value) + } +} diff --git a/Tests/TPPDFTests/PDFGeneratorTests.swift b/Tests/TPPDFTests/PDFGeneratorTests.swift new file mode 100644 index 00000000..310584ed --- /dev/null +++ b/Tests/TPPDFTests/PDFGeneratorTests.swift @@ -0,0 +1,58 @@ +// +// PDFGeneratorTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.09.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFGeneratorTests { + @Test func initializingGeneratorPreservesDocumentAndUsesEmptyLayoutAndFirstPage() { + // Arrange + let document = PDFDocument(format: .a4) + let expectedLayout = PDFLayout() + + // Act + let generator = PDFGenerator(document: document) + + // Assert + #expect(generator.document === document) + #expect(generator.headerFooterObjects.isEmpty) + #expect(generator.layout == expectedLayout) + #expect(generator.currentPage == 1) + #expect(generator.totalPages == 1) + } + + @Test(arguments: 0..<10) + func everyContainerUsesDefaultFont(index: Int) { + // Arrange + let generator = PDFGenerator(document: PDFDocument(format: .a4)) + let containers: [PDFContainer] = [.none] + PDFContainer.all + let expectedFont = Font.systemFont(ofSize: PDFConstants.defaultFontSize) + + // Act + let font = generator.fonts[containers[index]] + + // Assert + #expect(font == expectedFont) + } + + @Test func resettingGeneratorRestoresFirstPageAndEmptyLayout() { + // Arrange + let generator = PDFGenerator(document: PDFDocument(format: .a4)) + generator.currentPage = 20 + generator.layout.heights.add(30, to: .contentLeft) + generator.layout.indentation.setLeft(indentation: 45, in: .contentLeft) + + // Act + generator.resetGenerator() + + // Assert + #expect(generator.currentPage == 1) + #expect(generator.layout.heights == PDFLayoutHeights()) + #expect(generator.layout.indentation == PDFLayoutIndentations()) + } +} diff --git a/Tests/TPPDFTests/PDFGenerator_Spec.swift b/Tests/TPPDFTests/PDFGenerator_Spec.swift deleted file mode 100644 index 135079d3..00000000 --- a/Tests/TPPDFTests/PDFGenerator_Spec.swift +++ /dev/null @@ -1,66 +0,0 @@ -// -// PDFGenerator_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.09.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFGenerator_Spec: QuickSpec { - // swiftlint:disable closure_body_length - override func spec() { - describe("PDFGenerator") { - let document = PDFDocument(format: .a4) - let generator = PDFGenerator(document: document) - - context("variables") { - it("has a document") { - expect(generator.document).toNot(beNil()) - } - - it("has a header footer objects list") { - expect(generator.headerFooterObjects).to(beEmpty()) - } - - it("has a layout") { - expect(generator.layout) == PDFLayout() - } - - it("has a currentPage and a totalPages") { - expect(generator.currentPage) == 1 - expect(generator.totalPages) == 1 - } - - it("has a lazy font per container dictionary") { - expect(generator.fonts[PDFContainer.none]) == Font.systemFont(ofSize: PDFConstants.defaultFontSize) - expect(generator.fonts[PDFContainer.headerLeft]) == Font.systemFont(ofSize: PDFConstants.defaultFontSize) - expect(generator.fonts[PDFContainer.headerCenter]) == Font.systemFont(ofSize: PDFConstants.defaultFontSize) - expect(generator.fonts[PDFContainer.headerRight]) == Font.systemFont(ofSize: PDFConstants.defaultFontSize) - expect(generator.fonts[PDFContainer.contentLeft]) == Font.systemFont(ofSize: PDFConstants.defaultFontSize) - expect(generator.fonts[PDFContainer.contentCenter]) == Font.systemFont(ofSize: PDFConstants.defaultFontSize) - expect(generator.fonts[PDFContainer.contentRight]) == Font.systemFont(ofSize: PDFConstants.defaultFontSize) - expect(generator.fonts[PDFContainer.footerLeft]) == Font.systemFont(ofSize: PDFConstants.defaultFontSize) - expect(generator.fonts[PDFContainer.footerCenter]) == Font.systemFont(ofSize: PDFConstants.defaultFontSize) - expect(generator.fonts[PDFContainer.footerRight]) == Font.systemFont(ofSize: PDFConstants.defaultFontSize) - } - } - - it("can be resetted") { - generator.currentPage = 20 - generator.layout.heights.add(30, to: .contentLeft) - generator.layout.indentation.setLeft(indentation: 45, in: .contentLeft) - - generator.resetGenerator() - - expect(generator.currentPage) == 1 - expect(generator.layout.heights) == PDFLayoutHeights() - expect(generator.layout.indentation) == PDFLayoutIndentations() - } - } - } - // swiftlint:enable closure_body_length -} diff --git a/Tests/TPPDFTests/Table/PDFTable+Equatable_Spec.swift b/Tests/TPPDFTests/Table/PDFTable+Equatable_Spec.swift deleted file mode 100644 index b9c8022a..00000000 --- a/Tests/TPPDFTests/Table/PDFTable+Equatable_Spec.swift +++ /dev/null @@ -1,72 +0,0 @@ -// -// PDFTable+Equatable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF -import XCTest - -class PDFTable_Equatable_Spec: QuickSpec { - // swiftlint:disable closure_body_length - override func spec() { - describe("PDFTable") { - context("Equatable") { - var table: PDFTable! - - beforeEach { - table = PDFTable(rows: 0, columns: 0) - } - - it("is equal") { - let otherTable = PDFTable(rows: 0, columns: 0) - expect(table) == otherTable - } - - it("is not equal with different style") { - let otherTable = PDFTable(rows: 0, columns: 0) - otherTable.style = PDFTableStyle(rowHeaderCount: 2) - expect(table) != otherTable - } - - it("is not equal with different cells") { - let otherTable = PDFTable(rows: 0, columns: 0) - otherTable.cells = [[PDFTableCell(), PDFTableCell()]] - expect(table) != otherTable - - table.cells = [[PDFTableCell(content: try XCTUnwrap(PDFTableContent(content: "TEST"))), PDFTableCell()]] - expect(table) != otherTable - } - - it("is not equal with different widths") { - let otherTable = PDFTable(rows: 0, columns: 0) - otherTable.widths = [0.25, 0.75] - expect(table) != otherTable - } - - it("is not equal with different padding") { - let otherTable = PDFTable(rows: 0, columns: 0) - otherTable.padding = 100 - expect(table) != otherTable - } - - it("is not equal with different margin") { - let otherTable = PDFTable(rows: 0, columns: 0) - otherTable.margin = 100 - expect(table) != otherTable - } - - it("is not equal with different showHeadersOnEveryPage") { - let otherTable = PDFTable(rows: 0, columns: 0) - otherTable.showHeadersOnEveryPage = !table.showHeadersOnEveryPage - expect(table) != otherTable - } - } - } - } - // swiftlint:enable closure_body_length -} diff --git a/Tests/TPPDFTests/Table/PDFTableCell+Equatable_Spec.swift b/Tests/TPPDFTests/Table/PDFTableCell+Equatable_Spec.swift deleted file mode 100644 index 6940523e..00000000 --- a/Tests/TPPDFTests/Table/PDFTableCell+Equatable_Spec.swift +++ /dev/null @@ -1,43 +0,0 @@ -// -// PDFTableCell+Equatable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.16.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF -import XCTest - -class PDFTableCell_Equatable_Spec: QuickSpec { - override func spec() { - describe("PDFTableCell") { - context("Equatable") { - let cell = PDFTableCell() - - it("is equal") { - let otherCell = PDFTableCell() - expect(cell) == otherCell - } - - it("is not equal with different content") { - let otherCell = PDFTableCell(content: try XCTUnwrap(PDFTableContent(content: "123"))) - expect(cell) != otherCell - } - - it("is not equal with different alignment") { - let otherCell = PDFTableCell(alignment: PDFTableCellAlignment.bottomRight) - expect(cell) != otherCell - } - - it("is not equal with different style") { - let style = PDFTableCellStyle(colors: (fill: .green, text: .orange)) - let otherCell = PDFTableCell(style: style) - expect(cell) != otherCell - } - } - } - } -} diff --git a/Tests/TPPDFTests/Table/PDFTableCellEquatableTests.swift b/Tests/TPPDFTests/Table/PDFTableCellEquatableTests.swift new file mode 100644 index 00000000..6fbd58b5 --- /dev/null +++ b/Tests/TPPDFTests/Table/PDFTableCellEquatableTests.swift @@ -0,0 +1,33 @@ +// +// PDFTableCellEquatableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.16.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFTableCellEquatableTests { + @Test(arguments: ["same", "content", "alignment", "style"]) + func equalityDependsOnContentAlignmentAndStyle(property: String) throws { + // Arrange + let cell = PDFTableCell() + let otherCell: PDFTableCell + switch property { + case "content": + let content = try PDFTableContent(content: "123") + otherCell = PDFTableCell(content: try #require(content)) + case "alignment": otherCell = PDFTableCell(alignment: .bottomRight) + case "style": otherCell = PDFTableCell(style: PDFTableCellStyle(colors: (fill: .green, text: .orange))) + default: otherCell = PDFTableCell() + } + + // Act + let isEqual = cell == otherCell + + // Assert + #expect(isEqual == (property == "same")) + } +} diff --git a/Tests/TPPDFTests/Table/PDFTableCellPosition+Equatable_Spec.swift b/Tests/TPPDFTests/Table/PDFTableCellPosition+Equatable_Spec.swift deleted file mode 100644 index 3e03a87c..00000000 --- a/Tests/TPPDFTests/Table/PDFTableCellPosition+Equatable_Spec.swift +++ /dev/null @@ -1,36 +0,0 @@ -// -// PDFTableCellPosition+Equatable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.16.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFTableCellPosition_Equatable_Spec: QuickSpec { - override func spec() { - describe("PDFTableCellPosition") { - context("Equatable") { - let position = PDFTableCellPosition(row: 1, column: 2) - - it("is equal") { - let otherPosition = PDFTableCellPosition(row: 1, column: 2) - expect(position) == otherPosition - } - - it("is not equal with different row") { - let otherPosition = PDFTableCellPosition(row: 2, column: 2) - expect(position) != otherPosition - } - - it("is not equal with different column") { - let otherPosition = PDFTableCellPosition(row: 1, column: 3) - expect(position) != otherPosition - } - } - } - } -} diff --git a/Tests/TPPDFTests/Table/PDFTableCellPositionEquatableTests.swift b/Tests/TPPDFTests/Table/PDFTableCellPositionEquatableTests.swift new file mode 100644 index 00000000..2f147374 --- /dev/null +++ b/Tests/TPPDFTests/Table/PDFTableCellPositionEquatableTests.swift @@ -0,0 +1,25 @@ +// +// PDFTableCellPositionEquatableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.16.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFTableCellPositionEquatableTests { + @Test(arguments: [(1, 2, true), (2, 2, false), (1, 3, false)]) + func equalityDependsOnBothCoordinates(row: Int, column: Int, expectedEquality: Bool) { + // Arrange + let position = PDFTableCellPosition(row: 1, column: 2) + let otherPosition = PDFTableCellPosition(row: row, column: column) + + // Act + let isEqual = position == otherPosition + + // Assert + #expect(isEqual == expectedEquality) + } +} diff --git a/Tests/TPPDFTests/Table/PDFTableCellPositionHashableTests.swift b/Tests/TPPDFTests/Table/PDFTableCellPositionHashableTests.swift new file mode 100644 index 00000000..93d7cc3d --- /dev/null +++ b/Tests/TPPDFTests/Table/PDFTableCellPositionHashableTests.swift @@ -0,0 +1,27 @@ +// +// PDFTableCellPositionHashableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.16.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFTableCellPositionHashableTests { + @Test func hashingPositionIsStableAndDistinguishesDifferentCoordinates() { + // Arrange + let position = PDFTableCellPosition(row: 1, column: 2) + let otherPosition = PDFTableCellPosition(row: 4, column: 9) + + // Act + let hash = position.hashValue + let repeatedHash = position.hashValue + let otherHash = otherPosition.hashValue + + // Assert + #expect(hash == repeatedHash) + #expect(hash != otherHash) + } +} diff --git a/Tests/TPPDFTests/Table/PDFTableCellPositionTests.swift b/Tests/TPPDFTests/Table/PDFTableCellPositionTests.swift new file mode 100644 index 00000000..fa0ce66b --- /dev/null +++ b/Tests/TPPDFTests/Table/PDFTableCellPositionTests.swift @@ -0,0 +1,25 @@ +// +// PDFTableCellPositionTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.12.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFTableCellPositionTests { + @Test func initializingWithCoordinatesPreservesRowAndColumn() { + // Arrange + let row = 2 + let column = 5 + + // Act + let position = PDFTableCellPosition(row: row, column: column) + + // Assert + #expect(position.row == row) + #expect(position.column == column) + } +} diff --git a/Tests/TPPDFTests/Table/PDFTableCellPosition_Hashable_Spec.swift b/Tests/TPPDFTests/Table/PDFTableCellPosition_Hashable_Spec.swift deleted file mode 100644 index 96251261..00000000 --- a/Tests/TPPDFTests/Table/PDFTableCellPosition_Hashable_Spec.swift +++ /dev/null @@ -1,26 +0,0 @@ -// -// PDFTableCellPosition_Hashable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.16.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFTableCellPosition_Hashable_Spec: QuickSpec { - override func spec() { - describe("PDFTableCellPosition") { - context("Hashable") { - it("can create a hashvalue") { - let position1 = PDFTableCellPosition(row: 1, column: 2) - let position2 = PDFTableCellPosition(row: 4, column: 9) - expect(position1.hashValue) == position1.hashValue - expect(position1.hashValue) != position2.hashValue - } - } - } - } -} diff --git a/Tests/TPPDFTests/Table/PDFTableCellPosition_Spec.swift b/Tests/TPPDFTests/Table/PDFTableCellPosition_Spec.swift deleted file mode 100644 index c258830e..00000000 --- a/Tests/TPPDFTests/Table/PDFTableCellPosition_Spec.swift +++ /dev/null @@ -1,38 +0,0 @@ -// -// PDFTableCellPosition_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.12.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFTableCellPosition_Spec: QuickSpec { - override func spec() { - describe("PDFTableCellPosition") { - let row = 2 - let column = 5 - let position = PDFTableCellPosition(row: row, column: column) - - context("variables") { - it("has a row") { - expect(position.row).toNot(beNil()) - } - - it("has a column") { - expect(position.column).toNot(beNil()) - } - } - - context("initializer") { - it("can be initialized with a row and a column") { - expect(position.row) == row - expect(position.column) == column - } - } - } - } -} diff --git a/Tests/TPPDFTests/Table/PDFTableCellTests.swift b/Tests/TPPDFTests/Table/PDFTableCellTests.swift new file mode 100644 index 00000000..b0f88825 --- /dev/null +++ b/Tests/TPPDFTests/Table/PDFTableCellTests.swift @@ -0,0 +1,59 @@ +// +// PDFTableCellTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.16.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFTableCellTests { + @Test func initializingCellUsesNoContentOrStyleAndCenterAlignment() { + // Arrange + let expectedAlignment = PDFTableCellAlignment.center + + // Act + let cell = PDFTableCell() + + // Assert + #expect(cell.content == nil) + #expect(cell.style == nil) + #expect(cell.alignment == expectedAlignment) + } + + @Test func initializingWithContentPreservesContent() throws { + // Arrange + let optionalContent = try PDFTableContent(content: "EXAMPLE") + let content = try #require(optionalContent) + + // Act + let cell = PDFTableCell(content: content) + + // Assert + #expect(cell.content == content) + } + + @Test func initializingWithAlignmentPreservesAlignment() { + // Arrange + let alignment = PDFTableCellAlignment.bottomRight + + // Act + let cell = PDFTableCell(alignment: alignment) + + // Assert + #expect(cell.alignment == alignment) + } + + @Test func initializingWithStylePreservesStyle() { + // Arrange + let style = PDFTableCellStyle(colors: (fill: .blue, text: .green)) + + // Act + let cell = PDFTableCell(style: style) + + // Assert + #expect(cell.style == style) + } +} diff --git a/Tests/TPPDFTests/Table/PDFTableCell_Spec.swift b/Tests/TPPDFTests/Table/PDFTableCell_Spec.swift deleted file mode 100644 index 73c959cb..00000000 --- a/Tests/TPPDFTests/Table/PDFTableCell_Spec.swift +++ /dev/null @@ -1,57 +0,0 @@ -// -// PDFTableCell_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.16.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF -import XCTest - -class PDFTableCell_Spec: QuickSpec { - override func spec() { - describe("PDFTableCell") { - context("variables") { - let cell = PDFTableCell() - - it("has an optional content") { - expect(cell.content).to(beNil()) - } - - it("has an optional style") { - expect(cell.style).to(beNil()) - } - - it("has a default alignment") { - expect(cell.alignment) == PDFTableCellAlignment.center - } - } - - context("initializer") { - it("can be initialized with a content") { - let content = try XCTUnwrap(PDFTableContent(content: "EXAMPLE")) - let cell = PDFTableCell(content: content) - - expect(cell.content) == content - } - - it("can be initialized with an alignment") { - let alignment = PDFTableCellAlignment.bottomRight - let cell = PDFTableCell(alignment: alignment) - - expect(cell.alignment) == alignment - } - - it("can be initialized with a style") { - let style = PDFTableCellStyle(colors: (fill: Color.blue, text: Color.green)) - let cell = PDFTableCell(style: style) - - expect(cell.style) == style - } - } - } - } -} diff --git a/Tests/TPPDFTests/Table/PDFTableContent+Equatable_Spec.swift b/Tests/TPPDFTests/Table/PDFTableContent+Equatable_Spec.swift deleted file mode 100644 index 1533b8b9..00000000 --- a/Tests/TPPDFTests/Table/PDFTableContent+Equatable_Spec.swift +++ /dev/null @@ -1,89 +0,0 @@ -// -// PDFTableContent+Equatable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.16.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Foundation -import Nimble -import Quick -@testable import TPPDF - -class PDFTableContent_Equatable_Spec: QuickSpec { - // swiftlint:disable closure_body_length function_body_length - override func spec() { - describe("PDFTableContent") { - context("Equatable") { - var content: PDFTableContent! - - beforeEach { - content = PDFTableContent(type: .none, content: nil) - } - - it("is equal") { - let otherContent = PDFTableContent(type: .none, content: nil) - expect(content) == otherContent - } - - it("is not equal with different type") { - let otherContent = PDFTableContent(type: .string, content: nil) - expect(content) != otherContent - } - - it("is not equal with different strings") { - content.type = .string - content.content = "EXAMPLE" - let otherContent = PDFTableContent(type: .string, content: "INVALID") - expect(content) != otherContent - } - - it("is not equal with different attributed strings") { - content.type = .attributedString - content.content = NSAttributedString(string: "EXAMPLE") - - let otherContent = PDFTableContent(type: .attributedString, content: NSAttributedString(string: "INVALID")) - expect(content) != otherContent - } - - it("is not equal with different images") { - content.type = .image - content.content = Image() - - // swiftlint:disable line_length - let base64Data = """ - iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAAG0OVFdAAAAAXNSR0IArs4c6QAAAIRlWElmTU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAABIAAAAAQAAAEgAAAABAAOgAQADAAAAAQABAACgAgAEAA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llTv/LqjZzS5ATaFsBXP2CbeL8C6w33/TKvLSvWfhoaZ1osgGKEzKSBoX5ZpwGIsl6cWT5AWixAOqYbVZtGv+ZahR1ALB8f0Cw1c6jXQKW+xUQG3PF6ykuh556LiwcwqDV7H5/6DM4plrwV4BXyUeTkiCLVIS1dALvzdlzU7ijW5Z8KZgkoWaGd8J7yloky0jMMNPN7KsUlgXRUurPI9EwEOUs89dyvJHDKAvSr6kg/MacUIP0y71clnuoC+lV1pJ+YUxYg/TLvVyX+F6HKI6BucPebAAAAAElFTkSuQmCC - """ - // swiftlint:enable line_length - let data = Data(base64Encoded: base64Data)! - let otherContent = PDFTableContent(type: .image, content: Image(data: data)!) - expect(content) != otherContent - } - - it("is not equal with different content nil") { - content.type = .image - content.content = Image() - - var otherContent = PDFTableContent(type: .image, content: nil) - expect(content) != otherContent - - content.type = .image - content.content = nil - - otherContent = PDFTableContent(type: .image, content: Image()) - expect(content) != otherContent - } - - it("ignores content if unknown objects") { - content.type = .image - content.content = ["RANDOM"] - - let otherContent = PDFTableContent(type: .image, content: ["RANDOM"]) - expect(content) == otherContent - } - } - } - } - // swiftlint:enable closure_body_length function_body_length -} diff --git a/Tests/TPPDFTests/Table/PDFTableContentEquatableTests.swift b/Tests/TPPDFTests/Table/PDFTableContentEquatableTests.swift new file mode 100644 index 00000000..0e7bbb71 --- /dev/null +++ b/Tests/TPPDFTests/Table/PDFTableContentEquatableTests.swift @@ -0,0 +1,70 @@ +// +// PDFTableContentEquatableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.16.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Foundation +import Testing +@testable import TPPDF + +struct PDFTableContentEquatableTests { + @Test(arguments: ["same", "type", "string", "attributedString", "unknownObjects"]) + func equalityDependsOnRecognizedTypeAndContentButIgnoresUnknownObjects(kind: String) { + // Arrange + let content = PDFTableContent(type: .none, content: nil) + let otherContent: PDFTableContent + switch kind { + case "type": otherContent = PDFTableContent(type: .string, content: nil) + case "string": + content.type = .string + content.content = "EXAMPLE" + otherContent = PDFTableContent(type: .string, content: "INVALID") + case "attributedString": + content.type = .attributedString + content.content = NSAttributedString(string: "EXAMPLE") + otherContent = PDFTableContent(type: .attributedString, content: NSAttributedString(string: "INVALID")) + case "unknownObjects": + content.type = .image + content.content = ["RANDOM"] + otherContent = PDFTableContent(type: .image, content: ["RANDOM"]) + default: otherContent = PDFTableContent(type: .none, content: nil) + } + + // Act + let isEqual = content == otherContent + + // Assert + #expect(isEqual == (kind == "same" || kind == "unknownObjects")) + } + + @Test func differentImagesMakeContentUnequal() throws { + // Arrange + let url = try #require(Bundle.module.url(forResource: "table-content-fixture", withExtension: "png")) + let image = try #require(Image(data: Data(contentsOf: url))) + let content = PDFTableContent(type: .image, content: Image()) + let otherContent = PDFTableContent(type: .image, content: image) + + // Act + let isEqual = content == otherContent + + // Assert + #expect(!isEqual) + } + + @Test(arguments: [false, true]) + func missingImageContentOnEitherSideMakesContentUnequal(missingOnLeft: Bool) { + // Arrange + let image = Image() + let content = PDFTableContent(type: .image, content: missingOnLeft ? nil : image) + let otherContent = PDFTableContent(type: .image, content: missingOnLeft ? image : nil) + + // Act + let isEqual = content == otherContent + + // Assert + #expect(!isEqual) + } +} diff --git a/Tests/TPPDFTests/Table/PDFTableContentTests.swift b/Tests/TPPDFTests/Table/PDFTableContentTests.swift new file mode 100644 index 00000000..5fa6f932 --- /dev/null +++ b/Tests/TPPDFTests/Table/PDFTableContentTests.swift @@ -0,0 +1,213 @@ +// +// PDFTableContentTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.16.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Foundation +import Testing +@testable import TPPDF + +struct PDFTableContentTests { + @Test func initializingWithNilUsesNoneTypeAndNoContent() throws { + // Arrange + let value: Any? = nil + + // Act + let optionalContent = try PDFTableContent(content: value) + + // Assert + let content = try #require(optionalContent) + #expect(content.type == .none) + #expect(content.content == nil) + } + + @Test func initializingWithNumberAcceptsNumericContent() throws { + // Arrange + let value = 123 + + // Act + let content = try PDFTableContent(content: value) + + // Assert + #expect(content != nil) + } + + @Test func initializingWithUnsupportedContentThrows() { + // Arrange + let value = ["EXAMPLE"] + + // Act and Assert + #expect(throws: (any Error).self) { + try PDFTableContent(content: value) + } + } + + @Test func initializingWithExplicitTypePreservesAttributedContent() { + // Arrange + let text = NSAttributedString(string: "EXAMPLE") + + // Act + let content = PDFTableContent(type: .attributedString, content: text) + + // Assert + #expect(content.type == .attributedString) + #expect(content.content as? NSAttributedString == text) + } + + @Test func settingNilContentUsesNoneTypeAndNoValue() throws { + // Arrange + let optionalContent = try PDFTableContent(content: nil) + let content = try #require(optionalContent) + + // Act + try content.setContent(content: nil) + + // Assert + #expect(content.type == .none) + #expect(content.content == nil) + } + + @Test(arguments: ["string", "image", "attributedString", "int", "double", "float"]) + func settingSupportedContentUpdatesTypeAndStoredValue(kind: String) throws { + // Arrange + let optionalContent = try PDFTableContent(content: nil) + let content = try #require(optionalContent) + let image = Image() + let text = NSAttributedString(string: "EXAMPLE") + let value: Any? + let expectedType: PDFTableContent.ContentType + let expectedString: String? + switch kind { + case "image": (value, expectedType, expectedString) = (image, .image, nil) + case "attributedString": (value, expectedType, expectedString) = (text, .attributedString, nil) + case "int": (value, expectedType, expectedString) = (123_456, .string, "123456") + case "double": (value, expectedType, expectedString) = (Double(123.456), .string, "123.456") + case "float": (value, expectedType, expectedString) = (Float(123.456), .string, "123.456") + default: (value, expectedType, expectedString) = ("EXAMPLE", .string, "EXAMPLE") + } + + // Act + try content.setContent(content: value) + + // Assert + #expect(content.type == expectedType) + switch kind { + case "image": #expect(content.content as? Image == image) + case "attributedString": #expect(content.content as? NSAttributedString == text) + default: #expect(content.content as? String == expectedString) + } + } + + @Test func settingUnsupportedContentThrows() throws { + // Arrange + let optionalContent = try PDFTableContent(content: nil) + let content = try #require(optionalContent) + let value = ["INVALID"] + + // Act and Assert + #expect(throws: (any Error).self) { + try content.setContent(content: value) + } + } + + @Test(arguments: 0..<4) + func typePredicatesIdentifyOnlyMatchingContentType(index: Int) { + // Arrange + let types: [PDFTableContent.ContentType] = [.none, .string, .attributedString, .image] + let content = PDFTableContent(type: .none, content: nil) + content.type = types[index] + + // Act + let isString = content.isString + let isAttributedString = content.isAttributedString + let isImage = content.isImage + + // Assert + #expect(isString == (index == 1)) + #expect(isAttributedString == (index == 2)) + #expect(isImage == (index == 3)) + } + + @Test(arguments: 0..<4) + func stringValueIsExposedOnlyWhenTypeIsString(index: Int) { + // Arrange + let types: [PDFTableContent.ContentType] = [.none, .string, .attributedString, .image] + let content = PDFTableContent(type: .none, content: "EXAMPLE") + content.type = types[index] + + // Act + let value = content.stringValue + + // Assert + #expect(value == (index == 1 ? "EXAMPLE" : nil)) + } + + @Test(arguments: 0..<4) + func attributedStringValueIsExposedOnlyWhenTypeMatches(index: Int) { + // Arrange + let types: [PDFTableContent.ContentType] = [.none, .string, .attributedString, .image] + let text = NSAttributedString(string: "EXAMPLE") + let content = PDFTableContent(type: .none, content: text) + content.type = types[index] + + // Act + let value = content.attributedStringValue + + // Assert + #expect(value == (index == 2 ? text : nil)) + } + + @Test(arguments: 0..<4) + func imageValueIsExposedOnlyWhenTypeMatches(index: Int) { + // Arrange + let types: [PDFTableContent.ContentType] = [.none, .string, .attributedString, .image] + let image = Image() + let content = PDFTableContent(type: .none, content: image) + content.type = types[index] + + // Act + let value = content.imageValue + + // Assert + #expect(value == (index == 3 ? image : nil)) + } + + @Test func stringExtensionProducesStringContent() { + // Arrange + let text = "EXAMPLE" + + // Act + let content = text.asTableContent + + // Assert + #expect(content.type == .string) + #expect(content.content as? String == text) + } + + @Test func attributedStringExtensionProducesAttributedContent() { + // Arrange + let text = NSAttributedString(string: "EXAMPLE") + + // Act + let content = text.asTableContent + + // Assert + #expect(content.type == .attributedString) + #expect(content.content as? NSAttributedString == text) + } + + @Test func imageExtensionPreservesImageIdentity() { + // Arrange + let image = Image() + + // Act + let content = image.asTableContent + + // Assert + #expect(content.type == .image) + #expect(content.content as? Image === image) + } +} diff --git a/Tests/TPPDFTests/Table/PDFTableContent_Spec.swift b/Tests/TPPDFTests/Table/PDFTableContent_Spec.swift deleted file mode 100644 index ad15d48e..00000000 --- a/Tests/TPPDFTests/Table/PDFTableContent_Spec.swift +++ /dev/null @@ -1,281 +0,0 @@ -// -// PDFTableContent_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.16.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Foundation -import Nimble -import Quick -@testable import TPPDF -import XCTest - -class PDFTableContent_Spec: QuickSpec { - // swiftlint:disable closure_body_length function_body_length - override func spec() { - describe("PDFTableContent") { - context("enum ContentType") { - it("has a value none") { - expect(PDFTableContent.ContentType.none).toNot(beNil()) - } - - it("has a value none") { - expect(PDFTableContent.ContentType.string).toNot(beNil()) - } - - it("has a value none") { - expect(PDFTableContent.ContentType.attributedString).toNot(beNil()) - } - - it("has a value none") { - expect(PDFTableContent.ContentType.image).toNot(beNil()) - } - } - - context("variables") { - it("has a default type") { - expect((try? PDFTableContent(content: nil))?.type) == PDFTableContent.ContentType.none - } - - it("has an optional content") { - let content = try? PDFTableContent(content: nil) - expect(content).toNot(beNil()) - expect(content?.content).to(beNil()) - } - } - - context("initializer") { - it("can be initialized with any") { - expect { - try PDFTableContent(content: 123) - }.toNot(throwError()) - expect { - try PDFTableContent(content: ["EXAMPLE"]) - }.to(throwError()) - } - - it("can be initialized with type and optional content") { - let content = PDFTableContent(type: PDFTableContent.ContentType.attributedString, - content: NSAttributedString(string: "EXAMPLE")) - expect(content.type) == PDFTableContent.ContentType.attributedString - expect(content.content as? NSAttributedString) == NSAttributedString(string: "EXAMPLE") - } - } - - context("content") { - var content: PDFTableContent! - - beforeEach { - do { - content = try XCTUnwrap(PDFTableContent(content: nil)) - } catch { - XCTFail("Failed to create PDFTableContent") - } - } - - it("can set to nil") { - expect { - try content.setContent(content: nil) - }.toNot(throwError()) - expect(content.type).toEventually(equal(PDFTableContent.ContentType.none)) - expect(content.content).toEventually(beNil()) - } - - it("can set to string") { - let content = try XCTUnwrap(PDFTableContent(content: nil)) - - expect { - try content.setContent(content: "EXAMPLE") - }.toNot(throwError()) - expect(content.type).toEventually(equal(PDFTableContent.ContentType.string)) - expect(content.content as? String).toEventually(equal("EXAMPLE")) - } - - it("can set to image") { - let value = Image() - - expect { - try content.setContent(content: value) - }.toNot(throwError()) - expect(content.type).toEventually(equal(PDFTableContent.ContentType.image)) - expect(content.content as? Image).toEventually(equal(value)) - } - - it("can set to attributed string") { - let value = NSAttributedString(string: "EXAMPLE") - - expect { - try content.setContent(content: value) - }.toNot(throwError()) - expect(content.type).toEventually(equal(PDFTableContent.ContentType.attributedString)) - expect(content.content as? NSAttributedString).toEventually(equal(value)) - } - - it("can set to int") { - let value = 123_456 - - expect { - try content.setContent(content: value) - }.toNot(throwError()) - expect(content.type).toEventually(equal(PDFTableContent.ContentType.string)) - expect(content.content as? String).toEventually(equal("123456")) - } - - it("can set to double") { - let value = Double(123.456) - - expect { - try content.setContent(content: value) - }.toNot(throwError()) - expect(content.type).toEventually(equal(PDFTableContent.ContentType.string)) - expect(content.content as? String).toEventually(equal("123.456")) - } - - it("can set to float") { - let value = Float(123.456) - - expect { - try content.setContent(content: value) - }.toNot(throwError()) - expect(content.type).toEventually(equal(PDFTableContent.ContentType.string)) - expect(content.content as? String).toEventually(equal("123.456")) - } - - it("can not set to unsupported") { - let value = ["INVALID"] - - expect { - try content.setContent(content: value) - }.to(throwError()) - } - } - - context("computed variables") { - var content: PDFTableContent! - - beforeEach { - content = PDFTableContent(type: .none, content: nil) - } - - it("is a string") { - content.type = .string - expect(content.isString).to(beTrue()) - - content.type = .none - expect(content.isString).to(beFalse()) - - content.type = .image - expect(content.isString).to(beFalse()) - - content.type = .attributedString - expect(content.isString).to(beFalse()) - } - - it("is an attributed string") { - content.type = .attributedString - expect(content.isAttributedString).to(beTrue()) - - content.type = .none - expect(content.isAttributedString).to(beFalse()) - - content.type = .image - expect(content.isAttributedString).to(beFalse()) - - content.type = .string - expect(content.isAttributedString).to(beFalse()) - } - - it("is an image") { - content.type = .image - expect(content.isImage).to(beTrue()) - - content.type = .none - expect(content.isImage).to(beFalse()) - - content.type = .attributedString - expect(content.isImage).to(beFalse()) - - content.type = .string - expect(content.isImage).to(beFalse()) - } - - it("can get string value") { - content.content = "EXAMPLE" - content.type = .string - expect(content.stringValue) == "EXAMPLE" - - content.type = .none - expect(content.stringValue).to(beNil()) - - content.type = .attributedString - expect(content.stringValue).to(beNil()) - - content.type = .image - expect(content.stringValue).to(beNil()) - } - - it("can get attributed string value") { - content.content = NSAttributedString(string: "EXAMPLE") - content.type = .attributedString - expect(content.attributedStringValue) == NSAttributedString(string: "EXAMPLE") - - content.type = .none - expect(content.attributedStringValue).to(beNil()) - - content.type = .string - expect(content.attributedStringValue).to(beNil()) - - content.type = .image - expect(content.attributedStringValue).to(beNil()) - } - - it("can get image value") { - content.content = Image() - content.type = .image - expect(content.imageValue) == content.content as? Image - - content.type = .none - expect(content.imageValue).to(beNil()) - - content.type = .string - expect(content.imageValue).to(beNil()) - - content.type = .attributedString - expect(content.imageValue).to(beNil()) - } - } - - context("extensions") { - context("String") { - it("can be converted to content") { - let value = "EXAMPLE" - let content = value.asTableContent - expect(content.content as? String) == value - expect(content.type) == PDFTableContent.ContentType.string - } - } - - context("NSAttributedString") { - it("can be converted to content") { - let attributedString = NSAttributedString(string: "EXAMPLE") - let content = attributedString.asTableContent - expect(content.content as? NSAttributedString) == attributedString - expect(content.type) == PDFTableContent.ContentType.attributedString - } - } - - context("Image") { - it("can be converted to content") { - let image = Image() - let content = image.asTableContent - expect(content.content as? Image).to(be(image)) - expect(content.type) == PDFTableContent.ContentType.image - } - } - } - } - } - // swiftlint:enable closure_body_length function_body_length -} diff --git a/Tests/TPPDFTests/Table/PDFTableEquatableTests.swift b/Tests/TPPDFTests/Table/PDFTableEquatableTests.swift new file mode 100644 index 00000000..ded8d2fd --- /dev/null +++ b/Tests/TPPDFTests/Table/PDFTableEquatableTests.swift @@ -0,0 +1,38 @@ +// +// PDFTableEquatableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFTableEquatableTests { + @Test(arguments: ["same", "style", "cellCount", "cellContent", "widths", "padding", "margin", "headers"]) + func equalityDependsOnStyleCellsAndLayoutSettings(property: String) throws { + // Arrange + let table = PDFTable(rows: 0, columns: 0) + let otherTable = PDFTable(rows: 0, columns: 0) + switch property { + case "style": otherTable.style = PDFTableStyle(rowHeaderCount: 2) + case "cellCount": otherTable.cells = [[PDFTableCell(), PDFTableCell()]] + case "cellContent": + let content = try PDFTableContent(content: "TEST") + table.cells = [[PDFTableCell(content: try #require(content)), PDFTableCell()]] + otherTable.cells = [[PDFTableCell(), PDFTableCell()]] + case "widths": otherTable.widths = [0.25, 0.75] + case "padding": otherTable.padding = 100 + case "margin": otherTable.margin = 100 + case "headers": otherTable.showHeadersOnEveryPage = !table.showHeadersOnEveryPage + default: break + } + + // Act + let isEqual = table == otherTable + + // Assert + #expect(isEqual == (property == "same")) + } +} diff --git a/Tests/TPPDFTests/Table/PDFTableTests.swift b/Tests/TPPDFTests/Table/PDFTableTests.swift new file mode 100644 index 00000000..f02931c2 --- /dev/null +++ b/Tests/TPPDFTests/Table/PDFTableTests.swift @@ -0,0 +1,41 @@ +// +// PDFTableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.16.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFTableTests { + @Test func initializingEmptyTableUsesDefaultStyleAndLayoutSettings() { + // Arrange + let expectedStyle = PDFTableStyleDefaults.simple + + // Act + let table = PDFTable(rows: 0, columns: 0) + + // Assert + #expect(table.style == expectedStyle) + #expect(table.cells.isEmpty) + #expect(table.widths.isEmpty) + #expect(table.padding == 0) + #expect(table.margin == 0) + #expect(!table.showHeadersOnEveryPage) + } + + @Test func settingStyleThroughCellSubscriptUpdatesStoredCell() { + // Arrange + let table = PDFTable(rows: 0, columns: 0) + table.cells = [[PDFTableCell()]] + let style = PDFTableCellStyle(colors: (fill: .green, text: .orange)) + + // Act + table[0, 0].style = style + + // Assert + #expect(table.cells[0][0].style == style) + } +} diff --git a/Tests/TPPDFTests/Table/PDFTableValidatorTests.swift b/Tests/TPPDFTests/Table/PDFTableValidatorTests.swift new file mode 100644 index 00000000..2face859 --- /dev/null +++ b/Tests/TPPDFTests/Table/PDFTableValidatorTests.swift @@ -0,0 +1,76 @@ +// +// PDFTableValidatorTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.16.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFTableValidatorTests { + @Test(arguments: [false, true]) + func validatingTableWithEmptyOrConfiguredCellsSucceeds(configured: Bool) { + // Arrange + let table = PDFTable(rows: 2, columns: 4) + if configured { + table.widths = [0.1, 0.2, 0.3, 0.4] + table.content = [["1|1", "1|2", "1|3", "1|4"], ["2|1", "2|2", "2|3", "2|4"]] + table.alignment = [[.left, .top, .right, .bottom], [.topLeft, .topRight, .bottomRight, .bottomLeft]] + } + + // Act and Assert + #expect(throws: Never.self) { + try PDFTableValidator.validateTable(table: table) + } + } + + @Test(arguments: ["empty", "alignmentCount", "alignmentRowSize", "columnWidths"]) + func invalidTableDataThrowsExpectedValidationError(scenario: String) throws { + // Arrange + let data = [["1|1", "1|2", "1|3", "1|4"], ["2|1", "2|2", "2|3", "2|4"]] + let input = scenario == "empty" ? [] : data + let alignments: [[PDFTableCellAlignment]]? = scenario == "alignmentCount" ? [] + : scenario == "alignmentRowSize" ? [[], []] : nil + let widths: [CGFloat]? = scenario == "columnWidths" ? [] : nil + let expectedMessage = scenario == "alignmentCount" ? "Data and alignment must be equal size!" + : scenario == "columnWidths" ? "Data and widths for row with index 0 does not have the same amount!" + : "Data and alignment for row with index 0 does not have the same amount!" + + // Act + let caughtError = #expect(throws: PDFError.self) { + try PDFTableValidator.validateTableData(data: input, alignments: alignments, columnWidths: widths) + } + + // Assert + let error = try #require(caughtError) + switch error { + case .tableIsEmpty: + #expect(scenario == "empty") + case let .tableStructureInvalid(message): + #expect(scenario != "empty") + #expect(message == expectedMessage) + default: + Issue.record("Unexpected validation error: \(error)") + } + } + + #if os(macOS) && swift(>=6.2) + @Test func assigningContentWithWrongColumnCountTerminatesProcess() async { + // Arrange + let expectedExit = ExitTest.Condition.failure + + // Act + let result = await #expect(processExitsWith: expectedExit) { + let table = PDFTable(rows: 2, columns: 4) + table.widths = [0.1, 0.2, 0.3, 0.4] + table.content = [["1", "2"]] + } + + // Assert + #expect(result != nil) + } + #endif +} diff --git a/Tests/TPPDFTests/Table/PDFTableValidator_Spec.swift b/Tests/TPPDFTests/Table/PDFTableValidator_Spec.swift deleted file mode 100644 index 8461bbb5..00000000 --- a/Tests/TPPDFTests/Table/PDFTableValidator_Spec.swift +++ /dev/null @@ -1,120 +0,0 @@ -// -// PDFTableValidator_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.16.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Nimble -import Quick -@testable import TPPDF - -class PDFTableValidator_Spec: QuickSpec { - // swiftlint:disable closure_body_length function_body_length - override func spec() { - describe("PDFTableValidator") { - let data = [ - [ - "1|1", - "1|2", - "1|3", - "1|4", - ], - [ - "2|1", - "2|2", - "2|3", - "2|4", - ], - ] - let alignments = [ - [ - PDFTableCellAlignment.left, - PDFTableCellAlignment.top, - PDFTableCellAlignment.right, - PDFTableCellAlignment.bottom, - ], - [ - PDFTableCellAlignment.topLeft, - PDFTableCellAlignment.topRight, - PDFTableCellAlignment.bottomRight, - PDFTableCellAlignment.bottomLeft, - ], - ] - let widths: [CGFloat] = [ - 0.1, - 0.2, - 0.3, - 0.4, - ] - - var table: PDFTable! - - beforeEach { - table = PDFTable(rows: 2, columns: 4) - } - - context("table validation") { - it("should succeed when no cells in table") { - expect { - try PDFTableValidator.validateTable(table: table) - }.toNot(throwError()) - } - - it("fails when not same amount of data than widths") { - #if arch(x86_64) - table.widths = widths - expect { - table.content = [["1", "2"]] - }.to(throwAssertion()) - #endif - } - - it("should not fail with valid data") { - table.widths = widths - table.content = data - table.alignment = alignments - - expect { - try PDFTableValidator.validateTable(table: table) - }.toNot(throwError()) - } - } - - context("data validation") { - it("fails when no data given") { - expect { - try PDFTableValidator.validateTableData(data: []) - }.to(throwError(PDFError.tableIsEmpty)) - } - - it("fails when alignments not given and count not same") { - expect { - try PDFTableValidator.validateTableData(data: data, alignments: []) - }.to(throwError( - PDFError.tableStructureInvalid(message: "Data and alignment must be equal size!") - )) - } - - it("fails when data row and alignment row is not the same") { - expect { - try PDFTableValidator.validateTableData(data: data, alignments: [[], []]) - }.to(throwError( - PDFError.tableStructureInvalid(message: "Data and alignment for row with index 0 does not have the same amount!") - )) - } - - it("fails when data row and column widths count is not the same") { - expect { - try PDFTableValidator.validateTableData(data: data, alignments: nil, columnWidths: []) - }.to(throwError( - PDFError.tableStructureInvalid(message: "Data and alignment for row with index 0 does not have the same amount!") - )) - } - } - } - } - // swiftlint:enable closure_body_length function_body_length -} diff --git a/Tests/TPPDFTests/Table/PDFTable_Spec.swift b/Tests/TPPDFTests/Table/PDFTable_Spec.swift deleted file mode 100644 index dc5e1539..00000000 --- a/Tests/TPPDFTests/Table/PDFTable_Spec.swift +++ /dev/null @@ -1,57 +0,0 @@ -// -// PDFTable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.16.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFTable_Spec: QuickSpec { - override func spec() { - describe("PDFTable") { - context("variables") { - let table = PDFTable(rows: 0, columns: 0) - - it("has a default style") { - expect(table.style) == PDFTableStyleDefaults.simple - } - - it("has no default cells") { - expect(table.cells.isEmpty).to(beTrue()) - } - - it("hsa no default widths") { - expect(table.widths) == [] - } - - it("has a default padding") { - expect(table.padding) == 0 - } - - it("has a default margin") { - expect(table.margin) == 0 - } - - it("has a default showHeadersOnEveryPage") { - expect(table.showHeadersOnEveryPage).to(beFalse()) - } - } - - context("cell style") { - it("can set style of cell") { - let table = PDFTable(rows: 0, columns: 0) - table.cells = [[PDFTableCell()]] - - let style = PDFTableCellStyle(colors: (fill: Color.green, text: Color.orange)) - - table[0, 0].style = style - expect(table.cells[0][0].style).toEventually(equal(style)) - } - } - } - } -} diff --git a/Tests/TPPDFTests/Text/PDFAttributedText+Equatable_Spec.swift b/Tests/TPPDFTests/Text/PDFAttributedText+Equatable_Spec.swift deleted file mode 100644 index 1139e4cb..00000000 --- a/Tests/TPPDFTests/Text/PDFAttributedText+Equatable_Spec.swift +++ /dev/null @@ -1,32 +0,0 @@ -// -// PDFAttributedText+Equatable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Foundation -import Nimble -import Quick -@testable import TPPDF - -class PDFAttributedText_Equatable_Spec: QuickSpec { - override func spec() { - describe("PDFAttributedText") { - context("Equatable") { - let text = PDFAttributedText(text: NSAttributedString(string: "EXAMPLE")) - - it("is equal") { - let text2 = PDFAttributedText(text: NSAttributedString(string: "EXAMPLE")) - expect(text) == text2 - } - - it("is not equal with different text") { - let text2 = PDFAttributedText(text: NSAttributedString(string: "INVALID")) - expect(text) != text2 - } - } - } - } -} diff --git a/Tests/TPPDFTests/Text/PDFAttributedTextEquatableTests.swift b/Tests/TPPDFTests/Text/PDFAttributedTextEquatableTests.swift new file mode 100644 index 00000000..cf781436 --- /dev/null +++ b/Tests/TPPDFTests/Text/PDFAttributedTextEquatableTests.swift @@ -0,0 +1,26 @@ +// +// PDFAttributedTextEquatableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Foundation +import Testing +@testable import TPPDF + +struct PDFAttributedTextEquatableTests { + @Test(arguments: [("EXAMPLE", true), ("INVALID", false)]) + func equalityDependsOnAttributedContent(content: String, expectedEquality: Bool) { + // Arrange + let text = PDFAttributedText(text: NSAttributedString(string: "EXAMPLE")) + let otherText = PDFAttributedText(text: NSAttributedString(string: content)) + + // Act + let isEqual = text == otherText + + // Assert + #expect(isEqual == expectedEquality) + } +} diff --git a/Tests/TPPDFTests/Text/PDFAttributedTextObjectTests.swift b/Tests/TPPDFTests/Text/PDFAttributedTextObjectTests.swift new file mode 100644 index 00000000..f52b2cd9 --- /dev/null +++ b/Tests/TPPDFTests/Text/PDFAttributedTextObjectTests.swift @@ -0,0 +1,54 @@ +// +// PDFAttributedTextObjectTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.05.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Foundation +import Testing +@testable import TPPDF + +struct PDFAttributedTextObjectTests { + @Test func initializingWithSimpleTextStoresOnlySimpleText() { + // Arrange + let text = PDFSimpleText(text: "example") + + // Act + let object = PDFAttributedTextObject(text: text) + + // Assert + #expect(object.simpleText != nil) + #expect(object.attributedText == nil) + } + + @Test func initializingWithAttributedTextStoresOnlyAttributedText() { + // Arrange + let text = PDFAttributedText(text: NSAttributedString(string: "example")) + + // Act + let object = PDFAttributedTextObject(text: text) + + // Assert + #expect(object.simpleText == nil) + #expect(object.attributedText != nil) + } + + #if os(macOS) && swift(>=6.2) + @Test func initializingWithUnsupportedTextSubclassTerminatesProcess() async { + // Arrange + let expectedExit = ExitTest.Condition.failure + + // Act + let result = await #expect(processExitsWith: expectedExit) { + class CustomText: PDFText {} + let text = CustomText() + _ = PDFAttributedTextObject(text: text) + } + + // Assert + #expect(result != nil) + } + #endif +} diff --git a/Tests/TPPDFTests/Text/PDFAttributedTextObject_Spec.swift b/Tests/TPPDFTests/Text/PDFAttributedTextObject_Spec.swift deleted file mode 100644 index 2c3a3ab4..00000000 --- a/Tests/TPPDFTests/Text/PDFAttributedTextObject_Spec.swift +++ /dev/null @@ -1,50 +0,0 @@ -// -// PDFAttributedTextObject_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.05.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Foundation -import Nimble -import Quick -@testable import TPPDF - -class PDFAttributedTextObject_Spec: QuickSpec { - override func spec() { - describe("PDFAttributedTextObject") { - describe("initializer") { - let simpleText = "example" - - it("can be initialized with a simple object") { - let simpleTextObject = PDFSimpleText(text: simpleText) - - let textObject = PDFAttributedTextObject(text: simpleTextObject) - expect(textObject.simpleText).toNot(beNil()) - expect(textObject.attributedText).to(beNil()) - } - - it("can be initialized with a simple object") { - let attributedText = NSAttributedString(string: simpleText) - let attributedTextObject = PDFAttributedText(text: attributedText) - - let textObject = PDFAttributedTextObject(text: attributedTextObject) - expect(textObject.simpleText).to(beNil()) - expect(textObject.attributedText).toNot(beNil()) - } - - it("can not be initialized with a custom object") { - #if arch(x86_64) - class CustomTextObject: PDFText {} - - let custom = CustomTextObject() - expect { - _ = PDFAttributedTextObject(text: custom) - }.to(throwAssertion()) - #endif - } - } - } - } -} diff --git a/Tests/TPPDFTests/Text/PDFAttributedTextTests.swift b/Tests/TPPDFTests/Text/PDFAttributedTextTests.swift new file mode 100644 index 00000000..df6877a3 --- /dev/null +++ b/Tests/TPPDFTests/Text/PDFAttributedTextTests.swift @@ -0,0 +1,24 @@ +// +// PDFAttributedTextTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.05.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Foundation +import Testing +@testable import TPPDF + +struct PDFAttributedTextTests { + @Test func initializingWithAttributedStringPreservesText() { + // Arrange + let text = NSAttributedString(string: "example") + + // Act + let object = PDFAttributedText(text: text) + + // Assert + #expect(object.text == text) + } +} diff --git a/Tests/TPPDFTests/Text/PDFAttributedText_Spec.swift b/Tests/TPPDFTests/Text/PDFAttributedText_Spec.swift deleted file mode 100644 index 323f0f5c..00000000 --- a/Tests/TPPDFTests/Text/PDFAttributedText_Spec.swift +++ /dev/null @@ -1,25 +0,0 @@ -// -// PDFAttributedText_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.05.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Foundation -import Nimble -import Quick -@testable import TPPDF - -class PDFAttributedText_Spec: QuickSpec { - override func spec() { - describe("PDFAttributedText") { - it("can be initialized with an attributed string") { - let attributed = NSAttributedString(string: "example") - let textObject = PDFAttributedText(text: attributed) - - expect(textObject.text) == attributed - } - } - } -} diff --git a/Tests/TPPDFTests/Text/PDFFontObjectTests.swift b/Tests/TPPDFTests/Text/PDFFontObjectTests.swift new file mode 100644 index 00000000..8d6db9cc --- /dev/null +++ b/Tests/TPPDFTests/Text/PDFFontObjectTests.swift @@ -0,0 +1,42 @@ +// +// PDFFontObjectTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 06.12.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFFontObjectTests { + @Test func initializingWithFontPreservesFont() { + // Arrange + let font = Font.boldSystemFont(ofSize: 100) + + // Act + let object = PDFFontObject(font: font) + + // Assert + #expect(object.font == font) + } + + @Test func calculatingSetsContainerFontAndReturnsUnpositionedObject() throws { + // Arrange + let font = Font.boldSystemFont(ofSize: 100) + let object = PDFFontObject(font: font) + let generator = PDFGenerator(document: PDFDocument(format: .a4)) + let container = PDFContainer.contentLeft + + // Act + let result = try object.calculate(generator: generator, container: container) + + // Assert + #expect(generator.fonts[container] == font) + #expect(object.frame == CGRect.null) + #expect(result.count == 1) + #expect(result.first?.0 == container) + #expect(result.first?.1 as? PDFFontObject === object) + } +} diff --git a/Tests/TPPDFTests/Text/PDFFontObject_Spec.swift b/Tests/TPPDFTests/Text/PDFFontObject_Spec.swift deleted file mode 100644 index 39a6d54e..00000000 --- a/Tests/TPPDFTests/Text/PDFFontObject_Spec.swift +++ /dev/null @@ -1,57 +0,0 @@ -// -// PDFFontObject_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 06.12.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Nimble -import Quick -@testable import TPPDF - -class PDFFontObject_Spec: QuickSpec { - override func spec() { - describe("PDFFontObject") { - let font = Font.boldSystemFont(ofSize: 100) - var object: PDFFontObject! - - beforeEach { - object = PDFFontObject(font: font) - } - - context("variables") { - it("has a font instance") { - expect(object.font) == font - } - } - - describe("calculation") { - let document = PDFDocument(format: .a4) - var generator: PDFGenerator! - - let container = PDFContainer.contentLeft - var result: [PDFLocatedRenderObject]! - - beforeEach { - generator = PDFGenerator(document: document) - result = [] - } - - it("can set text color") { - expect { - result = try object.calculate(generator: generator, container: container) - }.toNot(throwError()) - expect(generator.fonts[container]).toEventually(equal(font)) - - expect(object.frame) == CGRect.null - - expect(result).toEventually(haveCount(1)) - expect(result.first?.0).toEventually(equal(container)) - expect(result.first?.1 as? PDFFontObject).toEventually(be(object)) - } - } - } - } -} diff --git a/Tests/TPPDFTests/Text/PDFSimpleText+Equatable_Spec.swift b/Tests/TPPDFTests/Text/PDFSimpleText+Equatable_Spec.swift deleted file mode 100644 index a51188a1..00000000 --- a/Tests/TPPDFTests/Text/PDFSimpleText+Equatable_Spec.swift +++ /dev/null @@ -1,36 +0,0 @@ -// -// PDFSimpleText+Equatable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFSimpleText_Equatable_Spec: QuickSpec { - override func spec() { - describe("PDFSimpleText") { - context("Equatable") { - let text = PDFSimpleText(text: "EXAMPLE") - - it("is equal") { - let otherText = PDFSimpleText(text: "EXAMPLE") - expect(text) == otherText - } - - it("is not equal with different text") { - let otherText = PDFSimpleText(text: "DIFFERENT") - expect(text) != otherText - } - - it("is not equal with different spacing") { - let otherText = PDFSimpleText(text: "EXAMPLE", spacing: 20) - expect(text) != otherText - } - } - } - } -} diff --git a/Tests/TPPDFTests/Text/PDFSimpleTextEquatableTests.swift b/Tests/TPPDFTests/Text/PDFSimpleTextEquatableTests.swift new file mode 100644 index 00000000..86484f41 --- /dev/null +++ b/Tests/TPPDFTests/Text/PDFSimpleTextEquatableTests.swift @@ -0,0 +1,26 @@ +// +// PDFSimpleTextEquatableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFSimpleTextEquatableTests { + @Test(arguments: [("EXAMPLE", 0.0, true), ("DIFFERENT", 0.0, false), ("EXAMPLE", 20.0, false)]) + func equalityDependsOnContentAndSpacing(content: String, spacing: Double, expectedEquality: Bool) { + // Arrange + let text = PDFSimpleText(text: "EXAMPLE") + let otherText = PDFSimpleText(text: content, spacing: CGFloat(spacing)) + + // Act + let isEqual = text == otherText + + // Assert + #expect(isEqual == expectedEquality) + } +} diff --git a/Tests/TPPDFTests/Text/PDFSimpleTextTests.swift b/Tests/TPPDFTests/Text/PDFSimpleTextTests.swift new file mode 100644 index 00000000..9c10dfde --- /dev/null +++ b/Tests/TPPDFTests/Text/PDFSimpleTextTests.swift @@ -0,0 +1,38 @@ +// +// PDFSimpleTextTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 11.14.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFSimpleTextTests { + @Test func initializingWithSpacingPreservesTextAndSpacing() { + // Arrange + let text = "EXAMPLE" + let spacing: CGFloat = 10 + + // Act + let object = PDFSimpleText(text: text, spacing: spacing) + + // Assert + #expect(object.text == text) + #expect(object.spacing == spacing) + } + + @Test func initializingWithoutSpacingUsesZeroSpacing() { + // Arrange + let text = "STRING" + + // Act + let object = PDFSimpleText(text: text) + + // Assert + #expect(object.text == text) + #expect(object.spacing == 0) + } +} diff --git a/Tests/TPPDFTests/Text/PDFSimpleText_Spec.swift b/Tests/TPPDFTests/Text/PDFSimpleText_Spec.swift deleted file mode 100644 index b64de335..00000000 --- a/Tests/TPPDFTests/Text/PDFSimpleText_Spec.swift +++ /dev/null @@ -1,45 +0,0 @@ -// -// PDFSimpleText_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 11.14.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Nimble -import Quick -@testable import TPPDF - -class PDFSimpleText_Spec: QuickSpec { - override func spec() { - describe("PDFSimpleText") { - let simpleText = "EXAMPLE" - let spacing: CGFloat = 10.0 - var textObject: PDFSimpleText! - - beforeEach { - textObject = PDFSimpleText(text: simpleText, spacing: spacing) - } - - context("variables") { - it("has a text instance") { - expect(textObject.text) == simpleText - } - - it("has a spacing instance with default value") { - expect(textObject.spacing) == 10.0 - } - } - - it("can be initialized without spacing") { - let text = "STRING" - - textObject = PDFSimpleText(text: text) - - expect(textObject.text) == text - expect(textObject.spacing) == 0 - } - } - } -} diff --git a/Tests/TPPDFTests/Text/PDFTextColorObjectTests.swift b/Tests/TPPDFTests/Text/PDFTextColorObjectTests.swift new file mode 100644 index 00000000..34ac8fdc --- /dev/null +++ b/Tests/TPPDFTests/Text/PDFTextColorObjectTests.swift @@ -0,0 +1,42 @@ +// +// PDFTextColorObjectTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 05.12.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFTextColorObjectTests { + @Test func initializingWithColorPreservesColor() { + // Arrange + let color = Color.red + + // Act + let object = PDFTextColorObject(color: color) + + // Assert + #expect(object.color == color) + } + + @Test func calculatingSetsContainerColorAndReturnsUnpositionedObject() throws { + // Arrange + let color = Color.red + let object = PDFTextColorObject(color: color) + let generator = PDFGenerator(document: PDFDocument(format: .a4)) + let container = PDFContainer.contentLeft + + // Act + let result = try object.calculate(generator: generator, container: container) + + // Assert + #expect(generator.textColor[container] == color) + #expect(object.frame == CGRect.null) + #expect(result.count == 1) + #expect(result.first?.0 == container) + #expect(result.first?.1 as? PDFTextColorObject === object) + } +} diff --git a/Tests/TPPDFTests/Text/PDFTextColorObject_Spec.swift b/Tests/TPPDFTests/Text/PDFTextColorObject_Spec.swift deleted file mode 100644 index 1c155284..00000000 --- a/Tests/TPPDFTests/Text/PDFTextColorObject_Spec.swift +++ /dev/null @@ -1,57 +0,0 @@ -// -// PDFTextColorObject_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 05.12.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Nimble -import Quick -@testable import TPPDF - -class PDFTextColorObject_Spec: QuickSpec { - override func spec() { - describe("PDFTextColorObject") { - let color = Color.red - var object: PDFTextColorObject! - - beforeEach { - object = PDFTextColorObject(color: color) - } - - context("variables") { - it("has a color instance") { - expect(object.color) == color - } - } - - describe("calculation") { - let document = PDFDocument(format: .a4) - var generator: PDFGenerator! - - let container = PDFContainer.contentLeft - var result: [PDFLocatedRenderObject]! - - beforeEach { - generator = PDFGenerator(document: document) - result = [] - } - - it("can set text color") { - expect { - result = try object.calculate(generator: generator, container: container) - }.toNot(throwError()) - expect(generator.textColor[container]).toEventually(equal(color)) - - expect(object.frame) == CGRect.null - - expect(result).toEventually(haveCount(1)) - expect(result.first?.0).toEventually(equal(container)) - expect(result.first?.1 as? PDFTextColorObject).toEventually(be(object)) - } - } - } - } -} diff --git a/Tests/TPPDFTests/Text/PDFTextTests.swift b/Tests/TPPDFTests/Text/PDFTextTests.swift new file mode 100644 index 00000000..5f7808a7 --- /dev/null +++ b/Tests/TPPDFTests/Text/PDFTextTests.swift @@ -0,0 +1,44 @@ +// +// PDFTextTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 05.12.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Foundation +import Testing +@testable import TPPDF + +struct PDFTextTests { + @Test(arguments: [false, true]) + func matchingTextOfSameSubclassIsEqual(attributed: Bool) { + // Arrange + let text: PDFText = attributed + ? PDFAttributedText(text: NSAttributedString(string: "EXAMPLE")) + : PDFSimpleText(text: "EXAMPLE") + let otherText: PDFText = attributed + ? PDFAttributedText(text: NSAttributedString(string: "EXAMPLE")) + : PDFSimpleText(text: "EXAMPLE") + + // Act + let isEqual = text == otherText + let isUnequal = text != otherText + + // Assert + #expect(isEqual) + #expect(!isUnequal) + } + + @Test func matchingContentInDifferentTextSubclassesIsUnequal() { + // Arrange + let text: PDFText = PDFSimpleText(text: "EXAMPLE") + let otherText: PDFText = PDFAttributedText(text: NSAttributedString(string: "EXAMPLE")) + + // Act + let isEqual = text == otherText + + // Assert + #expect(!isEqual) + } +} diff --git a/Tests/TPPDFTests/Text/PDFText_Spec.swift b/Tests/TPPDFTests/Text/PDFText_Spec.swift deleted file mode 100644 index 1dc0883c..00000000 --- a/Tests/TPPDFTests/Text/PDFText_Spec.swift +++ /dev/null @@ -1,43 +0,0 @@ -// -// PDFText_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 05.12.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Foundation -import Nimble -import Quick -@testable import TPPDF - -class PDFText_Spec: QuickSpec { - override func spec() { - describe("PDFSimpleText") { - context("Equatable") { - it("is equal if both simple text") { - let text: PDFText = PDFSimpleText(text: "EXAMPLE") - let otherText: PDFText = PDFSimpleText(text: "EXAMPLE") - - expect(text == otherText).to(beTrue()) - expect(text != otherText).to(beFalse()) - } - - it("is equal if both attributed text") { - let text: PDFText = PDFAttributedText(text: NSAttributedString(string: "EXAMPLE")) - let otherText: PDFText = PDFAttributedText(text: NSAttributedString(string: "EXAMPLE")) - - expect(text == otherText).to(beTrue()) - expect(text != otherText).to(beFalse()) - } - - it("is not equal with different text subclasses") { - let text: PDFText = PDFSimpleText(text: "EXAMPLE") - let otherText: PDFText = PDFAttributedText(text: NSAttributedString(string: "EXAMPLE")) - - expect(text == otherText).to(beFalse()) - } - } - } - } -} diff --git a/Tests/TPPDFTests/resources/table-content-fixture.png b/Tests/TPPDFTests/resources/table-content-fixture.png new file mode 100644 index 00000000..11b671c9 Binary files /dev/null and b/Tests/TPPDFTests/resources/table-content-fixture.png differ