Skip to content

Commit

Permalink
Enable swift-testing by default and migrate all test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Jan 1, 2024
1 parent ac6fbe5 commit ca26ebe
Show file tree
Hide file tree
Showing 25 changed files with 287 additions and 341 deletions.
18 changes: 18 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@
"branch" : "main",
"revision" : "6835ce0972f40ef76f1214dc0efc75ec19082631"
}
},
{
"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" : {
"revision" : "56f3c5086bcb6c88680f48aeba7222617ed088d3",
"version" : "0.2.0"
}
}
],
"version" : 2
Expand Down
7 changes: 2 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ let package = Package(
.target(name: "CoreServices", path: "PrivateFrameworks/CoreServices"),
.target(name: "UIKitCore", path: "PrivateFrameworks/UIKitCore"),
openSwiftUITarget,
openSwiftUITestTarget,
openSwiftUICompatibilityTestTarget,
]
)
Expand Down Expand Up @@ -153,17 +152,15 @@ if swiftLogCondition {
}

// Remove the check when swift-testing reaches 1.0.0
let swiftTestingCondition = envEnable("OPENSWIFTUI_SWIFT_TESTING")
let swiftTestingCondition = envEnable("OPENSWIFTUI_SWIFT_TESTING", default: true)
if swiftTestingCondition {
package.dependencies.append(
.package(url: "https://github.com/apple/swift-testing", from: "0.2.0")
)
openSwiftUITestTarget.dependencies.append(
.product(name: "Testing", package: "swift-testing")
)
var swiftSettings: [SwiftSetting] = (openSwiftUITestTarget.swiftSettings ?? [])
swiftSettings.append(.define("OPENSWIFTUI_SWIFT_TESTING"))
openSwiftUITestTarget.swiftSettings = swiftSettings
package.targets.append(openSwiftUITestTarget)
}

let compatibilityTestCondition = envEnable("OPENSWIFTUI_COMPATIBILITY_TEST")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,23 @@
//

@testable import OpenSwiftUI
import XCTest
import Testing

final class AccessibilityBoundedNumberTests: XCTestCase {
func testBoundedNumberLocalizedDescription() throws {
if let boundedNumber = AccessibilityBoundedNumber(for: 4.5, in: 3.0...16.0, by: 0.1) {
XCTAssertEqual(boundedNumber.localizedDescription, "4.5") //decimal case
} else {
XCTFail("Failed to init bounded number")
}
if let boundedNumber = AccessibilityBoundedNumber(for: 4.5, in: 1.0...101.0, by: 0.1) {
XCTAssertEqual(boundedNumber.localizedDescription, "4%") // .percent case
} else {
XCTFail("Failed to init bounded number")
}
if let boundedNumber = AccessibilityBoundedNumber(for: 1.5, in: 1.3...2.3, by: 0.1) {
XCTAssertEqual(boundedNumber.localizedDescription, "1.5") // .decimal case
} else {
XCTFail("Failed to init bounded number")
}
if let boundedNumber = AccessibilityBoundedNumber(for: 1.5, in: 1.0...2.0, by: 0.1) {
XCTAssertEqual(boundedNumber.localizedDescription, "150%") // .percent case
} else {
XCTFail("Failed to init bounded number")
}
struct AccessibilityBoundedNumberTests {
@Test(arguments: [
(4.5, 3.0 ... 16.0, 0.1,"4.5"),
(4.5, 1.0 ... 101.0, 0.1,"4%"),
(1.5, 1.3 ... 2.3, 0.1,"1.5"),
(1.5, 1.0 ... 2.0, 0.1,"150%"),
])
func boundedNumberLocalizedDescription(
value: Double,
range: ClosedRange<Double>,
strideValue: Double,
expectedDescription: String
) throws {
let boundedNumber = try #require(AccessibilityBoundedNumber(for: value, in: range, by: strideValue))
#expect(boundedNumber.localizedDescription == expectedDescription)
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
//
// EnvironmentValuesOpenURLTests.swift
//
//
//
// Created by Kyle on 2023/11/28.
//

import XCTest
import Foundation
@testable import OpenSwiftUI
import Testing

final class EnvironmentValuesOpenURLTests: XCTestCase {
struct EnvironmentValuesOpenURLTests {
#if os(iOS) || os(macOS) || os(tvOS)
@Test
func testOpenURLActionKey() {
let value = OpenURLActionKey.defaultValue
value.callAsFunction(URL(string: "https://example.com")!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@
// Created by Kyle on 2023/10/17.
//

@testable import OpenSwiftUI
import Foundation
#if OPENSWIFTUI_SWIFT_TESTING
@testable import OpenSwiftUI
import Testing
#else
import XCTest
#endif

#if OPENSWIFTUI_SWIFT_TESTING
struct BloomFilterTests {
#if os(macOS)
@Test("Bloom Filter's init", .enabled(if: ProcessInfo.processInfo.operatingSystemVersionString == "14.0"))
Expand Down Expand Up @@ -54,43 +49,3 @@ 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
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
//
// MutableBoxTests.swift
//
//
//
// Created by Kyle on 2023/10/17.
//

import XCTest
@testable import OpenSwiftUI
import Testing

final class MutableBoxTests: XCTestCase {
func testExample() throws {
struct MutableBoxTests {
@Test
func wrappedValue() {
@MutableBox var box = 3
$box.wrappedValue = 4
XCTAssertEqual(box, 4)
#expect(box == 4)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
//

@testable import OpenSwiftUI
import XCTest
import Testing

final class ConstantLocationTests: XCTestCase {
func testConstantLocation() throws {
struct ConstantLocationTests {
@Test
func constantLocation() throws {
let location = ConstantLocation(value: 0)
XCTAssertEqual(location.wasRead, true)
XCTAssertEqual(location.get(), 0)
#expect(location.wasRead == true)
#expect(location.get() == 0)
location.wasRead = false
location.set(1, transaction: .init())
XCTAssertEqual(location.wasRead, true)
XCTAssertEqual(location.get(), 0)
#expect(location.wasRead == true)
#expect(location.get() == 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
//

@testable import OpenSwiftUI
import XCTest
import Testing

final class FunctionalLocationTests: XCTestCase {
func testFunctionalLocation() throws {
struct FunctionalLocationTests {
@Test
func functionalLocation() {
class V {
var count = 0
}
Expand All @@ -19,12 +20,11 @@ final class FunctionalLocationTests: XCTestCase {
} setValue: { newCount, _ in
value.count = newCount * newCount
}

XCTAssertEqual(location.wasRead, true)
XCTAssertEqual(location.get(), 0)
#expect(location.wasRead == true)
#expect(location.get() == 0)
location.wasRead = false
location.set(2, transaction: .init())
XCTAssertEqual(location.wasRead, true)
XCTAssertEqual(location.get(), 4)
#expect(location.wasRead == true)
#expect(location.get() == 4)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
// Created by Kyle on 2023/11/8.
//

import XCTest
@testable import OpenSwiftUI
import Testing

final class LocationBoxTests: XCTestCase {
func testBasicLocationBox() throws {
struct LocationBoxTests {
@Test
func basicLocationBox() throws {
class MockLocation: Location {
private var value: Int = 0
var wasRead: Bool = false
private var value = 0
var wasRead = false
typealias Value = Int
func get() -> Value { value }
func set(_ value: Int, transaction: Transaction) { self.value = value }
func set(_ value: Int, transaction _: Transaction) { self.value = value }
func update() -> (Int, Bool) {
defer { value += 1 }
return (value, value == 0)
Expand All @@ -25,41 +26,41 @@ final class LocationBoxTests: XCTestCase {
let location = MockLocation()
let box = LocationBox(location: location)

XCTAssertEqual(location.wasRead, false)
XCTAssertEqual(box.wasRead, false)
#expect(location.wasRead == false)
#expect(box.wasRead == false)
location.wasRead = true
XCTAssertEqual(location.wasRead, true)
XCTAssertEqual(box.wasRead, true)
#expect(location.wasRead == true)
#expect(box.wasRead == true)
box.wasRead = false
XCTAssertEqual(location.wasRead, false)
XCTAssertEqual(box.wasRead, false)
#expect(location.wasRead == false)
#expect(box.wasRead == false)

XCTAssertEqual(location.get(), 0)
XCTAssertEqual(box.get(), 0)
#expect(location.get() == 0)
#expect(box.get() == 0)
location.set(3, transaction: .init())
XCTAssertEqual(location.get(), 3)
XCTAssertEqual(box.get(), 3)
#expect(location.get() == 3)
#expect(box.get() == 3)
box.set(0, transaction: .init())
XCTAssertEqual(location.get(), 0)
XCTAssertEqual(box.get(), 0)
#expect(location.get() == 0)
#expect(box.get() == 0)

let (value, result) = box.update()
XCTAssertEqual(location.get(), 1)
XCTAssertEqual(value, 0)
XCTAssertEqual(result, true)
#expect((value, result) == (0, true))
#expect(location.get() == 1)
}

func testProjecting() {
@Test
func projecting() {
struct V {
var count = 0
}

class MockLocation: Location {
private var value = V()
var wasRead: Bool = false
var wasRead = false
typealias Value = V
func get() -> Value { value }
func set(_ value: Value, transaction: Transaction) { self.value = value }
func set(_ value: Value, transaction _: Transaction) { self.value = value }
func update() -> (Value, Bool) {
defer { value.count += 1 }
return (value, value.count == 0)
Expand All @@ -70,12 +71,12 @@ final class LocationBoxTests: XCTestCase {
let box = LocationBox(location: location)

let keyPath: WritableKeyPath = \V.count
XCTAssertEqual(box.cache.checkReference(for: keyPath, on: location), false)
#expect(box.cache.checkReference(for: keyPath, on: location) == false)
let newLocation = box.projecting(keyPath)
XCTAssertEqual(box.cache.checkReference(for: keyPath, on: location), true)
XCTAssertEqual(location.get().count, 0)
#expect(box.cache.checkReference(for: keyPath, on: location) == true)
#expect(location.get().count == 0)
_ = box.update()
XCTAssertEqual(location.get().count, 1)
XCTAssertEqual(newLocation.get(), 1)
#expect(location.get().count == 1)
#expect(newLocation.get() == 1)
}
}
Loading

0 comments on commit ca26ebe

Please sign in to comment.