From 7a2577217977037d0ea510b0b96ddebee21641db Mon Sep 17 00:00:00 2001 From: Philip Niedertscheider Date: Sat, 10 Oct 2026 23:25:12 +0200 Subject: [PATCH] test(list): Migrate specs to Swift Testing --- .../List/PDFList+Equatable_Spec.swift | 48 --- .../List/PDFListEquatableTests.swift | 57 +++ .../List/PDFListItemEquatableTests.swift | 42 ++ .../List/PDFListItemObject_Spec.swift | 372 ------------------ .../List/PDFListItemSymbolTests.swift | 69 ++++ .../List/PDFListItemSymbol_Spec.swift | 67 ---- Tests/TPPDFTests/List/PDFListItemTests.swift | 81 ++++ .../List/PDFListItem_Equatable_Spec.swift | 54 --- Tests/TPPDFTests/List/PDFListItem_Spec.swift | 92 ----- .../TPPDFTests/List/PDFListObjectTests.swift | 117 ++++++ Tests/TPPDFTests/List/PDFListTests.swift | 86 ++++ Tests/TPPDFTests/List/PDFList_Spec.swift | 121 ------ 12 files changed, 452 insertions(+), 754 deletions(-) delete mode 100644 Tests/TPPDFTests/List/PDFList+Equatable_Spec.swift create mode 100644 Tests/TPPDFTests/List/PDFListEquatableTests.swift create mode 100644 Tests/TPPDFTests/List/PDFListItemEquatableTests.swift delete mode 100644 Tests/TPPDFTests/List/PDFListItemObject_Spec.swift create mode 100644 Tests/TPPDFTests/List/PDFListItemSymbolTests.swift delete mode 100644 Tests/TPPDFTests/List/PDFListItemSymbol_Spec.swift create mode 100644 Tests/TPPDFTests/List/PDFListItemTests.swift delete mode 100644 Tests/TPPDFTests/List/PDFListItem_Equatable_Spec.swift delete mode 100644 Tests/TPPDFTests/List/PDFListItem_Spec.swift create mode 100644 Tests/TPPDFTests/List/PDFListObjectTests.swift create mode 100644 Tests/TPPDFTests/List/PDFListTests.swift delete mode 100644 Tests/TPPDFTests/List/PDFList_Spec.swift diff --git a/Tests/TPPDFTests/List/PDFList+Equatable_Spec.swift b/Tests/TPPDFTests/List/PDFList+Equatable_Spec.swift deleted file mode 100644 index 727cae73..00000000 --- a/Tests/TPPDFTests/List/PDFList+Equatable_Spec.swift +++ /dev/null @@ -1,48 +0,0 @@ -// -// PDFList+Equatable_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 05.12.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFList_Equatable_Spec: QuickSpec { - override func spec() { - describe("PDFList") { - context("Equatable") { - var object: PDFList! - - beforeEach { - object = PDFList(indentations: [(pre: 5, past: 10)]) - } - - it("is equal") { - let otherObject = PDFList(indentations: [(pre: 5, past: 10)]) - expect(object) == otherObject - } - - it("is not equal with different indentations") { - let otherObject = PDFList(indentations: [(pre: 10.0, past: 20.0)]) - expect(object) != otherObject - - otherObject.levelIndentations.append((pre: 10.0, past: 10.0)) - expect(object) != otherObject - } - - it("is not equal with different items") { - let otherObject = PDFList(indentations: [(pre: 5, past: 10)]) - - otherObject.addItem(PDFListItem(symbol: .dash, content: "CONTENT")) - expect(object) != otherObject - - object.addItem(PDFListItem(symbol: .dash, content: "ANOTHERONE")) - expect(object) != otherObject - } - } - } - } -} diff --git a/Tests/TPPDFTests/List/PDFListEquatableTests.swift b/Tests/TPPDFTests/List/PDFListEquatableTests.swift new file mode 100644 index 00000000..8a20cc76 --- /dev/null +++ b/Tests/TPPDFTests/List/PDFListEquatableTests.swift @@ -0,0 +1,57 @@ +// +// PDFListEquatableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 05.12.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFListEquatableTests { + @Test func listsWithSameIndentationsAndItemsAreEqual() { + // Arrange + let list = PDFList(indentations: [(pre: 5, past: 10)]) + let otherList = PDFList(indentations: [(pre: 5, past: 10)]) + + // Act + let isEqual = list == otherList + + // Assert + #expect(isEqual) + } + + @Test(arguments: [false, true]) + func differentIndentationsMakeListsUnequal(addLevel: Bool) { + // Arrange + let list = PDFList(indentations: [(pre: 5, past: 10)]) + let otherList = PDFList(indentations: [(pre: 10, past: 20)]) + if addLevel { + otherList.levelIndentations.append((pre: 10, past: 10)) + } + + // Act + let isEqual = list == otherList + + // Assert + #expect(!isEqual) + } + + @Test(arguments: [false, true]) + func differentItemCountsOrContentMakeListsUnequal(sameItemCount: Bool) { + // Arrange + let list = PDFList(indentations: [(pre: 5, past: 10)]) + let otherList = PDFList(indentations: [(pre: 5, past: 10)]) + otherList.addItem(PDFListItem(symbol: .dash, content: "CONTENT")) + if sameItemCount { + list.addItem(PDFListItem(symbol: .dash, content: "ANOTHERONE")) + } + + // Act + let isEqual = list == otherList + + // Assert + #expect(!isEqual) + } +} diff --git a/Tests/TPPDFTests/List/PDFListItemEquatableTests.swift b/Tests/TPPDFTests/List/PDFListItemEquatableTests.swift new file mode 100644 index 00000000..4449ca47 --- /dev/null +++ b/Tests/TPPDFTests/List/PDFListItemEquatableTests.swift @@ -0,0 +1,42 @@ +// +// PDFListItemEquatableTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 05.12.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFListItemEquatableTests { + @Test func defaultItemsAreEqual() { + // Arrange + let item = PDFListItem() + let otherItem = PDFListItem() + + // Act + let isEqual = item == otherItem + + // Assert + #expect(isEqual) + } + + @Test(arguments: ["content", "children", "symbol"]) + func changingItemValuesMakesItemsUnequal(property: String) { + // Arrange + let item = PDFListItem() + let otherItem = PDFListItem() + switch property { + case "content": otherItem.content = "RANDOM" + case "children": otherItem.children = [PDFListItem()] + default: otherItem.symbol = .custom(value: "RANDOM") + } + + // Act + let isEqual = item == otherItem + + // Assert + #expect(!isEqual) + } +} diff --git a/Tests/TPPDFTests/List/PDFListItemObject_Spec.swift b/Tests/TPPDFTests/List/PDFListItemObject_Spec.swift deleted file mode 100644 index d9e3f640..00000000 --- a/Tests/TPPDFTests/List/PDFListItemObject_Spec.swift +++ /dev/null @@ -1,372 +0,0 @@ -// -// PDFListItemObject_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 06.12.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import CoreGraphics -import Foundation -import Nimble -import Quick -@testable import TPPDF - -class PDFListItemObject_Spec: QuickSpec { // swiftlint:disable:this type_body_length - // swiftlint:disable closure_body_length function_body_length - override func spec() { - describe("PDFListObject") { - let list = PDFList(indentations: [(pre: 10, past: 10), (pre: 20, past: 10)]) - list.addItem(PDFListItem(symbol: .numbered(value: "1"), content: "Heading 1")) - .addItem(PDFListItem(symbol: .numbered(value: nil), content: "Heading 2")) - .addItem(PDFListItem(symbol: .dot, content: "Heading 3") - .addItem(PDFListItem(symbol: .dash, content: "Subheading 1")) - .addItem(PDFListItem(symbol: .none, content: "Subheading 2")) - .addItem(PDFListItem(symbol: .inherit, content: "Subheading 3"))) - .addItem(PDFListItem(symbol: .custom(value: "+"), content: "Heading 4")) - - let object = PDFListObject(list: list) - - context("variables") { - it("has a list") { - expect(object.list) == list - } - } - - context("calculations") { - let document = PDFDocument(format: .a4) - let container = PDFContainer.contentLeft - var generator: PDFGenerator! - - beforeEach { - generator = PDFGenerator(document: document) - } - - it("can be calculated") { - var result: [PDFLocatedRenderObject]? - - expect { - result = try? object.calculate(generator: generator, container: container) - }.toNot(throwError()) - - expect(result).toNot(beNil()) - expect(result?.count).to(equal(13)) - - expect(result?[0].0).to(equal(PDFContainer.contentLeft)) - - var item: PDFAttributedTextObject? = result?[0].1 as? PDFAttributedTextObject - expect(item?.frame.origin.x).to(equal(document.layout.margin.left + 10)) - expect(item?.frame.origin.y).to(equal(60)) - expect(item?.frame.size.width).to(beCloseTo(12, within: 1)) - #if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.size.height).to(equal(21.0)) - #else - expect(item?.frame.size.height).to(equal(20.0)) - #endif - expect(item?.simpleText).to(equal(PDFSimpleText(text: "1."))) - - expect(result?[1].0).to(equal(PDFContainer.contentLeft)) - - item = result?[1].1 as? PDFAttributedTextObject - expect(item?.frame.origin.x).to(equal(document.layout.margin.left + 20)) - expect(item?.frame.origin.y).to(equal(60)) - #if os(watchOS) - expect(item?.frame.size.width).to(beCloseTo(76.2, within: 1)) - #elseif targetEnvironment(macCatalyst) - expect(item?.frame.size.width).to(beCloseTo(78, within: 1)) - #else - expect(item?.frame.size.width).to(beCloseTo(75, within: 1)) - #endif - #if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.size.height).to(equal(21.0)) - #else - expect(item?.frame.size.height).to(equal(20.0)) - #endif - expect(item?.simpleText).to(equal(PDFSimpleText(text: "Heading 1"))) - - expect(result?[2].0).to(equal(PDFContainer.contentLeft)) - - item = result?[2].1 as? PDFAttributedTextObject - expect(item?.frame.origin.x).to(equal(document.layout.margin.left + 10)) - #if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.origin.y).to(equal(81)) - #else - expect(item?.frame.origin.y).to(equal(80)) - #endif - expect(item?.frame.size.width).to(beCloseTo(13, within: 1)) - #if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.size.height).to(equal(21.0)) - #else - expect(item?.frame.size.height).to(equal(20.0)) - #endif - expect(item?.simpleText).to(equal(PDFSimpleText(text: "?."))) - - expect(result?[3].0).to(equal(PDFContainer.contentLeft)) - - item = result?[3].1 as? PDFAttributedTextObject - expect(item?.frame.origin.x).to(equal(document.layout.margin.left + 20)) - #if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.origin.y).to(equal(81)) - #else - expect(item?.frame.origin.y).to(equal(80)) - #endif - #if targetEnvironment(macCatalyst) - expect(item?.frame.size.width).to(beCloseTo(80, within: 1)) - #else - expect(item?.frame.size.width).to(beCloseTo(78, within: 1)) - #endif - #if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.size.height).to(equal(21.0)) - #else - expect(item?.frame.size.height).to(equal(20.0)) - #endif - expect(item?.simpleText).to(equal(PDFSimpleText(text: "Heading 2"))) - - expect(result?[4].0).to(equal(PDFContainer.contentLeft)) - - item = result?[4].1 as? PDFAttributedTextObject - expect(item?.frame.origin.x).to(equal(document.layout.margin.left + 10)) -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.origin.y).to(equal(102)) - #else - expect(item?.frame.origin.y).to(equal(100)) - #endif - #if os(watchOS) - expect(item?.frame.size.width).to(beCloseTo(5.4, within: 1)) - #else - expect(item?.frame.size.width).to(beCloseTo(4, within: 1)) - #endif -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.size.height).to(equal(21.0)) - #else - expect(item?.frame.size.height).to(equal(20.0)) - #endif - expect(item?.simpleText).to(equal(PDFSimpleText(text: PDFListItemSymbol.dot.stringValue))) - - expect(result?[5].0).to(equal(PDFContainer.contentLeft)) - - item = result?[5].1 as? PDFAttributedTextObject - expect(item?.frame.origin.x).to(equal(document.layout.margin.left + 20)) -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.origin.y).to(equal(102)) - #else - expect(item?.frame.origin.y).to(equal(100)) - #endif -#if targetEnvironment(macCatalyst) - expect(item?.frame.size.width).to(beCloseTo(81, within: 1)) -#else - expect(item?.frame.size.width).to(beCloseTo(79, within: 1)) - #endif -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.size.height).to(equal(21.0)) - #else - expect(item?.frame.size.height).to(equal(20.0)) - #endif - expect(item?.simpleText).to(equal(PDFSimpleText(text: "Heading 3"))) - - expect(result?[6].0).to(equal(PDFContainer.contentLeft)) - - item = result?[6].1 as? PDFAttributedTextObject - expect(item?.frame.origin.x).to(equal(document.layout.margin.left + 20)) -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.origin.y).to(equal(123)) - #else - expect(item?.frame.origin.y).to(equal(120)) - #endif - #if os(watchOS) - expect(item?.frame.size.width).to(beCloseTo(6.9, within: 1)) - #else - expect(item?.frame.size.width).to(beCloseTo(8, within: 1)) - #endif -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.size.height).to(equal(21.0)) - #else - expect(item?.frame.size.height).to(equal(20.0)) - #endif - expect(item?.simpleText).to(equal(PDFSimpleText(text: PDFListItemSymbol.dash.stringValue))) - - expect(result?[7].0).to(equal(PDFContainer.contentLeft)) - - item = result?[7].1 as? PDFAttributedTextObject - expect(item?.frame.origin.x).to(equal(document.layout.margin.left + 30)) -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.origin.y).to(equal(123)) - #else - expect(item?.frame.origin.y).to(equal(120)) - #endif - #if os(watchOS) - expect(item?.frame.size.width).to(beCloseTo(104.5, within: 1)) - #elseif targetEnvironment(macCatalyst) - expect(item?.frame.size.width).to(beCloseTo(106.4, within: 1)) -#else - expect(item?.frame.size.width).to(beCloseTo(103, within: 1)) - #endif -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.size.height).to(equal(21.0)) - #else - expect(item?.frame.size.height).to(equal(20.0)) - #endif - expect(item?.simpleText).to(equal(PDFSimpleText(text: "Subheading 1"))) - - expect(result?[8].0).to(equal(PDFContainer.contentLeft)) - - item = result?[8].1 as? PDFAttributedTextObject - expect(item?.frame.origin.x).to(equal(document.layout.margin.left + 30)) -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.origin.y).to(equal(144)) - #else - expect(item?.frame.origin.y).to(equal(140)) - #endif -#if targetEnvironment(macCatalyst) - expect(item?.frame.size.width).to(beCloseTo(108.8, within: 1)) -#else - expect(item?.frame.size.width).to(beCloseTo(106, within: 1)) - #endif -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.size.height).to(equal(21.0)) - #else - expect(item?.frame.size.height).to(equal(20.0)) - #endif - expect(item?.simpleText).to(equal(PDFSimpleText(text: "Subheading 2"))) - - expect(result?[9].0).to(equal(PDFContainer.contentLeft)) - - item = result?[9].1 as? PDFAttributedTextObject - expect(item?.frame.origin.x).to(equal(document.layout.margin.left + 20)) -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.origin.y).to(equal(165)) - #else - expect(item?.frame.origin.y).to(equal(160)) - #endif - #if os(watchOS) - expect(item?.frame.size.width).to(beCloseTo(5.4, within: 1)) - #else - expect(item?.frame.size.width).to(beCloseTo(4, within: 1)) - #endif -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.size.height).to(equal(21.0)) - #else - expect(item?.frame.size.height).to(equal(20.0)) - #endif - expect(item?.simpleText).to(equal(PDFSimpleText(text: PDFListItemSymbol.dot.stringValue))) - - expect(result?[10].0).to(equal(PDFContainer.contentLeft)) - - item = result?[10].1 as? PDFAttributedTextObject - expect(item?.frame.origin.x).to(equal(document.layout.margin.left + 30)) -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.origin.y).to(equal(165)) - #else - expect(item?.frame.origin.y).to(equal(160)) - #endif - #if os(watchOS) - expect(item?.frame.size.width).to(beCloseTo(106.2, within: 1)) - #elseif targetEnvironment(macCatalyst) - expect(item?.frame.size.width).to(beCloseTo(109.2, within: 1)) - #else - expect(item?.frame.size.width).to(beCloseTo(105.4, within: 1)) - #endif -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.size.height).to(equal(21.0)) - #else - expect(item?.frame.size.height).to(equal(20.0)) - #endif - expect(item?.simpleText).to(equal(PDFSimpleText(text: "Subheading 3"))) - - expect(result?[11].0).to(equal(PDFContainer.contentLeft)) - - item = result?[11].1 as? PDFAttributedTextObject - expect(item?.frame.origin.x).to(equal(document.layout.margin.left + 10)) -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.origin.y).to(equal(186)) - #else - expect(item?.frame.origin.y).to(equal(180)) - #endif - #if os(watchOS) - expect(item?.frame.size.width).to(beCloseTo(9.47, within: 1)) - #else - expect(item?.frame.size.width).to(beCloseTo(11, within: 1)) - #endif -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.size.height).to(equal(21.0)) - #else - expect(item?.frame.size.height).to(equal(20.0)) - #endif - expect(item?.simpleText).to(equal(PDFSimpleText(text: "+"))) - - expect(result?[12].0).to(equal(PDFContainer.contentLeft)) - - item = result?[12].1 as? PDFAttributedTextObject - expect(item?.frame.origin.x).to(equal(document.layout.margin.left + 20)) -#if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.origin.y).to(equal(186)) - #else - expect(item?.frame.origin.y).to(equal(180)) - #endif - #if targetEnvironment(macCatalyst) - expect(item?.frame.size.width).to(beCloseTo(81.2, within: 1)) - #else - expect(item?.frame.size.width).to(beCloseTo(79, within: 1)) - #endif - #if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item?.frame.size.height).to(equal(21.0)) - #else - expect(item?.frame.size.height).to(equal(20.0)) - #endif - expect(item?.simpleText).to(equal(PDFSimpleText(text: "Heading 4"))) - } - - it("should use zero indentation") { - object.list.levelIndentations = [] - var result: [PDFLocatedRenderObject]? - - expect { - result = try? object.calculate(generator: generator, container: container) - }.toNot(throwError()) - - expect(result).toNot(beNil()) - expect(result?.count).to(equal(13)) - - expect(result?[0].0).to(equal(PDFContainer.contentLeft)) - - guard let item = result?[0].1 as? PDFAttributedTextObject else { - fail() - return - } - expect(item.frame.origin.x) == document.layout.margin.left - expect(item.frame.origin.y) == 60 - expect(item.frame.size.width).to(beCloseTo(12, within: 1)) - #if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(item.frame.size.height) == 21 - #else - expect(item.frame.size.height) == 20 - #endif - expect(item.simpleText) == PDFSimpleText(text: "1.") - - expect(result?[1].0).to(equal(PDFContainer.contentLeft)) - - guard let otherItem = result?[1].1 as? PDFAttributedTextObject else { - fail() - return - } - expect(otherItem.frame.origin.x) == document.layout.margin.left - expect(otherItem.frame.origin.y) == 60 - #if targetEnvironment(macCatalyst) - expect(otherItem.frame.size.width).to(beCloseTo(78.2, within: 1)) - #else - expect(otherItem.frame.size.width).to(beCloseTo(76, within: 1)) - #endif - #if (os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)) && !targetEnvironment(macCatalyst) - expect(otherItem.frame.size.height) == 21 - #else - expect(otherItem.frame.size.height) == 20 - #endif - expect(otherItem.simpleText) == PDFSimpleText(text: "Heading 1") - - expect(result?[2].0).to(equal(PDFContainer.contentLeft)) - } - } - } - } - // swiftlint:enable closure_body_length function_body_length -} diff --git a/Tests/TPPDFTests/List/PDFListItemSymbolTests.swift b/Tests/TPPDFTests/List/PDFListItemSymbolTests.swift new file mode 100644 index 00000000..01db00a2 --- /dev/null +++ b/Tests/TPPDFTests/List/PDFListItemSymbolTests.swift @@ -0,0 +1,69 @@ +// +// PDFListItemSymbolTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 05.12.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFListItemSymbolTests { + @Test(arguments: [ + (0, "", "none"), (1, "", "inherit"), (2, "\u{00B7}", "dot"), + (3, "-", "dash"), (4, "%@", "custom|%@"), (5, "%@.", "numbered|%@"), + ]) + func symbolsProvideExpectedDisplayTextAndRawValues(index: Int, expectedText: String, expectedRawValue: String) { + // Arrange + let symbols: [PDFListItemSymbol] = [.none, .inherit, .dot, .dash, .custom(value: "%@"), .numbered(value: "%@")] + let symbol = symbols[index] + + // Act + let text = symbol.stringValue + let rawValue = symbol.rawValue + + // Assert + #expect(text == expectedText) + #expect(rawValue == expectedRawValue) + } + + @Test func numberedSymbolWithoutTemplateHasNilRawValueSuffix() { + // Arrange + let symbol = PDFListItemSymbol.numbered(value: nil) + + // Act + let rawValue = symbol.rawValue + + // Assert + #expect(rawValue == "numbered|nil") + } + + @Test func rawValueTypeIsString() { + // Arrange + let rawValueType = PDFListItemSymbol.RawValue.self + + // Act + let isString = rawValueType == String.self + + // Assert + #expect(isString) + } + + @Test(arguments: [ + ("none", "none"), ("inherit", "inherit"), ("dot", "dot"), ("dash", "dash"), + ("", "none"), ("|", "none"), ("||", "none"), ("asdf|", "none"), + ("custom|", "custom|"), ("custom|%@", "custom|%@"), + ("numbered|", "numbered|"), ("numbered|nil", "numbered|nil"), ("numbered|%@", "numbered|%@"), + ]) + func initializingFromRawValuePreservesRecognizedValuesAndFallsBackForUnknownValues(rawValue: String, expectedValue: String) { + // Arrange + let input = rawValue + + // Act + let symbol = PDFListItemSymbol(rawValue: input) + + // Assert + #expect(symbol?.rawValue == expectedValue) + } +} diff --git a/Tests/TPPDFTests/List/PDFListItemSymbol_Spec.swift b/Tests/TPPDFTests/List/PDFListItemSymbol_Spec.swift deleted file mode 100644 index 31ada516..00000000 --- a/Tests/TPPDFTests/List/PDFListItemSymbol_Spec.swift +++ /dev/null @@ -1,67 +0,0 @@ -// -// PDFListItemSymbol_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 05.12.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class PDFListItemSymbol_Spec: QuickSpec { - // swiftlint:disable closure_body_length - override func spec() { - describe("PDFListItemSymbol") { - it("should have enum values") { - expect(PDFListItemSymbol.none).toNot(beNil()) - expect(PDFListItemSymbol.inherit).toNot(beNil()) - expect(PDFListItemSymbol.dot).toNot(beNil()) - expect(PDFListItemSymbol.dash).toNot(beNil()) - expect(PDFListItemSymbol.custom(value: "%@")).toNot(beNil()) - expect(PDFListItemSymbol.numbered(value: "%@")).toNot(beNil()) - } - - it("has a string value") { - expect(PDFListItemSymbol.none.stringValue) == "" // swiftlint:disable:this empty_string - expect(PDFListItemSymbol.inherit.stringValue) == "" // swiftlint:disable:this empty_string - expect(PDFListItemSymbol.dot.stringValue) == "\u{00B7}" - expect(PDFListItemSymbol.dash.stringValue) == "-" - expect(PDFListItemSymbol.custom(value: "%@").stringValue) == "%@" - expect(PDFListItemSymbol.numbered(value: "%@").stringValue) == "%@." - } - - it("has String as RawValue") { - expect(type(of: PDFListItemSymbol.RawValue.self) == type(of: String.self)).to(beTrue()) - } - - it("has a custom raw value") { - expect(PDFListItemSymbol.none.rawValue) == "none" - expect(PDFListItemSymbol.inherit.rawValue) == "inherit" - expect(PDFListItemSymbol.dot.rawValue) == "dot" - expect(PDFListItemSymbol.dash.rawValue) == "dash" - expect(PDFListItemSymbol.custom(value: "%@").rawValue) == "custom|%@" - expect(PDFListItemSymbol.numbered(value: "%@").rawValue) == "numbered|%@" - expect(PDFListItemSymbol.numbered(value: nil).rawValue) == "numbered|nil" - } - - it("can be initialized with raw value") { - expect(PDFListItemSymbol(rawValue: "none")?.rawValue) == PDFListItemSymbol.none.rawValue - expect(PDFListItemSymbol(rawValue: "inherit")?.rawValue) == PDFListItemSymbol.inherit.rawValue - expect(PDFListItemSymbol(rawValue: "dot")?.rawValue) == PDFListItemSymbol.dot.rawValue - expect(PDFListItemSymbol(rawValue: "dash")?.rawValue) == PDFListItemSymbol.dash.rawValue - expect(PDFListItemSymbol(rawValue: "")?.rawValue) == PDFListItemSymbol.none.rawValue - expect(PDFListItemSymbol(rawValue: "|")?.rawValue) == PDFListItemSymbol.none.rawValue - expect(PDFListItemSymbol(rawValue: "||")?.rawValue) == PDFListItemSymbol.none.rawValue - expect(PDFListItemSymbol(rawValue: "asdf|")?.rawValue) == PDFListItemSymbol.none.rawValue - expect(PDFListItemSymbol(rawValue: "custom|")?.rawValue) == PDFListItemSymbol.custom(value: "").rawValue - expect(PDFListItemSymbol(rawValue: "custom|%@")?.rawValue) == PDFListItemSymbol.custom(value: "%@").rawValue - expect(PDFListItemSymbol(rawValue: "numbered|")?.rawValue) == PDFListItemSymbol.numbered(value: "").rawValue - expect(PDFListItemSymbol(rawValue: "numbered|nil")?.rawValue) == PDFListItemSymbol.numbered(value: nil).rawValue - expect(PDFListItemSymbol(rawValue: "numbered|%@")?.rawValue) == PDFListItemSymbol.numbered(value: "%@").rawValue - } - } - } - // swiftlint:enable closure_body_length -} diff --git a/Tests/TPPDFTests/List/PDFListItemTests.swift b/Tests/TPPDFTests/List/PDFListItemTests.swift new file mode 100644 index 00000000..08cb08ab --- /dev/null +++ b/Tests/TPPDFTests/List/PDFListItemTests.swift @@ -0,0 +1,81 @@ +// +// PDFListItemTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 05.12.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import Testing +@testable import TPPDF + +struct PDFListItemTests { + @Test func initializingWithoutArgumentsUsesInheritedSymbolAndNoContentOrRelations() { + // Arrange + let expectedSymbol = "inherit" + + // Act + let item = PDFListItem() + + // Assert + #expect(item.parent == nil) + #expect(item.content == nil) + #expect(item.children == nil) + #expect(item.symbol.rawValue == expectedSymbol) + } + + @Test func initializingWithSymbolPreservesSymbol() { + // Arrange + let symbol = PDFListItemSymbol.dash + + // Act + let item = PDFListItem(symbol: symbol) + + // Assert + #expect(item.symbol.rawValue == "dash") + } + + @Test func initializingWithContentPreservesContent() { + // Arrange + let content = "EXAMPLE" + + // Act + let item = PDFListItem(content: content) + + // Assert + #expect(item.content == content) + } + + @Test(arguments: [1, 2]) + func addingChildrenReturnsParentAndPreservesParentLinksContentAndSymbols(count: Int) throws { + // Arrange + let parent = PDFListItem() + let items = [PDFListItem(symbol: .dash, content: "A1"), PDFListItem(symbol: .dot, content: "A2")] + + // Act + let result = count == 1 ? parent.addItem(items[0]) : parent.addItems(items) + + // Assert + #expect(result === parent) + let children = try #require(parent.children) + try #require(children.count == count) + for index in 0.. PDFList { + let list = PDFList(indentations: [(pre: 10, past: 10), (pre: 20, past: 10)]) + list.addItem(PDFListItem(symbol: .numbered(value: "1"), content: "Heading 1")) + .addItem(PDFListItem(symbol: .numbered(value: nil), content: "Heading 2")) + .addItem(PDFListItem(symbol: .dot, content: "Heading 3") + .addItem(PDFListItem(symbol: .dash, content: "Subheading 1")) + .addItem(PDFListItem(symbol: .none, content: "Subheading 2")) + .addItem(PDFListItem(symbol: .inherit, content: "Subheading 3"))) + .addItem(PDFListItem(symbol: .custom(value: "+"), content: "Heading 4")) + return list + } +} diff --git a/Tests/TPPDFTests/List/PDFListTests.swift b/Tests/TPPDFTests/List/PDFListTests.swift new file mode 100644 index 00000000..799c70cb --- /dev/null +++ b/Tests/TPPDFTests/List/PDFListTests.swift @@ -0,0 +1,86 @@ +// +// PDFListTests.swift +// TPPDF +// +// Created by Philip Niedertscheider on 06.13.2017. +// Copyright © 2016-2026 techprimate GmbH. All rights reserved. +// + +import CoreGraphics +import Testing +@testable import TPPDF + +struct PDFListTests { + @Test func initializingListPreservesLevelIndentationsAndStartsEmpty() { + // Arrange + let indentations: [(pre: CGFloat, past: CGFloat)] = [(0, 10), (10, 10), (20, 10)] + + // Act + let list = PDFList(indentations: indentations) + let initialCount = list.count + + // Assert + #expect(initialCount == 0) + for index in indentations.indices { + #expect(list.levelIndentations[index].pre == indentations[index].pre) + #expect(list.levelIndentations[index].past == indentations[index].past) + } + } + + @Test func addingItemReturnsListAndPreservesContentAndSymbol() { + // Arrange + let list = PDFList(indentations: [(0, 10), (10, 10), (20, 10)]) + let item = PDFListItem(symbol: .dash, content: "Item 1") + + // Act + let result = list.addItem(item) + + // Assert + #expect(result === list) + #expect(list.count == 1) + #expect(list.items[0].content == "Item 1") + #expect(list.items[0].symbol.rawValue == "dash") + } + + @Test func clearingThenAddingNestedItemsFlattensLevelsContentAndInheritedSymbols() throws { + // Arrange + let list = PDFList(indentations: [(0, 10), (10, 10), (20, 10)]) + list.addItem(PDFListItem(symbol: .dash, content: "Item 1")) + let items = [ + PDFListItem(symbol: .dot, content: "Base 1").addItems([ + PDFListItem(symbol: .numbered(value: nil)).addItems([ + PDFListItem(content: "Child 2.1"), + PDFListItem(symbol: .none, content: "Child 2.2"), + PDFListItem(content: "Child 2.3"), + ]), + ]), + PDFListItem(symbol: .dash, content: "Base 2").addItems([ + PDFListItem(content: "Child 2.1").addItems([ + PDFListItem(symbol: .dot, content: "Child 2.1.1"), + ]), + PDFListItem(content: "Child 2.2").addItems([ + PDFListItem(symbol: .dash, content: "Child 2.2.1"), + ]), + ]), + ] + let expectedItems = [ + (0, "Base 1", "dot"), (1, "Child 2.1", "numbered|1"), (1, "Child 2.2", "none"), + (1, "Child 2.3", "numbered|3"), (0, "Base 2", "dash"), (1, "Child 2.1", "dash"), + (2, "Child 2.1.1", "dot"), (1, "Child 2.2", "dash"), (2, "Child 2.2.1", "dash"), + ] + + // Act + list.clear() + let result = list.addItems(items) + let flattened = list.flatted() + + // Assert + #expect(result === list) + try #require(flattened.count == 9) + for (index, expected) in expectedItems.enumerated() { + #expect(flattened[index].level == expected.0) + #expect(flattened[index].text == expected.1) + #expect(flattened[index].symbol.rawValue == expected.2) + } + } +} diff --git a/Tests/TPPDFTests/List/PDFList_Spec.swift b/Tests/TPPDFTests/List/PDFList_Spec.swift deleted file mode 100644 index a3567bc1..00000000 --- a/Tests/TPPDFTests/List/PDFList_Spec.swift +++ /dev/null @@ -1,121 +0,0 @@ -// -// PDFList_Spec.swift -// TPPDF -// -// Created by Philip Niedertscheider on 06.13.2017. -// Copyright © 2016-2026 techprimate GmbH. All rights reserved. -// - -import Nimble -import Quick -@testable import TPPDF - -class ListSpec: QuickSpec { - // swiftlint:disable closure_body_length function_body_length - override func spec() { - describe("PDFList") { - let list = PDFList(indentations: [(pre: 0.0, past: 10.0), (pre: 10.0, past: 10.0), (pre: 20.0, past: 10.0)]) - it("has no items by default") { - expect(list.count).to(equal(0)) - } - - it("should have different level indentations") { - expect(list.levelIndentations[0].pre).to(equal(0.0)) - expect(list.levelIndentations[0].past).to(equal(10.0)) - expect(list.levelIndentations[1].pre).to(equal(10.0)) - expect(list.levelIndentations[1].past).to(equal(10.0)) - expect(list.levelIndentations[2].pre).to(equal(20.0)) - expect(list.levelIndentations[2].past).to(equal(10.0)) - } - - it("should have one item") { - let result = list.addItem(PDFListItem(symbol: .dash, content: "Item 1")) - - expect(result) === list - expect(list.count).to(equal(1)) - expect(list.items[0].content).to(equal("Item 1")) - expect(list.items[0].symbol.rawValue).to(equal(PDFListItemSymbol.dash.rawValue)) - } - - it("should flatten to four items") { - list.clear() - - let result = list.addItems( - [ - PDFListItem(symbol: .dot, content: "Base 1") - .addItems( - [ - PDFListItem(symbol: .numbered(value: nil)) - .addItems( - [ - PDFListItem(content: "Child 2.1"), - PDFListItem(symbol: .none, content: "Child 2.2"), - PDFListItem(content: "Child 2.3"), - ] - ), - ] - ), - PDFListItem(symbol: .dash, content: "Base 2") - .addItems( - [ - PDFListItem(content: "Child 2.1") - .addItems( - [ - PDFListItem(symbol: .dot, content: "Child 2.1.1"), - ]), - PDFListItem(content: "Child 2.2") - .addItems( - [ - PDFListItem(symbol: .dash, content: "Child 2.2.1"), - ] - ), - ] - ), - ] - ) - - expect(result) === list - - let flattedList = list.flatted() - expect(flattedList.count).to(equal(9)) - - expect(flattedList[0].level).to(equal(0)) - expect(flattedList[0].text).to(equal("Base 1")) - expect(flattedList[0].symbol.rawValue).to(equal(PDFListItemSymbol.dot.rawValue)) - - expect(flattedList[1].level).to(equal(1)) - expect(flattedList[1].text).to(equal("Child 2.1")) - expect(flattedList[1].symbol.rawValue).to(equal(PDFListItemSymbol.numbered(value: "1").rawValue)) - - expect(flattedList[2].level).to(equal(1)) - expect(flattedList[2].text).to(equal("Child 2.2")) - expect(flattedList[2].symbol.rawValue).to(equal(PDFListItemSymbol.none.rawValue)) - - expect(flattedList[3].level).to(equal(1)) - expect(flattedList[3].text).to(equal("Child 2.3")) - expect(flattedList[3].symbol.rawValue).to(equal(PDFListItemSymbol.numbered(value: "3").rawValue)) - - expect(flattedList[4].level).to(equal(0)) - expect(flattedList[4].text).to(equal("Base 2")) - expect(flattedList[4].symbol.rawValue).to(equal(PDFListItemSymbol.dash.rawValue)) - - expect(flattedList[5].level).to(equal(1)) - expect(flattedList[5].text).to(equal("Child 2.1")) - expect(flattedList[5].symbol.rawValue).to(equal(PDFListItemSymbol.dash.rawValue)) - - expect(flattedList[6].level).to(equal(2)) - expect(flattedList[6].text).to(equal("Child 2.1.1")) - expect(flattedList[6].symbol.rawValue).to(equal(PDFListItemSymbol.dot.rawValue)) - - expect(flattedList[7].level).to(equal(1)) - expect(flattedList[7].text).to(equal("Child 2.2")) - expect(flattedList[7].symbol.rawValue).to(equal(PDFListItemSymbol.dash.rawValue)) - - expect(flattedList[8].level).to(equal(2)) - expect(flattedList[8].text).to(equal("Child 2.2.1")) - expect(flattedList[8].symbol.rawValue).to(equal(PDFListItemSymbol.dash.rawValue)) - } - } - } - // swiftlint:enable closure_body_length function_body_length -}