From c3c4bf1f1b8f57de53a2a7a08bfc741947bd277d Mon Sep 17 00:00:00 2001 From: Sam Deane Date: Wed, 18 Dec 2024 10:33:27 +0000 Subject: [PATCH] Fixed an issue where isKind(of: failed to recognise embedded tests. --- .../EmbeddedXCTest/EmbeddingController.swift | 170 +++++++++--------- 1 file changed, 84 insertions(+), 86 deletions(-) diff --git a/Sources/SwiftGodotTestability/EmbeddedXCTest/EmbeddingController.swift b/Sources/SwiftGodotTestability/EmbeddedXCTest/EmbeddingController.swift index 87c05067b..c45ac8c65 100644 --- a/Sources/SwiftGodotTestability/EmbeddedXCTest/EmbeddingController.swift +++ b/Sources/SwiftGodotTestability/EmbeddedXCTest/EmbeddingController.swift @@ -17,106 +17,104 @@ import XCTest /// host, supplying it a closure that will re-run the tests. /// The host can then run the tests in a different context. public class EmbeddingController: NSObject, XCTestObservation { - /// The shared instance of the observer. - nonisolated(unsafe) static var _instance: EmbeddingController? - - /// The host that will re-run the tests. - let host: TestHost - - /// Initialise the observer with a host. - init(host: TestHost) { - self.host = host - } - - /// Test suite we build up by observing the normal test run. - /// We'll re-run this suite later, with the isRunning flag set to true, - let embeddedSuite = XCTestSuite(name: "Embedded Tests") - - /// Failures that occurred during the embedded test run. - var embeddedFailures: [(XCTestCase, String, String?, Int)] = [] - - /// Are we currently running the embedded tests? - var isRunningEmbedded = false - - /// Are the embedded tests currently running? - public static var isRunningEmbedded: Bool { _instance?.isRunningEmbedded ?? false } - - /// Install hooks into the testing system. - /// - /// We add ourselves as an observer of XCTest events, so that we can - /// collect information about the tests that are being run. - /// - /// Once the normal test run is complete, we re-run the tests that - /// we've collected, with our isRunning flag set to true, so that - /// the test bodies are actually executed. - public static func setUp(hostClass: TestHost.Type) { - if _instance == nil { - // turn off buffering on stdout so that we see the output immediately - setbuf(__stdoutp, nil) - - let observer = EmbeddingController(host: hostClass.init()) - _instance = observer - XCTestObservationCenter.shared.addTestObserver(observer) + /// The shared instance of the observer. + nonisolated(unsafe) static var _instance: EmbeddingController? + + /// The host that will re-run the tests. + let host: TestHost + + /// Initialise the observer with a host. + init(host: TestHost) { + self.host = host } - } - - /// Record a test suite that has finished running. - func registerSuite(_ suite: XCTestSuite) { - if !isRunningEmbedded { - if let test = suite.tests.first as? XCTestCase { - let testClass = type(of: test) - if testClass .isKind(of: EmbeddedTestBase.self) { - let injected = EmbeddedTestCaseSuite(for: testClass, tests: suite.tests) - embeddedSuite.addTest(injected) - } - } + + /// Test suite we build up by observing the normal test run. + /// We'll re-run this suite later, with the isRunning flag set to true, + let embeddedSuite = XCTestSuite(name: "Embedded Tests") + + /// Failures that occurred during the embedded test run. + var embeddedFailures: [(XCTestCase, String, String?, Int)] = [] + + /// Are we currently running the embedded tests? + var isRunningEmbedded = false + + /// Are the embedded tests currently running? + public static var isRunningEmbedded: Bool { _instance?.isRunningEmbedded ?? false } + + /// Install hooks into the testing system. + /// + /// We add ourselves as an observer of XCTest events, so that we can + /// collect information about the tests that are being run. + /// + /// Once the normal test run is complete, we re-run the tests that + /// we've collected, with our isRunning flag set to true, so that + /// the test bodies are actually executed. + public static func setUp(hostClass: TestHost.Type) { + if _instance == nil { + // turn off buffering on stdout so that we see the output immediately + setbuf(__stdoutp, nil) + + let observer = EmbeddingController(host: hostClass.init()) + _instance = observer + XCTestObservationCenter.shared.addTestObserver(observer) + } } - } - public func runEmbeddedTests() -> Int { - print( - """ + /// Record a test suite that has finished running. + func registerSuite(_ suite: XCTestSuite) { + if !isRunningEmbedded { + if let test = suite.tests.first as? EmbeddedTestBase { + let testClass = type(of: test) + let injected = EmbeddedTestCaseSuite(for: testClass, tests: suite.tests) + embeddedSuite.addTest(injected) + } + } + } - ---------------------------------------------------------- - Running embedded tests - ---------------------------------------------------------- + public func runEmbeddedTests() -> Int { + print( + """ - """) + ---------------------------------------------------------- + Running embedded tests + ---------------------------------------------------------- - isRunningEmbedded = true - embeddedSuite.run() - isRunningEmbedded = false + """) - print( - """ + isRunningEmbedded = true + embeddedSuite.run() + isRunningEmbedded = false - ---------------------------------------------------------- + print( + """ - Finished running embedded tests with \(embeddedFailures.count) failures. + ---------------------------------------------------------- - """) + Finished running embedded tests with \(embeddedFailures.count) failures. - return embeddedFailures.count - } + """) - public func testCase( - _ test: XCTestCase, didFailWithDescription description: String, inFile path: String?, - atLine line: Int - ) { - embeddedFailures.append((test, description, path, line)) - } + return embeddedFailures.count + } + + public func testCase( + _ test: XCTestCase, didFailWithDescription description: String, inFile path: String?, + atLine line: Int + ) { + embeddedFailures.append((test, description, path, line)) + } - public func testSuiteDidFinish(_ testSuite: XCTestSuite) { - if testSuite.className == "XCTestCaseSuite" { - registerSuite(testSuite) - // } else { - // print("SKIPPED \(testSuite) \(type(of: testSuite))") + public func testSuiteDidFinish(_ testSuite: XCTestSuite) { + if testSuite.className == "XCTestCaseSuite" { + registerSuite(testSuite) + // } else { + // print("SKIPPED \(testSuite) \(type(of: testSuite))") + } } - } - public func testBundleDidFinish(_ testBundle: Bundle) { - host.embedTests { - self.runEmbeddedTests() + public func testBundleDidFinish(_ testBundle: Bundle) { + host.embedTests { + self.runEmbeddedTests() + } } - } }