Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ if isTestingEnabled {
"Quick",
"Nimble",
], resources: [
.copy("resources/graphics-fixture.gif"),
.copy("resources/sample.pdf"),
]),
.testTarget(name: "TPPDFIntegrationTests", dependencies: [
Expand Down
35 changes: 35 additions & 0 deletions Tests/TPPDFTests/Graphics/ColorCloseToEqualTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// ColorCloseToEqualTests.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 ColorCloseToEqualTests {
@Test(arguments: [
(0.79999, 0.69999, 0.59999, 0.49999, true),
(0.80001, 0.70001, 0.60001, 0.50001, true),
(0.79998, 0.7, 0.6, 0.5, false),
(0.8, 0.69998, 0.6, 0.5, false),
(0.8, 0.7, 0.59998, 0.5, false),
(0.8, 0.7, 0.6, 0.49998, false),
])
func comparingColorsRespectsToleranceForEachComponent(
red: Double, green: Double, blue: Double, alpha: Double, expectedCloseness: Bool
) {
// Arrange
let color = Color(red: 0.8, green: 0.7, blue: 0.6, alpha: 0.5)
let otherColor = Color(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: CGFloat(alpha))

// Act
let isClose = color.isClose(to: otherColor, decimals: 5)

// Assert
#expect(isClose == expectedCloseness)
}
}
136 changes: 136 additions & 0 deletions Tests/TPPDFTests/Graphics/PDFGraphicsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//
// PDFGraphicsTests.swift
// TPPDF
//
// Created by Philip Niedertscheider on 11.02.2017.
// Copyright © 2016-2026 techprimate GmbH. All rights reserved.
//

import CoreGraphics
import Foundation
import Testing
@testable import TPPDF

struct PDFGraphicsTests {
@Test func dashedLinesUseButtCapsAndLengthsProportionalToWidth() throws {
// Arrange
let style = PDFLineStyle(type: .dashed, color: .red, width: 5)

// Act
let dashes = try #require(PDFGraphics.createDashes(style: style))

// Assert
#expect(dashes.lengths == [15, 15])
#expect(dashes.cap == .butt)
}

@Test func dottedLinesUseRoundCapsAndLengthsProportionalToWidth() throws {
// Arrange
let style = PDFLineStyle(type: .dotted, color: .red, width: 5)

// Act
let dashes = try #require(PDFGraphics.createDashes(style: style))

// Assert
#expect(dashes.lengths == [0, 10])
#expect(dashes.cap == .round)
}

@Test func linesWithoutDashesDoNotCreateDashPattern() {
// Arrange
let style = PDFLineStyle(type: .none, color: .red, width: 5)

// Act
let dashes = PDFGraphics.createDashes(style: style)

// Assert
#expect(dashes == nil)
}

@Test func resizingWithoutCompressionCreatesImageAtRequestedQuality() throws {
// Arrange
let image = try loadImage()
let frame = CGRect(x: 0, y: 0, width: 40, height: 40)

// Act
let result = PDFGraphics.resizeAndCompressImage(
image: image, frame: frame, shouldResize: true, shouldCompress: false, quality: 0.2
)

// Assert
#expect(image.size == CGSize(width: 61, height: 68))
#expect(result !== image)
#expect(abs(result.size.width - 24) < 0.0001)
#expect(abs(result.size.height - 24) < 0.0001)
}

@Test func compressingWithoutResizingPreservesImageSize() throws {
// Arrange
let image = try loadImage()
let frame = CGRect(x: 0, y: 0, width: 40, height: 40)

// Act
let result = PDFGraphics.resizeAndCompressImage(
image: image, frame: frame, shouldResize: false, shouldCompress: true, quality: 0.2
)

// Assert
#expect(image.size == CGSize(width: 61, height: 68))
#expect(result !== image)
#expect(result.size == image.size)
}

@Test(arguments: [(0.2, 24.0), (0.0, 8.0)])
func resizingAndCompressingUsesQualityToDetermineSize(quality: Double, expectedLength: Double) throws {
// Arrange
let image = try loadImage()
let frame = CGRect(x: 0, y: 0, width: 40, height: 40)
let expectedSize = CGSize(width: expectedLength, height: expectedLength)

// Act
let result = PDFGraphics.resizeAndCompressImage(
image: image, frame: frame, shouldResize: true, shouldCompress: true, quality: CGFloat(quality)
)

// Assert
#expect(image.size == CGSize(width: 61, height: 68))
#expect(result !== image)
#expect(result.size == expectedSize)
}

@Test func disablingResizingAndCompressionReturnsOriginalImage() throws {
// Arrange
let image = try loadImage()
let frame = CGRect(x: 0, y: 0, width: 40, height: 40)

// Act
let result = PDFGraphics.resizeAndCompressImage(
image: image, frame: frame, shouldResize: false, shouldCompress: false, quality: 0.2
)

// Assert
#expect(image.size == CGSize(width: 61, height: 68))
#expect(result === image)
}

@Test func roundingCornersCreatesNewImageWithoutResizingOrCompression() throws {
// Arrange
let image = try loadImage()
let frame = CGRect(x: 0, y: 0, width: 40, height: 40)

// Act
let result = PDFGraphics.resizeAndCompressImage(
image: image, frame: frame, shouldResize: false, shouldCompress: false, quality: 0,
roundCorners: [.allCorners], cornerRadius: 10
)

// Assert
#expect(image.size == CGSize(width: 61, height: 68))
#expect(result !== image)
}

private func loadImage() throws -> Image {
let url = try #require(Bundle.module.url(forResource: "graphics-fixture", withExtension: "gif"))
return try #require(Image(data: Data(contentsOf: url)))
}
}
138 changes: 0 additions & 138 deletions Tests/TPPDFTests/Graphics/PDFGraphics_Spec.swift

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// PDFLineSeparatorObjectEquatableTests.swift
// TPPDF
//
// Created by Philip Niedertscheider on 11.12.2017.
// Copyright © 2016-2026 techprimate GmbH. All rights reserved.
//

import Testing
@testable import TPPDF

struct PDFLineSeparatorObjectEquatableTests {
@Test func separatorsWithSameStyleAreEqual() {
// Arrange
let style = PDFLineStyle(type: .dotted, color: .orange, width: 0.25)
let separator = PDFLineSeparatorObject(style: style)
let otherSeparator = PDFLineSeparatorObject(style: style)

// Act
let isEqual = separator == otherSeparator

// Assert
#expect(isEqual)
}

@Test func separatorsWithDifferentStylesAreUnequal() {
// Arrange
let firstStyle = PDFLineStyle(type: .dotted, color: .orange, width: 0.25)
let secondStyle = PDFLineStyle(type: .dashed, color: .blue, width: 1)
let separator = PDFLineSeparatorObject(style: firstStyle)
let otherSeparator = PDFLineSeparatorObject(style: secondStyle)

// Act
let isEqual = separator == otherSeparator

// Assert
#expect(!isEqual)
}
}
Loading
Loading