From d9355598da696700fc6bdd1a7f358debfa5cc4d4 Mon Sep 17 00:00:00 2001 From: Marco Saia Date: Mon, 7 Oct 2024 16:36:36 +0200 Subject: [PATCH] iOS: fixed unit tests compatibility with Swift 6 --- .../Tests/DdSdkNativeInitializationTests.swift | 16 +++++++++++----- packages/core/ios/Tests/DdSdkTests.swift | 16 +++++++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/core/ios/Tests/DdSdkNativeInitializationTests.swift b/packages/core/ios/Tests/DdSdkNativeInitializationTests.swift index 3d7151812..e09df5d17 100644 --- a/packages/core/ios/Tests/DdSdkNativeInitializationTests.swift +++ b/packages/core/ios/Tests/DdSdkNativeInitializationTests.swift @@ -9,6 +9,8 @@ import XCTest @testable import DatadogInternal class DdSdkNativeInitializationTests: XCTestCase { + var consoleMessage = "" + override func setUp() { super.setUp() } @@ -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.") } } diff --git a/packages/core/ios/Tests/DdSdkTests.swift b/packages/core/ios/Tests/DdSdkTests.swift index 04eb25f06..a203b825e 100644 --- a/packages/core/ios/Tests/DdSdkTests.swift +++ b/packages/core/ios/Tests/DdSdkTests.swift @@ -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?) {} @@ -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(), @@ -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(), @@ -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 {