Skip to content

Commit

Permalink
added Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixHerrmann committed Oct 26, 2021
1 parent aa4a713 commit 026fbd6
Show file tree
Hide file tree
Showing 6 changed files with 433 additions and 2 deletions.
105 changes: 105 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/UIImageColors-Package.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1310"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "UIImageColors"
BuildableName = "UIImageColors"
BlueprintName = "UIImageColors"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "UIImageColorsObjc"
BuildableName = "UIImageColorsObjc"
BlueprintName = "UIImageColorsObjc"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "UIImageColorsTests"
BuildableName = "UIImageColorsTests"
BlueprintName = "UIImageColorsTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "UIImageColorsTests"
BuildableName = "UIImageColorsTests"
BlueprintName = "UIImageColorsTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "UIImageColors"
BuildableName = "UIImageColors"
BlueprintName = "UIImageColors"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
52 changes: 52 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/UIImageColorsTests.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1300"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "UIImageColorsTests"
BuildableName = "UIImageColorsTests"
BlueprintName = "UIImageColorsTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
11 changes: 9 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ let package = Package(
],
targets: [
.target(name: "UIImageColors"),
.target(name: "UIImageColorsObjc",
dependencies: [.target(name: "UIImageColors")])
.target(
name: "UIImageColorsObjc",
dependencies: [.target(name: "UIImageColors")]),
.testTarget(
name: "UIImageColorsTests",
dependencies: [
.target(name: "UIImageColors"),
.target(name: "UIImageColorsObjc")
])
]
)
43 changes: 43 additions & 0 deletions Tests/UIImageColorsTests/Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Helpers.swift
// UIImageColors
//
// Created by Felix Herrmann on 26.10.21.
//

#if canImport(UIKit)
import UIKit
#elseif canImport(AppKit)
import AppKit
#endif


// MARK: - Color + rgb

#if canImport(UIKit)

extension UIColor {

internal var rgb: (Int, Int, Int) {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
getRed(&red, green: &green, blue: &blue, alpha: nil)
return (Int((red * 255).rounded()), Int((green * 255).rounded()), Int((blue * 255).rounded()))
}
}

#elseif canImport(AppKit)

extension NSColor {

internal var rgb: (Int, Int, Int) {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
getRed(&red, green: &green, blue: &blue, alpha: nil)
return (Int((red * 255).rounded()), Int((green * 255).rounded()), Int((blue * 255).rounded()))
}
}

#endif
86 changes: 86 additions & 0 deletions Tests/UIImageColorsTests/UIImageColorsObjcTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// UIImageColorsObjcTests.swift
// UIImageColors
//
// Created by Felix Herrmann on 24.10.21.
//

import XCTest
@testable import UIImageColorsObjc
#if canImport(UIKit)
import UIKit
#elseif canImport(AppKit)
import AppKit
#endif

final class UIImageColorsObjcTests: XCTestCase {

private var examplePath: String {
var url = URL(fileURLWithPath: #filePath)
url.deleteLastPathComponent()
url.deleteLastPathComponent()
url.deleteLastPathComponent()
url.appendPathComponent("Example")
url.appendPathComponent("Shared")
url.appendPathComponent("Assets.xcassets")
url.appendPathComponent("Calvin Klein - Kaws.imageset/Calvin Klein - Kaws.jpg")
return url.path
}

#if canImport(UIKit)
var image: UIImage!
#elseif canImport(AppKit)
var image: NSImage!
#endif

override func setUp() {
super.setUp()

#if canImport(UIKit)
image = UIImage(contentsOfFile: examplePath)
#elseif canImport(AppKit)
image = NSImage(contentsOfFile: examplePath)
#endif

if image == nil {
fatalError("Test-Image could not be loaded")
}
}

func testSynchronousResults() throws {
let colors = try XCTUnwrap(image.getColorsObjc(quality: .full))
let primary = try XCTUnwrap(colors.primary)
let secondary = try XCTUnwrap(colors.secondary)
let detail = try XCTUnwrap(colors.detail)

XCTAssertTrue(colors.background.rgb == (231, 231, 231))
XCTAssertTrue(primary.rgb == (0, 0, 0))
XCTAssertTrue(secondary.rgb == (255, 84, 126))
XCTAssertTrue(detail.rgb == (115, 110, 106) || detail.rgb == (127, 120, 114)) // detail value is not consistent
}

func testAsynchronousResults() throws {
let expectation = XCTestExpectation(description: "Test asynchronous Objective-C results")

image.getColorsObjc(quality: .full) { colors in

do {
let colors = try XCTUnwrap(colors)
let primary = try XCTUnwrap(colors.primary)
let secondary = try XCTUnwrap(colors.secondary)
let detail = try XCTUnwrap(colors.detail)

XCTAssertTrue(colors.background.rgb == (231, 231, 231))
XCTAssertTrue(primary.rgb == (0, 0, 0))
XCTAssertTrue(secondary.rgb == (255, 84, 126))
XCTAssertTrue(detail.rgb == (115, 110, 106) || detail.rgb == (127, 120, 114)) // detail value is not consistent
} catch {
XCTFail(error.localizedDescription)
}

expectation.fulfill()
}

wait(for: [expectation], timeout: 10)
}
}
Loading

0 comments on commit 026fbd6

Please sign in to comment.