diff --git a/CHANGELOG.md b/CHANGELOG.md index d7bbf68..d03e8cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Package.swift b/Package.swift index ca7429f..64b4ec5 100644 --- a/Package.swift +++ b/Package.swift @@ -1,7 +1,7 @@ // swift-tools-version:6.0 -import PackageDescription import CompilerPluginSupport +import PackageDescription let package = Package( name: "Hamcrest", @@ -19,7 +19,6 @@ let package = Package( dependencies: [], path: "Hamcrest"), - .testTarget( name: "HamcrestTests", dependencies: [ diff --git a/README.md b/README.md index 32cbc23..6e9162f 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/SwiftTesting/Source/Macros/AssertThatMacro.swift b/SwiftTesting/Source/Macros/AssertThatMacro.swift index 1171291..4b78860 100644 --- a/SwiftTesting/Source/Macros/AssertThatMacro.swift +++ b/SwiftTesting/Source/Macros/AssertThatMacro.swift @@ -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") @@ -28,8 +26,6 @@ public struct AssertThatMacro: ExpressionMacro, Sendable { } return "checkMatcher(\(firstArgument), \(secondArgument), comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()" } - - } @main @@ -39,5 +35,4 @@ struct HamcrestMacroPlugin: CompilerPlugin { public let providingMacros: [Macro.Type] = [ AssertThatMacro.self ] - } diff --git a/SwiftTesting/Source/Main/SwiftTesting.swift b/SwiftTesting/Source/Main/SwiftTesting.swift index fef74d0..6c13dd3 100644 --- a/SwiftTesting/Source/Main/SwiftTesting.swift +++ b/SwiftTesting/Source/Main/SwiftTesting.swift @@ -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( _ value: @autoclosure () throws -> T, @@ -18,8 +17,6 @@ import SwiftSyntaxMacros isRequired: Bool, sourceLocation: Testing.SourceLocation ) -> Result { - - if let message = applyMatcher(matcher, toValue: value) { let expression = Testing.__Expression.__fromSyntaxNode(message) return __checkValue( @@ -40,5 +37,3 @@ import SwiftSyntaxMacros _ comment: @autoclosure () -> Comment? = nil, sourceLocation: Testing.SourceLocation = #_sourceLocation ) = #externalMacro(module: "HamcrestSwiftTestingMacros", type: "AssertThatMacro") - - diff --git a/SwiftTesting/Source/Test/AssertThatMacroTests.swift b/SwiftTesting/Source/Test/AssertThatMacroTests.swift index e1fb664..f8a8e07 100644 --- a/SwiftTesting/Source/Test/AssertThatMacroTests.swift +++ b/SwiftTesting/Source/Test/AssertThatMacroTests.swift @@ -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) @@ -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) @@ -38,7 +37,6 @@ final class AssertThatMacroTests: XCTestCase { #endif } - func test_macro_syntax_with_equal_string() throws { #if canImport(HamcrestSwiftTestingMacros) diff --git a/SwiftTesting/Source/Test/SwiftTestIntegrationTests.swift b/SwiftTesting/Source/Test/SwiftTestIntegrationTests.swift index cf793da..88eb886 100644 --- a/SwiftTesting/Source/Test/SwiftTestIntegrationTests.swift +++ b/SwiftTesting/Source/Test/SwiftTestIntegrationTests.swift @@ -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")) @@ -25,7 +23,4 @@ struct SwiftTestIntegrationTests { throw XCTSkip("macros are only supported when running tests for the host platform") #endif } - - } -