Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract TestHelpers to their own target #47

Merged
merged 1 commit into from
Aug 1, 2024
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
10 changes: 8 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ let package = Package(
.copy("PrivacyInfo.xcprivacy")
],
swiftSettings: [
.enableExperimentalFeature("StrictConcurrency=complete")
.enableExperimentalFeature("StrictConcurrency")
]
),
.testTarget(
name: "UID2Tests",
dependencies: ["UID2", "TestHelpers"]
),
.target(
name: "TestHelpers",
dependencies: ["UID2"],
path: "Tests/TestHelpers",
resources: [
.copy("TestData")
]),
]
)
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import Foundation
@testable import UID2

final class FixtureLoader {
public final class FixtureLoader {
enum Error: Swift.Error {
case missingFixture(String)
}

/// Read `Data` from a Fixture.
static func data(
public static func data(
fixture: String,
withExtension fileExtension: String = "json",
subdirectory: String = "TestData"
Expand All @@ -31,7 +31,7 @@ final class FixtureLoader {

/// Decode a `Decodable` from a Fixture.
/// Expects the fixture to use snake_case key encoding.
static func decode<T>(
public static func decode<T>(
_ type: T.Type,
fixture: String,
withExtension fileExtension: String = "json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import Foundation

internal final class HTTPStub {
static let shared: HTTPStub = {
public final class HTTPStub {
public static let shared: HTTPStub = {
let stub = HTTPStub()
URLProtocol.registerClass(HTTPStubProtocol.self)
return stub
Expand All @@ -17,7 +17,7 @@ internal final class HTTPStub {
private init() {}

// Provides stubs in response to requests
var stubs: ((URLRequest) -> Result<(data: Data, response: HTTPURLResponse), Error>)!
public var stubs: ((URLRequest) -> Result<(data: Data, response: HTTPURLResponse), Error>)!

// Stub for the current request
private var stub: Result<(data: Data, response: HTTPURLResponse), Error>!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
import Foundation
@testable import UID2

final class MockNetworkSession: NetworkSession {
public final class MockNetworkSession: NetworkSession {

private let fileName: String
private let fileExtension: String
private let responseCode: Int

init(_ fileName: String, _ fileExtension: String, _ responseCode: Int = 200) {
public init(_ fileName: String, _ fileExtension: String, _ responseCode: Int = 200) {
self.fileName = fileName
self.fileExtension = fileExtension
self.responseCode = responseCode
}

func loadData(for request: URLRequest) throws -> (Data, HTTPURLResponse) {
public func loadData(for request: URLRequest) throws -> (Data, HTTPURLResponse) {
let jsonData = try FixtureLoader.data(fixture: fileName, withExtension: fileExtension)
let response = HTTPURLResponse(url: request.url!, statusCode: responseCode, httpVersion: nil, headerFields: nil)!
return (jsonData, response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ private final class Atomic<Value: Sendable>: @unchecked Sendable {

/// A test convenience which exposes the Symmetric Key it generates for the client.
/// This key can then be used to encrypt stub responses for the client.
internal final class TestCryptoUtil {
public final class TestCryptoUtil {
private let atomicSymmetricKey: Atomic<SymmetricKey?>

/// `SymmetricKey` generated by the client, or `nil` if the key has not yet been generated.
var symmetricKey: SymmetricKey? {
public var symmetricKey: SymmetricKey? {
atomicSymmetricKey.value
}
let cryptoUtil: CryptoUtil
public let cryptoUtil: CryptoUtil

init() {
public init() {
let symmetricKey = Atomic<SymmetricKey?>(nil)
self.atomicSymmetricKey = symmetricKey

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import XCTest

/// `XCTAssertThrowsError` doesn't support async expressions.
internal func assertThrowsError<T>(
public func assertThrowsError<T>(
_ expression: @escaping @autoclosure () async throws -> T,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
Expand Down
1 change: 1 addition & 0 deletions Tests/UID2Tests/IdentityPackageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Brad Leege on 3/29/23.
//

import TestHelpers
import XCTest
@testable import UID2

Expand Down
2 changes: 1 addition & 1 deletion Tests/UID2Tests/RefreshTokenAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Brad Leege on 2/1/23.
//

import TestHelpers
import XCTest
@testable import UID2

Expand All @@ -13,7 +14,6 @@ final class RefreshTokenAPITests: XCTestCase {
/// 🟩 `POST /v2/token/refresh` - HTTP 200 - Success
/// uid2-iOS-sdk@test.com
func testRefreshTokenSuccess() async throws {

// Load Generate Token
let generateTokenResponse = try FixtureLoader.decode(RefreshTokenResponse.self, fixture: "generate-token-200-success")
guard let generateToken = generateTokenResponse.toUID2Identity() else {
Expand Down
1 change: 1 addition & 0 deletions Tests/UID2Tests/UID2ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import CryptoKit
import Foundation
import TestHelpers
@testable import UID2
import XCTest

Expand Down
1 change: 1 addition & 0 deletions Tests/UID2Tests/UID2ManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Combine
import CryptoKit
import Foundation
import TestHelpers
@testable import UID2
import XCTest

Expand Down
1 change: 1 addition & 0 deletions Tests/UID2Tests/UID2TokenTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import TestHelpers
import XCTest
@testable import UID2

Expand Down
Loading