Skip to content

Commit

Permalink
Fixed an issue where isKind(of: failed to recognise embedded tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
samdeane committed Dec 18, 2024
1 parent 15d3696 commit c3c4bf1
Showing 1 changed file with 84 additions and 86 deletions.
170 changes: 84 additions & 86 deletions Sources/SwiftGodotTestability/EmbeddedXCTest/EmbeddingController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
}

0 comments on commit c3c4bf1

Please sign in to comment.