Skip to content

Commit

Permalink
Replace all uses of XCTest (#139)
Browse files Browse the repository at this point in the history
Replaces most uses of XCTest with with Swift-Testing. One last migration
to `SwiftSyntaxMacrosGenericTestSupport` is needed to fully remove
XCTest.,
  • Loading branch information
rauhul authored Dec 15, 2024
1 parent 0584c0d commit 13d1abe
Show file tree
Hide file tree
Showing 49 changed files with 1,376 additions and 1,570 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
lint:
name: Lint
runs-on: ubuntu-latest
container: swift:6.0-jammy
container: swiftlang/swift:nightly-main
steps:
- uses: actions/checkout@v4
- name: Install Dependencies
Expand Down
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

CONFIGURATION = debug
SWIFT_FORMAT_CONFIGURATION := SupportingFiles/Tools/swift-format/.swift-format
SKIP_LINT =

.PHONY: all lint format build test clean
all: test
Expand Down
5 changes: 1 addition & 4 deletions Tests/MMIOFileCheckTests/MMIOFileCheckTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
//
//===----------------------------------------------------------------------===//

// FIXME: switch over to swift-testing
// XCTest is really painful for dynamic test lists

import Dispatch
import Foundation
import MMIOUtilities
Expand Down Expand Up @@ -87,7 +84,7 @@ struct MMIOFileCheckTests: @unchecked Sendable {
plutil \
-extract CFBundleIdentifier raw \
-o - \
/Library/Developer/Toolchains/swift-latest.xctoolchain/Info.plist
~/Library/Developer/Toolchains/swift-latest.xctoolchain/Info.plist
""")
} catch {
print("Failed to locate toolchain by plist: \(error)")
Expand Down
12 changes: 6 additions & 6 deletions Tests/MMIOInterposableTests/MMIOInterposableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
//
//===----------------------------------------------------------------------===//

import XCTest
import Testing

@testable import MMIOInterposable

final class MMIOInterposableTests: XCTestCase {
struct MMIOInterposableTests {
@RegisterBlock
struct Example {
@RegisterBlock(offset: 0x0)
Expand All @@ -40,13 +40,13 @@ final class MMIOInterposableTests: XCTestCase {
var reserved0: Reserved0
}

func test_registerBlock_passesInterposerToChildren() {
@Test func registerBlock_passesInterposerToChildren() {
let interposer = MMIOTracingInterposer()
let example = Example(unsafeAddress: 0x1000, interposer: interposer)
XCTAssertTrue(example.interposer === example.regA.interposer)
#expect(example.interposer === example.regA.interposer)
}

func test_tracingInterposer_producesExpectedTrace() {
@Test func tracingInterposer_producesExpectedTrace() {
let interposer = MMIOTracingInterposer()
let example = Example(unsafeAddress: 0x1000, interposer: interposer)
_ = example.regA.read()
Expand All @@ -60,7 +60,7 @@ final class MMIOInterposableTests: XCTestCase {
example.regB.modify { r, w in
w.rst = false
}
XCTAssertMMIOInterposerTrace(
assertMMIOInterposerTrace(
interposer: interposer,
trace: [
.load(of: UInt32(0), from: 0x1000),
Expand Down
43 changes: 19 additions & 24 deletions Tests/MMIOInterposableTests/MMIOTracingInterposer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import MMIOInterposable
import MMIOUtilities
import XCTest
import Testing

class MMIOTracingInterposer {
// This could be made more efficient by mapping to 8 byte blocks.
Expand All @@ -29,7 +29,7 @@ extension MMIOTracingInterposer: MMIOInterposer {
func load<Value>(
from pointer: UnsafePointer<Value>
) -> Value where Value: FixedWidthInteger & UnsignedInteger {
XCTAssertMMIOAlignment(pointer: pointer)
assertMMIOAlignment(pointer: pointer)

let address = UInt(bitPattern: pointer)
let size = MemoryLayout<Value>.size
Expand All @@ -50,7 +50,7 @@ extension MMIOTracingInterposer: MMIOInterposer {
_ value: Value,
to pointer: UnsafeMutablePointer<Value>
) where Value: FixedWidthInteger & UnsignedInteger {
XCTAssertMMIOAlignment(pointer: pointer)
assertMMIOAlignment(pointer: pointer)

let address = UInt(bitPattern: pointer)
let size = MemoryLayout<Value>.size
Expand All @@ -65,39 +65,34 @@ extension MMIOTracingInterposer: MMIOInterposer {
}
}

// swift-format-ignore: AlwaysUseLowerCamelCase
func XCTAssertMMIOAlignment<Value>(
func assertMMIOAlignment<Value>(
pointer: UnsafePointer<Value>,
file: StaticString = #filePath,
line: UInt = #line
sourceLocation: SourceLocation = #_sourceLocation
) where Value: FixedWidthInteger & UnsignedInteger {
let address = UInt(bitPattern: pointer)
let alignment = UInt(MemoryLayout<Value>.alignment)
if !address.isMultiple(of: alignment) {
XCTFail(
"""
Invalid load or store of type '\(Value.self)' from unaligned address \
'\(hex: UInt(bitPattern: pointer))'
""",
file: file,
line: line)
}
#expect(
address.isMultiple(of: alignment),
"""
Invalid load or store of type '\(Value.self)' from unaligned address \
'\(hex: address)'
""",
sourceLocation: sourceLocation)
}

// swift-format-ignore: AlwaysUseLowerCamelCase
func XCTAssertMMIOInterposerTrace(
func assertMMIOInterposerTrace(
interposer: MMIOTracingInterposer,
trace: [MMIOTracingInterposerEvent],
file: StaticString = #filePath,
line: UInt = #line
sourceLocation: SourceLocation = #_sourceLocation
) {
// Exit early if the actual trace matches the expected trace.
let actualTrace = interposer.trace
let expectedTrace = trace
guard actualTrace != expectedTrace else { return }

XCTFail(
diff(expected: expectedTrace, actual: actualTrace, noun: "trace"),
file: file,
line: line)
Issue.record(
Comment(
rawValue: diff(
expected: expectedTrace, actual: actualTrace, noun: "trace")),
sourceLocation: sourceLocation)
}
114 changes: 61 additions & 53 deletions Tests/MMIOInterposableTests/MMIOTracingInterposerEventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,75 @@
//
//===----------------------------------------------------------------------===//

import XCTest
import Testing

final class MMIOTracingInterposerEventTests: XCTestCase {
func test_load() {
XCTAssertEqual(
MMIOTracingInterposerEvent.load(of: UInt8(1), from: 0x10),
MMIOTracingInterposerEvent(load: true, address: 0x10, size: 8, value: 1))
XCTAssertEqual(
MMIOTracingInterposerEvent.load(of: UInt16(2), from: 0x10),
MMIOTracingInterposerEvent(load: true, address: 0x10, size: 16, value: 2))
XCTAssertEqual(
MMIOTracingInterposerEvent.load(of: UInt32(3), from: 0x10),
MMIOTracingInterposerEvent(load: true, address: 0x10, size: 32, value: 3))
XCTAssertEqual(
MMIOTracingInterposerEvent.load(of: UInt64(4), from: 0x10),
MMIOTracingInterposerEvent(load: true, address: 0x10, size: 64, value: 4))
struct MMIOTracingInterposerEventTests {
@Test func load() {
#expect(
MMIOTracingInterposerEvent.load(of: UInt8(1), from: 0x10)
== MMIOTracingInterposerEvent(
load: true, address: 0x10, size: 8, value: 1))
#expect(
MMIOTracingInterposerEvent.load(of: UInt16(2), from: 0x10)
== MMIOTracingInterposerEvent(
load: true, address: 0x10, size: 16, value: 2))
#expect(
MMIOTracingInterposerEvent.load(of: UInt32(3), from: 0x10)
== MMIOTracingInterposerEvent(
load: true, address: 0x10, size: 32, value: 3))
#expect(
MMIOTracingInterposerEvent.load(of: UInt64(4), from: 0x10)
== MMIOTracingInterposerEvent(
load: true, address: 0x10, size: 64, value: 4))
}

func test_store() {
XCTAssertEqual(
MMIOTracingInterposerEvent.store(of: UInt8(1), to: 0x10),
MMIOTracingInterposerEvent(load: false, address: 0x10, size: 8, value: 1))
XCTAssertEqual(
MMIOTracingInterposerEvent.store(of: UInt16(2), to: 0x10),
MMIOTracingInterposerEvent(load: false, address: 0x10, size: 16, value: 2)
@Test func store() {
#expect(
MMIOTracingInterposerEvent.store(of: UInt8(1), to: 0x10)
== MMIOTracingInterposerEvent(
load: false, address: 0x10, size: 8, value: 1))
#expect(
MMIOTracingInterposerEvent.store(of: UInt16(2), to: 0x10)
== MMIOTracingInterposerEvent(
load: false, address: 0x10, size: 16, value: 2)
)
XCTAssertEqual(
MMIOTracingInterposerEvent.store(of: UInt32(3), to: 0x10),
MMIOTracingInterposerEvent(load: false, address: 0x10, size: 32, value: 3)
#expect(
MMIOTracingInterposerEvent.store(of: UInt32(3), to: 0x10)
== MMIOTracingInterposerEvent(
load: false, address: 0x10, size: 32, value: 3)
)
XCTAssertEqual(
MMIOTracingInterposerEvent.store(of: UInt64(4), to: 0x10),
MMIOTracingInterposerEvent(load: false, address: 0x10, size: 64, value: 4)
#expect(
MMIOTracingInterposerEvent.store(of: UInt64(4), to: 0x10)
== MMIOTracingInterposerEvent(
load: false, address: 0x10, size: 64, value: 4)
)
}

func test_description() {
XCTAssertEqual(
MMIOTracingInterposerEvent.load(of: UInt8(1), from: 0x10).description,
"m[0x0000_0000_0000_0010] -> 0x01")
XCTAssertEqual(
MMIOTracingInterposerEvent.load(of: UInt16(2), from: 0x10).description,
"m[0x0000_0000_0000_0010] -> 0x0002")
XCTAssertEqual(
MMIOTracingInterposerEvent.load(of: UInt32(3), from: 0x10).description,
"m[0x0000_0000_0000_0010] -> 0x0000_0003")
XCTAssertEqual(
MMIOTracingInterposerEvent.load(of: UInt64(4), from: 0x10).description,
"m[0x0000_0000_0000_0010] -> 0x0000_0000_0000_0004")
@Test func description() {
#expect(
MMIOTracingInterposerEvent.load(of: UInt8(1), from: 0x10).description
== "m[0x0000_0000_0000_0010] -> 0x01")
#expect(
MMIOTracingInterposerEvent.load(of: UInt16(2), from: 0x10).description
== "m[0x0000_0000_0000_0010] -> 0x0002")
#expect(
MMIOTracingInterposerEvent.load(of: UInt32(3), from: 0x10).description
== "m[0x0000_0000_0000_0010] -> 0x0000_0003")
#expect(
MMIOTracingInterposerEvent.load(of: UInt64(4), from: 0x10).description
== "m[0x0000_0000_0000_0010] -> 0x0000_0000_0000_0004")

XCTAssertEqual(
MMIOTracingInterposerEvent.store(of: UInt8(1), to: 0x10).description,
"m[0x0000_0000_0000_0010] <- 0x01")
XCTAssertEqual(
MMIOTracingInterposerEvent.store(of: UInt16(2), to: 0x10).description,
"m[0x0000_0000_0000_0010] <- 0x0002")
XCTAssertEqual(
MMIOTracingInterposerEvent.store(of: UInt32(3), to: 0x10).description,
"m[0x0000_0000_0000_0010] <- 0x0000_0003")
XCTAssertEqual(
MMIOTracingInterposerEvent.store(of: UInt64(4), to: 0x10).description,
"m[0x0000_0000_0000_0010] <- 0x0000_0000_0000_0004")
#expect(
MMIOTracingInterposerEvent.store(of: UInt8(1), to: 0x10).description
== "m[0x0000_0000_0000_0010] <- 0x01")
#expect(
MMIOTracingInterposerEvent.store(of: UInt16(2), to: 0x10).description
== "m[0x0000_0000_0000_0010] <- 0x0002")
#expect(
MMIOTracingInterposerEvent.store(of: UInt32(3), to: 0x10).description
== "m[0x0000_0000_0000_0010] <- 0x0000_0003")
#expect(
MMIOTracingInterposerEvent.store(of: UInt64(4), to: 0x10).description
== "m[0x0000_0000_0000_0010] <- 0x0000_0000_0000_0004")
}
}
62 changes: 30 additions & 32 deletions Tests/MMIOInterposableTests/MMIOTracingInterposerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,76 +9,74 @@
//
//===----------------------------------------------------------------------===//

import XCTest
import Testing

final class MMIOTracingInterposerTests: XCTestCase {
func test_memory_load() {
struct MMIOTracingInterposerTests {
@Test func memory_load() {
let interposer = MMIOTracingInterposer()
interposer.memory[0x10] = 0x5a
interposer.memory[0x11] = 0xa5
let value: UInt16 = interposer.load(from: .init(bitPattern: 0x10)!)
XCTAssertEqual(value, 0xa55a)
#expect(value == 0xa55a)

XCTAssertEqual(
interposer.trace,
[
assertMMIOInterposerTrace(
interposer: interposer,
trace: [
.load(of: UInt16(0xa55a), from: 0x10)
])
}

func test_memory_store() {
@Test func memory_store() {
let interposer = MMIOTracingInterposer()
interposer.store(
UInt16(0xa55a),
to: .init(bitPattern: 0x10)!)
XCTAssertEqual(interposer.memory[0x10], 0x5a)
XCTAssertEqual(interposer.memory[0x11], 0xa5)
XCTAssertEqual(
interposer.trace,
[
#expect(interposer.memory[0x10] == 0x5a)
#expect(interposer.memory[0x11] == 0xa5)
assertMMIOInterposerTrace(
interposer: interposer,
trace: [
.store(of: UInt16(0xa55a), to: 0x10)
])
}

func test_XCTAssertMMIOAlignment_pass() {
XCTAssertMMIOAlignment(pointer: UnsafePointer<UInt16>(bitPattern: 0x2)!)
@Test func assertMMIOAlignment_pass() {
assertMMIOAlignment(pointer: UnsafePointer<UInt16>(bitPattern: 0x2)!)
}

func test_XCTAssertMMIOAlignment_fail() {
#if !os(Linux)
XCTExpectFailure("testing negative case")
XCTAssertMMIOAlignment(pointer: UnsafePointer<UInt16>(bitPattern: 0x1)!)
#endif
@Test func assertMMIOAlignment_fail() {
withKnownIssue("testing negative case") {
assertMMIOAlignment(pointer: UnsafePointer<UInt16>(bitPattern: 0x1)!)
}
}

func test_XCTAssertMMIOInterposerTrace_pass() {
@Test func assertMMIOInterposerTrace_pass() {
let interposer = MMIOTracingInterposer()
interposer.trace = [
.store(of: UInt8(0xa5), to: 0x10),
.load(of: UInt8(0x5a), from: 0x20),
]
XCTAssertMMIOInterposerTrace(
assertMMIOInterposerTrace(
interposer: interposer,
trace: [
.store(of: UInt8(0xa5), to: 0x10),
.load(of: UInt8(0x5a), from: 0x20),
])
}

func test_XCTAssertMMIOInterposerTrace_fail() {
#if !os(Linux)
XCTExpectFailure("testing negative case")
@Test func assertMMIOInterposerTrace_fail() {
let interposer = MMIOTracingInterposer()
interposer.trace = [
.store(of: UInt8(0xa5), to: 0x10),
.load(of: UInt8(0x5a), from: 0x20),
]
XCTAssertMMIOInterposerTrace(
interposer: interposer,
trace: [
.load(of: UInt8(0x5a), from: 0x20),
.load(of: UInt8(0xa6), from: 0x30),
])
#endif
withKnownIssue("testing negative case") {
assertMMIOInterposerTrace(
interposer: interposer,
trace: [
.load(of: UInt8(0x5a), from: 0x20),
.load(of: UInt8(0xa6), from: 0x30),
])
}
}
}
Loading

0 comments on commit 13d1abe

Please sign in to comment.