<commit_before>import Foundation
import XCTest

/// Default handler for Nimble. This assertion handler passes failures along to
/// XCTest.
public class NimbleXCTestHandler : AssertionHandler {
    public func assert(_ assertion: Bool, message: FailureMessage, location: SourceLocation) {
        if !assertion {
            recordFailure("\(message.stringValue)\n", location: location)
        }
    }
}

/// Alternative handler for Nimble. This assertion handler passes failures along
/// to XCTest by attempting to reduce the failure message size.
public class NimbleShortXCTestHandler: AssertionHandler {
    public func assert(_ assertion: Bool, message: FailureMessage, location: SourceLocation) {
        if !assertion {
            let msg: String
            if let actual = message.actualValue {
                msg = "got: \(actual) \(message.postfixActual)"
            } else {
                msg = "expected \(message.to) \(message.postfixMessage)"
            }
            recordFailure("\(msg)\n", location: location)
        }
    }
}

/// Fallback handler in case XCTest is unavailable. This assertion handler will abort
/// the program if it is invoked.
class NimbleXCTestUnavailableHandler : AssertionHandler {
    func assert(_ assertion: Bool, message: FailureMessage, location: SourceLocation) {
        fatalError("XCTest is not available and no custom assertion handler was configured. Aborting.")
    }
}

#if _runtime(_ObjC)
    /// Helper class providing access to the currently executing XCTestCase instance, if any
@objc final internal class CurrentTestCaseTracker: NSObject, XCTestObservation {
    @objc static let sharedInstance = CurrentTestCaseTracker()

    private(set) var currentTestCase: XCTestCase?

    @objc func testCaseWillStart(testCase: XCTestCase) {
        currentTestCase = testCase
    }

    @objc func testCaseDidFinish(testCase: XCTestCase) {
        currentTestCase = nil
    }
}
#endif


func isXCTestAvailable() -> Bool {
#if _runtime(_ObjC)
    // XCTest is weakly linked and so may not be present
    return NSClassFromString("XCTestCase") != nil
#else
    return true
#endif
}

private func recordFailure(_ message: String, location: SourceLocation) {
#if _runtime(_ObjC)
    if let testCase = CurrentTestCaseTracker.sharedInstance.currentTestCase {
        testCase.recordFailure(withDescription: message, inFile: location.file, atLine: location.line, expected: true)
    } else {
        let msg = "Attempted to report a test failure to XCTest while no test case was running. " +
        "The failure was:\n\"\(message)\"\nIt occurred at: \(location.file):\(location.line)"
        NSException(name: NSInternalInconsistencyException, reason: msg, userInfo: nil).raise()
    }
#else
    XCTFail("\(message)\n", file: location.file, line: location.line)
#endif
}
<commit_msg>Fix `XCTestObservation` adaption of `CurrentTestCaseTracker`<commit_after>import Foundation
import XCTest

/// Default handler for Nimble. This assertion handler passes failures along to
/// XCTest.
public class NimbleXCTestHandler : AssertionHandler {
    public func assert(_ assertion: Bool, message: FailureMessage, location: SourceLocation) {
        if !assertion {
            recordFailure("\(message.stringValue)\n", location: location)
        }
    }
}

/// Alternative handler for Nimble. This assertion handler passes failures along
/// to XCTest by attempting to reduce the failure message size.
public class NimbleShortXCTestHandler: AssertionHandler {
    public func assert(_ assertion: Bool, message: FailureMessage, location: SourceLocation) {
        if !assertion {
            let msg: String
            if let actual = message.actualValue {
                msg = "got: \(actual) \(message.postfixActual)"
            } else {
                msg = "expected \(message.to) \(message.postfixMessage)"
            }
            recordFailure("\(msg)\n", location: location)
        }
    }
}

/// Fallback handler in case XCTest is unavailable. This assertion handler will abort
/// the program if it is invoked.
class NimbleXCTestUnavailableHandler : AssertionHandler {
    func assert(_ assertion: Bool, message: FailureMessage, location: SourceLocation) {
        fatalError("XCTest is not available and no custom assertion handler was configured. Aborting.")
    }
}

#if _runtime(_ObjC)
    /// Helper class providing access to the currently executing XCTestCase instance, if any
@objc final internal class CurrentTestCaseTracker: NSObject, XCTestObservation {
    @objc static let sharedInstance = CurrentTestCaseTracker()

    private(set) var currentTestCase: XCTestCase?

    @objc func testCaseWillStart(_ testCase: XCTestCase) {
        currentTestCase = testCase
    }

    @objc func testCaseDidFinish(_ testCase: XCTestCase) {
        currentTestCase = nil
    }
}
#endif


func isXCTestAvailable() -> Bool {
#if _runtime(_ObjC)
    // XCTest is weakly linked and so may not be present
    return NSClassFromString("XCTestCase") != nil
#else
    return true
#endif
}

private func recordFailure(_ message: String, location: SourceLocation) {
#if _runtime(_ObjC)
    if let testCase = CurrentTestCaseTracker.sharedInstance.currentTestCase {
        testCase.recordFailure(withDescription: message, inFile: location.file, atLine: location.line, expected: true)
    } else {
        let msg = "Attempted to report a test failure to XCTest while no test case was running. " +
        "The failure was:\n\"\(message)\"\nIt occurred at: \(location.file):\(location.line)"
        NSException(name: NSInternalInconsistencyException, reason: msg, userInfo: nil).raise()
    }
#else
    XCTFail("\(message)\n", file: location.file, line: location.line)
#endif
}

{'fraction_non_alphanumeric': 0.09187772925764193, 'fraction_numerical': 0.0, 'mean_word_length': 3.3543726235741445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3742264', 'n_tokens_mistral': 1584, 'n_tokens_neox': 1511, 'n_words': 508}