Skip to content

Commit

Permalink
Fix swift-testing build issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Nov 21, 2023
1 parent 166ee6d commit f27c3e8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 25 deletions.
18 changes: 0 additions & 18 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ if useAG {
name: "AttributeGraphTests",
dependencies: [
"AttributeGraph",
.product(name: "Testing", package: "swift-testing"),
]
),
]
Expand Down Expand Up @@ -105,7 +104,6 @@ if useAG {
name: "OpenGraphTests",
dependencies: [
"OpenGraph",
.product(name: "Testing", package: "swift-testing"),
]
),
]
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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
28 changes: 27 additions & 1 deletion Tests/OpenSwiftUITests/ProtocolDescriptorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

0 comments on commit f27c3e8

Please sign in to comment.