Files
purchases-ios/Tests/UnitTests/TestHelpers/CurrentTestCaseTracker.swift
T
05c8bec967 [AUTOMATIC] Release/5.1.0 (#4036)
**This is an automatic release.**

### New Features
* Paywalls with custom purchase and restore logic handlers (#3973) via
James Borthwick (@jamesrb1)
### Bugfixes
* Prevent paywall PurchaseHandler from being cleared on rerender (#4035)
via Josh Holtz (@joshdholtz)
* Update Purchase Tester for 5.0.0 (#4015) via Will Taylor
(@fire-at-will)
### Dependency Updates
* Bump fastlane from 2.221.0 to 2.221.1 (#3977) via dependabot[bot]
(@dependabot[bot])
### Other Changes
* Bring official `xcodes` back to CI (#4029) via Cesar de la Vega
(@vegaro)
* Paywalls tester with sandbox purchases (#4024) via James Borthwick
(@jamesrb1)
* Update v5 migration guide to contain current latest version (#4019)
via Toni Rico (@tonidero)
* CI Build Docs Improvements (#4014) via Will Taylor (@fire-at-will)
* Use available resource class for backend-integration-tests-offline-job
(#4013) via Will Taylor (@fire-at-will)
* Add `X-Preferred-Locales` header (#4008) via Cesar de la Vega
(@vegaro)

---------

Co-authored-by: Toni Rico <antonio.rico.diez@revenuecat.com>
Co-authored-by: Josh Holtz <me@joshholtz.com>
2024-07-16 16:52:35 -05:00

82 lines
2.3 KiB
Swift

//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// CurrentTestCaseTracker.swift
//
// Created by Nacho Soto on 12/22/21.
import XCTest
/// Helper class providing access to the currently executing XCTestCase instance, if any
final class CurrentTestCaseTracker: NSObject, XCTestObservation {
static let shared = CurrentTestCaseTracker()
private(set) var currentTestCase: XCTestCase?
@objc func testCaseWillStart(_ testCase: XCTestCase) {
currentTestCase = testCase
}
@objc func testCaseDidFinish(_ testCase: XCTestCase) {
currentTestCase = nil
}
var testInProgress: Bool {
return self.currentTestCase != nil
}
/// Extracts the name of the current running test.
///
/// Example: extracts `testLoginCachesForSameUserIDs`
/// from `-[BackendTests testLoginCachesForSameUserIDs]`
static var sanitizedTestName: String {
guard let test = Self.shared.currentTestCase else {
fatalError("No test currently running")
}
return test.sanitizedName
}
}
extension CurrentTestCaseTracker {
static var osVersionAndTestName: String {
#if os(macOS)
return "macOS/\(Self.sanitizedTestName)"
#elseif os(watchOS)
return "watchOS/\(Self.sanitizedTestName)"
#else
let osVersionEquivalent = OSVersionEquivalent.current
return "iOS\(osVersionEquivalent.rawValue)/\(Self.sanitizedTestName)"
#endif
}
}
private extension XCTestCase {
var sanitizedName: String {
let className = String(describing: type(of: self))
// swiftlint:disable:next force_try
let regex = try! NSRegularExpression(pattern: "-\\[\(className) (?<name>.*)\\]")
let range = NSRange(location: 0, length: self.name.utf8.count)
let nameRange = regex
.firstMatch(in: self.name, options: [], range: range)!
.range(withName: "name")
let start = self.name.index(name.startIndex, offsetBy: nameRange.location)
return String(self.name[start..<self.name.index(start, offsetBy: nameRange.length)])
}
}