Skip to content

Commit

Permalink
Switch to SwiftSyntaxMacrosGenericTestSupport (#141)
Browse files Browse the repository at this point in the history
Replaces SwiftSyntaxMacrosTestSupport with
SwiftSyntaxMacrosGenericTestSupport and configures the failure handler
to use Testing.Issue.record to avoid XCTFail.
  • Loading branch information
rauhul authored Dec 16, 2024
1 parent b676a4f commit e8f0806
Show file tree
Hide file tree
Showing 25 changed files with 333 additions and 184 deletions.
6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ let package = Package(
"MMIOUtilities",
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
.product(
name: "SwiftSyntaxMacrosGenericTestSupport", package: "swift-syntax"),
]),

.target(name: "MMIOUtilities"),
Expand Down Expand Up @@ -149,7 +150,8 @@ let package = Package(
"SVDMacros",
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"),
.product(
name: "SwiftSyntaxMacrosGenericTestSupport", package: "swift-syntax"),
]),
],
cxxLanguageStandard: .cxx11)
Expand Down
45 changes: 45 additions & 0 deletions Tests/MMIOInterposableTests/Assertions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift MMIO open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import MMIOUtilities
import Testing

func assertMMIOAlignment<Value>(
pointer: UnsafePointer<Value>,
sourceLocation: SourceLocation = #_sourceLocation
) where Value: FixedWidthInteger & UnsignedInteger {
let address = UInt(bitPattern: pointer)
let alignment = UInt(MemoryLayout<Value>.alignment)
#expect(
address.isMultiple(of: alignment),
"""
Invalid load or store of type '\(Value.self)' from unaligned address \
'\(hex: address)'
""",
sourceLocation: sourceLocation)
}

func assertMMIOInterposerTrace(
interposer: MMIOTracingInterposer,
trace: [MMIOTracingInterposerEvent],
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 }

Issue.record(
Comment(
rawValue: diff(
expected: expectedTrace, actual: actualTrace, noun: "trace")),
sourceLocation: sourceLocation)
}
33 changes: 0 additions & 33 deletions Tests/MMIOInterposableTests/MMIOTracingInterposer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
//===----------------------------------------------------------------------===//

import MMIOInterposable
import MMIOUtilities
import Testing

class MMIOTracingInterposer {
Expand Down Expand Up @@ -64,35 +63,3 @@ extension MMIOTracingInterposer: MMIOInterposer {
self.trace.append(.store(of: value, to: address))
}
}

func assertMMIOAlignment<Value>(
pointer: UnsafePointer<Value>,
sourceLocation: SourceLocation = #_sourceLocation
) where Value: FixedWidthInteger & UnsignedInteger {
let address = UInt(bitPattern: pointer)
let alignment = UInt(MemoryLayout<Value>.alignment)
#expect(
address.isMultiple(of: alignment),
"""
Invalid load or store of type '\(Value.self)' from unaligned address \
'\(hex: address)'
""",
sourceLocation: sourceLocation)
}

func assertMMIOInterposerTrace(
interposer: MMIOTracingInterposer,
trace: [MMIOTracingInterposerEvent],
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 }

Issue.record(
Comment(
rawValue: diff(
expected: expectedTrace, actual: actualTrace, noun: "trace")),
sourceLocation: sourceLocation)
}
99 changes: 99 additions & 0 deletions Tests/MMIOMacrosTests/Assertions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift MMIO open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

#if canImport(MMIOMacros)
import SwiftSyntax
import SwiftSyntaxMacroExpansion
import SwiftSyntaxMacrosGenericTestSupport
import SwiftSyntaxMacros
import Testing

@testable import MMIOMacros

func assertParse<Value>(
expression: ExprSyntax,
expected: Value,
sourceLocation: Testing.SourceLocation = #_sourceLocation
) where Value: ExpressibleByExprSyntax, Value: Equatable {
#expect(throws: Never.self, sourceLocation: sourceLocation) {
let context = MacroContext.makeSuppressingDiagnostics(Macro0.self)
let actual = try Value(expression: expression, in: context)
#expect(actual == expected, sourceLocation: sourceLocation)
}
}

func assertParseBitFieldTypeProjection(
expression: ExprSyntax,
sourceLocation: Testing.SourceLocation = #_sourceLocation
) {
// swift-format-ignore: NeverForceUnwrap
let base = expression.as(MemberAccessExprSyntax.self)!.base!
assertParse(
expression: expression,
expected: BitFieldTypeProjection(expression: base),
sourceLocation: sourceLocation)
}

func assertNoParse<Value>(
expression: ExprSyntax,
as _: Value.Type,
sourceLocation: Testing.SourceLocation = #_sourceLocation
) where Value: ExpressibleByExprSyntax {
#expect(throws: ExpansionError.self, sourceLocation: sourceLocation) {
let context = MacroContext.makeSuppressingDiagnostics(Macro0.self)
_ = try Value(expression: expression, in: context)
}
}

// Shim "assertMacroExpansion" to use swift-testing
typealias NoteSpec = SwiftSyntaxMacrosGenericTestSupport.NoteSpec
typealias FixItSpec = SwiftSyntaxMacrosGenericTestSupport.FixItSpec
typealias DiagnosticSpec = SwiftSyntaxMacrosGenericTestSupport.DiagnosticSpec

func assertMacroExpansion(
_ originalSource: String,
expandedSource expectedExpandedSource: String,
diagnostics: [DiagnosticSpec] = [],
macros: [String: Macro.Type],
applyFixIts: [String]? = nil,
fixedSource expectedFixedSource: String? = nil,
testModuleName: String = "TestModule",
testFileName: String = "test.swift",
indentationWidth: Trivia = .spaces(4),
sourceLocation: Testing.SourceLocation = #_sourceLocation,
fileID: StaticString = #fileID,
filePath: StaticString = #filePath
) {
SwiftSyntaxMacrosGenericTestSupport.assertMacroExpansion(
originalSource,
expandedSource: expectedExpandedSource,
diagnostics: diagnostics,
macroSpecs: macros.mapValues { MacroSpec(type: $0) },
applyFixIts: applyFixIts,
fixedSource: expectedFixedSource,
testModuleName: testModuleName,
testFileName: testFileName,
indentationWidth: indentationWidth,
failureHandler: {
Issue.record(
Comment(rawValue: $0.message),
sourceLocation: .init(
fileID: $0.location.fileID,
filePath: $0.location.filePath,
line: $0.location.line,
column: $0.location.column))
},
fileID: fileID,
filePath: filePath,
line: UInt(sourceLocation.line),
column: UInt(sourceLocation.column))
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,6 @@ import Testing

@testable import MMIOMacros

func assertParse<Value>(
expression: ExprSyntax,
expected: Value,
sourceLocation: Testing.SourceLocation = #_sourceLocation
) where Value: ExpressibleByExprSyntax, Value: Equatable {
#expect(throws: Never.self, sourceLocation: sourceLocation) {
let context = MacroContext.makeSuppressingDiagnostics(Macro0.self)
let actual = try Value(expression: expression, in: context)
#expect(actual == expected, sourceLocation: sourceLocation)
}
}

func assertParseBitFieldTypeProjection(
expression: ExprSyntax,
sourceLocation: Testing.SourceLocation = #_sourceLocation
) {
// swift-format-ignore: NeverForceUnwrap
let base = expression.as(MemberAccessExprSyntax.self)!.base!
assertParse(
expression: expression,
expected: BitFieldTypeProjection(expression: base),
sourceLocation: sourceLocation)
}

func assertNoParse<Value>(
expression: ExprSyntax,
as _: Value.Type,
sourceLocation: Testing.SourceLocation = #_sourceLocation
) where Value: ExpressibleByExprSyntax {
#expect(throws: ExpansionError.self, sourceLocation: sourceLocation) {
let context = MacroContext.makeSuppressingDiagnostics(Macro0.self)
_ = try Value(expression: expression, in: context)
}
}

struct ExpressibleByExprSyntaxTests {
@Test func exprSyntax() throws {
let expression: ExprSyntax = "Bool.self"
Expand Down
1 change: 0 additions & 1 deletion Tests/MMIOMacrosTests/Macros/BitFieldMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#if canImport(MMIOMacros)
import SwiftSyntax
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import Testing

@testable import MMIOMacros
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#if canImport(MMIOMacros)
import SwiftSyntax
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import Testing

@testable import MMIOMacros
Expand Down
1 change: 0 additions & 1 deletion Tests/MMIOMacrosTests/Macros/RegisterBlockMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#if canImport(MMIOMacros)
import SwiftSyntax
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import Testing

@testable import MMIOMacros
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#if canImport(MMIOMacros)
import SwiftSyntax
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import Testing

@testable import MMIOMacros
Expand Down
1 change: 0 additions & 1 deletion Tests/MMIOMacrosTests/Macros/RegisterMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#if canImport(MMIOMacros)
import SwiftSyntax
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import Testing

@testable import MMIOMacros
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#if canImport(MMIOMacros)
import SwiftSyntax
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import Testing

@testable import MMIOMacros
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import SwiftSyntax
import SwiftSyntaxMacroExpansion
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import Testing

@testable import MMIOMacros
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import SwiftSyntax
import SwiftSyntaxMacroExpansion
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import Testing

@testable import MMIOMacros
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import SwiftSyntax
import SwiftSyntaxMacroExpansion
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import Testing

@testable import MMIOMacros
Expand Down
58 changes: 58 additions & 0 deletions Tests/MMIOTests/Assertions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift MMIO open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import Testing

@testable import MMIO

func assertExtract<Storage>(
bitRanges: Range<Int>...,
from storage: Storage,
equals expected: Storage,
sourceLocation: SourceLocation = #_sourceLocation
) where Storage: FixedWidthInteger {
let actual = storage[bits: bitRanges]
#expect(
actual == expected,
"""
Extracting value \
from '\(hex: storage)' \
at bit ranges \(bitRanges
.map { "\($0.lowerBound)..<\($0.upperBound)" }
.joined(separator: ", "))] \
resulted in '\(hex: actual)', \
but expected '\(hex: expected)'
""",
sourceLocation: sourceLocation)
}

func assertInsert<Storage>(
value: Storage,
bitRanges: Range<Int>...,
into storage: Storage,
equals expected: Storage,
sourceLocation: SourceLocation = #_sourceLocation
) where Storage: FixedWidthInteger {
var actual = storage
actual[bits: bitRanges] = value
#expect(
actual == expected,
"""
Inserting '\(hex: value)' \
into '\(hex: storage)' \
at bit ranges [\(bitRanges
.map { "\($0.lowerBound)..<\($0.upperBound)" }
.joined(separator: ", "))] \
resulted in '\(hex: actual)', \
but expected to get '\(hex: expected)'
""",
sourceLocation: sourceLocation)
}
Loading

0 comments on commit e8f0806

Please sign in to comment.