-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to SwiftSyntaxMacrosGenericTestSupport (#141)
Replaces SwiftSyntaxMacrosTestSupport with SwiftSyntaxMacrosGenericTestSupport and configures the failure handler to use Testing.Issue.record to avoid XCTFail.
- Loading branch information
Showing
25 changed files
with
333 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.