Skip to content

Commit

Permalink
Fix tests on older Xcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ffried committed Sep 23, 2024
1 parent 5e5a1e8 commit 40fc541
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Tests/ColorComponentsTests/XCHelpers/AssertViewBody.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
import XCTest
import SwiftUI

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
@MainActor
func XCTAssertBody<V: View, Expected: View & Equatable>(
of view: V,
equals body: @autoclosure () throws -> Expected,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath, line: UInt = #line)
{
let viewBody = view.body
// Xcode as of version 16 erases all `some` return types to their `@_typeErasure` type (if any).
// There is a build setting for projects (SWIFT_ENABLE_OPAQUE_TYPE_ERASURE=NO), but that's not possible for SPM packages.
// However, we can "unpack" the any view for these builds. Worst case scenario: We don't check the view body.
// Kudos to Joe Groff for digging up the build setting: https://f.duriansoftware.com/@joe/113170928606701687
#if Xcode && swift(>=6.0)
let viewBody = view.body
XCTAssert(type(of: viewBody) == Expected.self || type(of: viewBody) == AnyView.self, "\(type(of: viewBody)) != \(Expected.self) || \(AnyView.self): \(message())",
file: file, line: line)
if let expectedTypeBody = viewBody as? Expected {
Expand All @@ -24,8 +25,8 @@ func XCTAssertBody<V: View, Expected: View & Equatable>(
XCTAssertEqual(actualView as? Expected, try body(), message(), file: file, line: line)
}
#else
XCTAssert(type(of: view.body) == Expected.self, "\(type(of: view.body)) != \(Expected.self): \(message())", file: file, line: line)
XCTAssertEqual(view.body as? Expected, try body(), message(), file: file, line: line)
XCTAssert(type(of: viewBody) == Expected.self, "\(type(of: viewBody)) != \(Expected.self): \(message())", file: file, line: line)
XCTAssertEqual(viewBody as? Expected, try body(), message(), file: file, line: line)
#endif
}
#endif

0 comments on commit 40fc541

Please sign in to comment.