Skip to content

Commit 1cf78fd

Browse files
committed
Added documentation for the Swift Testing support.
1 parent 9c9fa41 commit 1cf78fd

7 files changed

Lines changed: 38 additions & 23 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
Version 2.3
2+
-----------
3+
4+
_16 Sep 2024_
5+
6+
- Added Swift Testing support.
7+
- Migrated to Swift 6
8+
19
Version 2.2.4
210
-------------
11+
312
_13 Mar 2023_
413

514
- Added compatibilty with Xcode 13. Issue #53

Package.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// swift-tools-version:6.0
22

3-
import PackageDescription
43
import CompilerPluginSupport
4+
import PackageDescription
55

66
let package = Package(
77
name: "Hamcrest",
@@ -19,7 +19,6 @@ let package = Package(
1919
dependencies: [],
2020
path: "Hamcrest"),
2121

22-
2322
.testTarget(
2423
name: "HamcrestTests",
2524
dependencies: [

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,30 @@ Integration
369369

370370
Select the 'Add Package Dependency' option in your Xcode project and copy this repository's URL into the 'Choose Package Repository' window.
371371

372+
### Swift-Testing ###
373+
374+
Hamcrest also works with the new Swift-Testing framework. Migration to Swift-Testing is quite easy, because there `#assertThat` macro.
375+
The macro is in the module `HamcrestSwiftTesting` so this needs to imported.
376+
377+
e.g.
378+
379+
```
380+
381+
import Hamcrest
382+
import HamcrestSwiftTesting
383+
import Testing
384+
385+
struct ExampleTest {
386+
387+
@Test func test_assertThat() async throws {
388+
#assertThat("foo", equalTo("foo"))
389+
#assertThat("foo", not(equalTo("bar")))
390+
}
391+
}
392+
393+
```
394+
395+
372396
### CocoaPods ###
373397

374398
Integrate SwiftHamcrest using a Podfile similar to this:

SwiftTesting/Source/Macros/AssertThatMacro.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
//
77

88
import Foundation
9+
import SwiftCompilerPlugin
910
import SwiftSyntax
1011
import SwiftSyntaxMacros
11-
import SwiftCompilerPlugin
1212

1313
public struct AssertThatMacro: ExpressionMacro, Sendable {
14-
1514
public static func expansion(of node: some SwiftSyntax.FreestandingMacroExpansionSyntax, in context: some SwiftSyntaxMacros.MacroExpansionContext) throws -> SwiftSyntax.ExprSyntax {
16-
1715
let arguments = node.arguments
1816
guard arguments.count == 2 else {
1917
fatalError("the macro does not have proper arguments")
@@ -28,8 +26,6 @@ public struct AssertThatMacro: ExpressionMacro, Sendable {
2826
}
2927
return "checkMatcher(\(firstArgument), \(secondArgument), comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()"
3028
}
31-
32-
3329
}
3430

3531
@main
@@ -39,5 +35,4 @@ struct HamcrestMacroPlugin: CompilerPlugin {
3935
public let providingMacros: [Macro.Type] = [
4036
AssertThatMacro.self
4137
]
42-
4338
}

SwiftTesting/Source/Main/SwiftTesting.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
// Created by René Pirringer on 13.09.24.
66
//
77
import Foundation
8-
import Testing
98
import Hamcrest
109
import SwiftSyntax
1110
import SwiftSyntaxMacros
12-
11+
import Testing
1312

1413
@_disfavoredOverload public func checkMatcher<T>(
1514
_ value: @autoclosure () throws -> T,
@@ -18,8 +17,6 @@ import SwiftSyntaxMacros
1817
isRequired: Bool,
1918
sourceLocation: Testing.SourceLocation
2019
) -> Result<Void, any Error> {
21-
22-
2320
if let message = applyMatcher(matcher, toValue: value) {
2421
let expression = Testing.__Expression.__fromSyntaxNode(message)
2522
return __checkValue(
@@ -40,5 +37,3 @@ import SwiftSyntaxMacros
4037
_ comment: @autoclosure () -> Comment? = nil,
4138
sourceLocation: Testing.SourceLocation = #_sourceLocation
4239
) = #externalMacro(module: "HamcrestSwiftTestingMacros", type: "AssertThatMacro")
43-
44-

SwiftTesting/Source/Test/AssertThatMacroTests.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
//
55
// Created by René Pirringer on 16.09.24.
66
//
7+
import HamcrestSwiftTestingMacros
78
import SwiftSyntax
89
import SwiftSyntaxBuilder
910
import SwiftSyntaxMacros
1011
import SwiftSyntaxMacrosTestSupport
11-
import HamcrestSwiftTestingMacros
1212
import XCTest
1313

1414
#if canImport(HamcrestSwiftTestingMacros)
@@ -21,7 +21,6 @@ let testMacros: [String: Macro.Type] = [
2121
#endif
2222

2323
final class AssertThatMacroTests: XCTestCase {
24-
2524
func test_macro_syntax_with_equal_int() throws {
2625
#if canImport(HamcrestSwiftTestingMacros)
2726

@@ -38,7 +37,6 @@ final class AssertThatMacroTests: XCTestCase {
3837
#endif
3938
}
4039

41-
4240
func test_macro_syntax_with_equal_string() throws {
4341
#if canImport(HamcrestSwiftTestingMacros)
4442

SwiftTesting/Source/Test/SwiftTestIntegrationTests.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@
55
// Created by René Pirringer on 13.09.24.
66
//
77

8-
import Testing
98
import Hamcrest
109
import HamcrestSwiftTesting
10+
import Testing
1111

1212
struct SwiftTestIntegrationTests {
13-
1413
@Test func test_checkMatcher() async throws {
1514
checkMatcher("foo", equalTo("foo"), comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
1615
checkMatcher("foo", not(equalTo("bar")), comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
1716
}
1817

19-
2018
@Test func test_assertThat_macro() async throws {
2119
#if canImport(HamcrestSwiftTestingMacros)
2220
#assertThat("foo", equalTo("foo"))
@@ -25,7 +23,4 @@ struct SwiftTestIntegrationTests {
2523
throw XCTSkip("macros are only supported when running tests for the host platform")
2624
#endif
2725
}
28-
29-
3026
}
31-

0 commit comments

Comments
 (0)