From f27c3e8a6ec2bc67cba2ce74334acffe56f25e1b Mon Sep 17 00:00:00 2001 From: Kyle Date: Tue, 21 Nov 2023 11:06:03 +0800 Subject: [PATCH] Fix swift-testing build issue --- Package.resolved | 18 ------- Package.swift | 6 +-- .../Internal/BloomFilterTests.swift | 47 ++++++++++++++++++- .../ProtocolDescriptorTests.swift | 28 ++++++++++- 4 files changed, 74 insertions(+), 25 deletions(-) diff --git a/Package.resolved b/Package.resolved index ebca46b..16067fb 100644 --- a/Package.resolved +++ b/Package.resolved @@ -26,24 +26,6 @@ "revision" : "532d8b529501fb73a2455b179e0bbb6d49b652ed", "version" : "1.5.3" } - }, - { - "identity" : "swift-syntax", - "kind" : "remoteSourceControl", - "location" : "https://github.com/apple/swift-syntax.git", - "state" : { - "revision" : "6ad4ea24b01559dde0773e3d091f1b9e36175036", - "version" : "509.0.2" - } - }, - { - "identity" : "swift-testing", - "kind" : "remoteSourceControl", - "location" : "https://github.com/apple/swift-testing", - "state" : { - "branch" : "main", - "revision" : "88ee7b6e796f3af6254301d322b6f1be35048c44" - } } ], "version" : 2 diff --git a/Package.swift b/Package.swift index 301c4f2..11d52bf 100644 --- a/Package.swift +++ b/Package.swift @@ -72,7 +72,6 @@ if useAG { name: "AttributeGraphTests", dependencies: [ "AttributeGraph", - .product(name: "Testing", package: "swift-testing"), ] ), ] @@ -105,7 +104,6 @@ if useAG { name: "OpenGraphTests", dependencies: [ "OpenGraph", - .product(name: "Testing", package: "swift-testing"), ] ), ] @@ -144,8 +142,8 @@ if useOSLog { } // Remove this when swift-testing is 1.0.0 -let disableSwiftTesting = ProcessInfo.processInfo.environment["OPENSWIFTUI_DISABLE_SWIFT_TESTING"] != nil -if !disableSwiftTesting { +let useSwiftTesting = ProcessInfo.processInfo.environment["OPENSWIFTUI_USE_SWIFT_TESTING"] != nil +if useSwiftTesting { var swiftSettings: [SwiftSetting] = (openSwiftUITestTarget.swiftSettings ?? []) swiftSettings.append(.define("OPENSWIFTUI_USE_SWIFT_TESTING")) openSwiftUITestTarget.swiftSettings = swiftSettings diff --git a/Tests/OpenSwiftUITests/DataAndStorage/Internal/BloomFilterTests.swift b/Tests/OpenSwiftUITests/DataAndStorage/Internal/BloomFilterTests.swift index 0d65a5f..9610f39 100644 --- a/Tests/OpenSwiftUITests/DataAndStorage/Internal/BloomFilterTests.swift +++ b/Tests/OpenSwiftUITests/DataAndStorage/Internal/BloomFilterTests.swift @@ -5,11 +5,15 @@ // Created by Kyle on 2023/10/17. // -#if OPENSWIFTUI_USE_SWIFT_TESTING @testable import OpenSwiftUI -import Testing import Foundation +#if OPENSWIFTUI_USE_SWIFT_TESTING +import Testing +#else +import XCTest +#endif +#if OPENSWIFTUI_USE_SWIFT_TESTING struct BloomFilterTests { #if os(macOS) @Test("Bloom Filter's init", .enabled(if: ProcessInfo.processInfo.operatingSystemVersionString == "14.0")) @@ -50,4 +54,43 @@ struct BloomFilterTests { #endif } } +#else +final class BloomFilterTests: XCTestCase { + + func testInitType() throws { + #if os(macOS) + // hashValue: 0x1dd382138 + // 1 &<< (value &>> 0x10): 1 &<< 0x38 -> 0x0100_0000_0000_0000 + // 1 &<< (value &>> 0x0a): 1 &<< 0x08 -> 0x0000_0000_0000_0100 + // 1 &<< (value &>> 0x94): 1 &<< 0x13 -> 0x0000_0000_0008_0000 + try initTypeHelper(Int.self, expectedTypeValue: 0x1dd382138, expectedValue: 0x0100_0000_0008_0100, message: "macOS 14.0") + #elseif os(iOS) + try initTypeHelper(Int.self, expectedTypeValue: 0x1df10d1e0, expectedValue: 0x0010_0000_4001_0000, message: "iOS 15.5 Simulator") + #endif + } + + private func initTypeHelper(_ type: Any.Type, expectedTypeValue: Int, expectedValue: UInt, message: String = "") throws { + let typeValue = Int(bitPattern: unsafeBitCast(type, to: OpaquePointer.self)) + guard typeValue == expectedTypeValue else { + throw XCTSkip("The OS version is not covered. Please run it under \(message)") + } + XCTAssertEqual(BloomFilter(type: type).value, expectedValue) + } + + func testInitHashValue() throws { + // hashValue: 0 + // 1 &<< (value &>> 0x10): 1 &<< 0 -> 0x0000_0000_0000_0001 + // 1 &<< (value &>> 0x0a): 1 &<< 0 -> 0x0000_0000_0000_0001 + // 1 &<< (value &>> 0x04): 1 &<< 0 -> 0x0000_0000_0000_0001 + XCTAssertEqual(BloomFilter(hashValue: 0).value, 0x0000_0000_0000_0001) + + #if arch(x86_64) || arch(arm64) + // hashValue: 0x00000001dfa19ae0 + // 1 &<< (value &>> 0x10): 1 &<< 0x21 -> 0x0000_0002_0000_0000 + // 1 &<< (value &>> 0x0a): 1 &<< 0x26 -> 0x0000_0040_0000_0000 + // 1 &<< (value &>> 0x04): 1 &<< 0x2e -> 0x0000_4000_0000_0000 + XCTAssertEqual(BloomFilter(hashValue: 0x00000001dfa19ae0).value, 0x0000_4042_0000_0000) + #endif + } +} #endif diff --git a/Tests/OpenSwiftUITests/ProtocolDescriptorTests.swift b/Tests/OpenSwiftUITests/ProtocolDescriptorTests.swift index 85ae163..d751f4d 100644 --- a/Tests/OpenSwiftUITests/ProtocolDescriptorTests.swift +++ b/Tests/OpenSwiftUITests/ProtocolDescriptorTests.swift @@ -5,11 +5,15 @@ // Created by Kyle on 2023/10/3. // -#if OPENSWIFTUI_USE_SWIFT_TESTING @testable import OpenSwiftUI import OpenSwiftUIShims +#if OPENSWIFTUI_USE_SWIFT_TESTING import Testing +#else +import XCTest +#endif +#if OPENSWIFTUI_USE_SWIFT_TESTING struct ProtocolDescriptorTests { @Test func testExample() throws { @@ -31,4 +35,26 @@ struct ProtocolDescriptorTests { #expect(conformsToProtocol(ContentViewModifier.self, _viewModifierProtocolDescriptor())) } } +#else +final class ProtocolDescriptorTests: XCTestCase { + func testExample() throws { + struct ContentView: View { + var body: some View { + EmptyView() + } + } + + struct ContentViewModifier: ViewModifier { + func body(content _: Content) -> some View { + EmptyView() + } + } + + XCTAssertTrue(conformsToProtocol(ContentView.self, _viewProtocolDescriptor())) + XCTAssertFalse(conformsToProtocol(ContentView.self, _viewModifierProtocolDescriptor())) + + XCTAssertFalse(conformsToProtocol(ContentViewModifier.self, _viewProtocolDescriptor())) + XCTAssertTrue(conformsToProtocol(ContentViewModifier.self, _viewModifierProtocolDescriptor())) + } +} #endif