Skip to content

Commit

Permalink
Added documentation for the Swift Testing support.
Browse files Browse the repository at this point in the history
  • Loading branch information
renep committed Sep 16, 2024
1 parent 9c9fa41 commit 1cf78fd
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 23 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
Version 2.3
-----------

_16 Sep 2024_

- Added Swift Testing support.
- Migrated to Swift 6

Version 2.2.4
-------------

_13 Mar 2023_

- Added compatibilty with Xcode 13. Issue #53
Expand Down
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// swift-tools-version:6.0

import PackageDescription
import CompilerPluginSupport
import PackageDescription

let package = Package(
name: "Hamcrest",
Expand All @@ -19,7 +19,6 @@ let package = Package(
dependencies: [],
path: "Hamcrest"),


.testTarget(
name: "HamcrestTests",
dependencies: [
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,30 @@ Integration

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

### Swift-Testing ###

Hamcrest also works with the new Swift-Testing framework. Migration to Swift-Testing is quite easy, because there `#assertThat` macro.
The macro is in the module `HamcrestSwiftTesting` so this needs to imported.

e.g.

```
import Hamcrest
import HamcrestSwiftTesting
import Testing
struct ExampleTest {
@Test func test_assertThat() async throws {
#assertThat("foo", equalTo("foo"))
#assertThat("foo", not(equalTo("bar")))
}
}
```


### CocoaPods ###

Integrate SwiftHamcrest using a Podfile similar to this:
Expand Down
7 changes: 1 addition & 6 deletions SwiftTesting/Source/Macros/AssertThatMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
//

import Foundation
import SwiftCompilerPlugin
import SwiftSyntax
import SwiftSyntaxMacros
import SwiftCompilerPlugin

public struct AssertThatMacro: ExpressionMacro, Sendable {

public static func expansion(of node: some SwiftSyntax.FreestandingMacroExpansionSyntax, in context: some SwiftSyntaxMacros.MacroExpansionContext) throws -> SwiftSyntax.ExprSyntax {

let arguments = node.arguments
guard arguments.count == 2 else {
fatalError("the macro does not have proper arguments")
Expand All @@ -28,8 +26,6 @@ public struct AssertThatMacro: ExpressionMacro, Sendable {
}
return "checkMatcher(\(firstArgument), \(secondArgument), comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()"
}


}

@main
Expand All @@ -39,5 +35,4 @@ struct HamcrestMacroPlugin: CompilerPlugin {
public let providingMacros: [Macro.Type] = [
AssertThatMacro.self
]

}
7 changes: 1 addition & 6 deletions SwiftTesting/Source/Main/SwiftTesting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
// Created by René Pirringer on 13.09.24.
//
import Foundation
import Testing
import Hamcrest
import SwiftSyntax
import SwiftSyntaxMacros

import Testing

@_disfavoredOverload public func checkMatcher<T>(
_ value: @autoclosure () throws -> T,
Expand All @@ -18,8 +17,6 @@ import SwiftSyntaxMacros
isRequired: Bool,
sourceLocation: Testing.SourceLocation
) -> Result<Void, any Error> {


if let message = applyMatcher(matcher, toValue: value) {
let expression = Testing.__Expression.__fromSyntaxNode(message)
return __checkValue(
Expand All @@ -40,5 +37,3 @@ import SwiftSyntaxMacros
_ comment: @autoclosure () -> Comment? = nil,
sourceLocation: Testing.SourceLocation = #_sourceLocation
) = #externalMacro(module: "HamcrestSwiftTestingMacros", type: "AssertThatMacro")


4 changes: 1 addition & 3 deletions SwiftTesting/Source/Test/AssertThatMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
//
// Created by René Pirringer on 16.09.24.
//
import HamcrestSwiftTestingMacros
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros
import SwiftSyntaxMacrosTestSupport
import HamcrestSwiftTestingMacros
import XCTest

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

final class AssertThatMacroTests: XCTestCase {

func test_macro_syntax_with_equal_int() throws {
#if canImport(HamcrestSwiftTestingMacros)

Expand All @@ -38,7 +37,6 @@ final class AssertThatMacroTests: XCTestCase {
#endif
}


func test_macro_syntax_with_equal_string() throws {
#if canImport(HamcrestSwiftTestingMacros)

Expand Down
7 changes: 1 addition & 6 deletions SwiftTesting/Source/Test/SwiftTestIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
// Created by René Pirringer on 13.09.24.
//

import Testing
import Hamcrest
import HamcrestSwiftTesting
import Testing

struct SwiftTestIntegrationTests {

@Test func test_checkMatcher() async throws {
checkMatcher("foo", equalTo("foo"), comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
checkMatcher("foo", not(equalTo("bar")), comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()
}


@Test func test_assertThat_macro() async throws {
#if canImport(HamcrestSwiftTestingMacros)
#assertThat("foo", equalTo("foo"))
Expand All @@ -25,7 +23,4 @@ struct SwiftTestIntegrationTests {
throw XCTSkip("macros are only supported when running tests for the host platform")
#endif
}


}

0 comments on commit 1cf78fd

Please sign in to comment.