Skip to content

Commit

Permalink
iOS: fixed unit tests compatibility with Swift 6
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-saia-datadog committed Oct 7, 2024
1 parent 37f75e6 commit d935559
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
16 changes: 11 additions & 5 deletions packages/core/ios/Tests/DdSdkNativeInitializationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import XCTest
@testable import DatadogInternal

class DdSdkNativeInitializationTests: XCTestCase {
var consoleMessage = ""

override func setUp() {
super.setUp()
}
Expand Down Expand Up @@ -95,18 +97,22 @@ class DdSdkNativeInitializationTests: XCTestCase {

func testPrintsMessageWithIncorrectFile() {
let originalConsolePrint = consolePrint
defer { consolePrint = originalConsolePrint }

var printedMessage = ""
consolePrint = { (msg, level) in printedMessage += msg }
defer {
consolePrint = originalConsolePrint
self.consoleMessage = ""
}

consolePrint = { [weak self] (msg, level) in
self?.consoleMessage += msg
}

let mockJSONFileReader = MockJSONFileReader(mockResourceFilePath: "Fixtures/malformed-configuration")
let nativeInitialization = DdSdkNativeInitialization(
jsonFileReader: mockJSONFileReader
)

XCTAssertNil(nativeInitialization.getConfigurationFromJSONFile())
XCTAssertEqual(printedMessage, "Error parsing datadog-configuration.json file: 🔥 Datadog SDK usage error: JSON configuration file is missing top-level \"configuration\" key.")
XCTAssertEqual(self.consoleMessage, "Error parsing datadog-configuration.json file: 🔥 Datadog SDK usage error: JSON configuration file is missing top-level \"configuration\" key.")
}
}

Expand Down
16 changes: 11 additions & 5 deletions packages/core/ios/Tests/DdSdkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ final class DispatchQueueMock: DispatchQueueType {
}

internal class DdSdkTests: XCTestCase {
var consoleMessage = ""

private func mockResolve(args: Any?) {}
private func mockReject(args: String?, arg: String?, err: Error?) {}

Expand All @@ -39,10 +41,14 @@ internal class DdSdkTests: XCTestCase {

func testSDKInitialization() {
let originalConsolePrint = consolePrint
defer { consolePrint = originalConsolePrint }
defer {
consolePrint = originalConsolePrint
self.consoleMessage = ""
}

var printedMessage = ""
consolePrint = { (msg, level) in printedMessage += msg }
consolePrint = { [weak self] (msg, level) in
self?.consoleMessage += msg
}

DdSdkImplementation(
mainDispatchQueue: DispatchQueueMock(),
Expand All @@ -52,7 +58,7 @@ internal class DdSdkTests: XCTestCase {
RUMMonitorInternalProvider: { nil }
).initialize(configuration: .mockAny(), resolve: mockResolve, reject: mockReject)

XCTAssertEqual(printedMessage, "")
XCTAssertEqual(self.consoleMessage, "")

DdSdkImplementation(
mainDispatchQueue: DispatchQueueMock(),
Expand All @@ -62,7 +68,7 @@ internal class DdSdkTests: XCTestCase {
RUMMonitorInternalProvider: { nil }
).initialize(configuration: .mockAny(), resolve: mockResolve, reject: mockReject)

XCTAssertEqual(printedMessage, "Datadog SDK is already initialized, skipping initialization.")
XCTAssertEqual(self.consoleMessage, "Datadog SDK is already initialized, skipping initialization.")
}

func testResolvesPromiseAfterInitializationIsDone() throws {
Expand Down

0 comments on commit d935559

Please sign in to comment.