-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
237 additions
and
8 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
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 |
---|---|---|
@@ -1,14 +1,68 @@ | ||
// swift-tools-version:5.8 | ||
// swift-tools-version:6.0 | ||
|
||
import PackageDescription | ||
import CompilerPluginSupport | ||
|
||
let package = Package( | ||
name: "Hamcrest", | ||
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)], | ||
products: [ | ||
.library(name: "Hamcrest", targets: ["Hamcrest"]), | ||
.library(name: "Hamcrest", targets: ["Hamcrest"]) | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/swiftlang/swift-syntax.git", from: "600.0.0-latest") | ||
], | ||
targets: [ | ||
.target(name: "Hamcrest", dependencies: [], path: "Hamcrest"), | ||
.testTarget(name: "HamcrestTests", dependencies: ["Hamcrest"], path: "HamcrestTests"), | ||
.target( | ||
name: "Hamcrest", | ||
dependencies: [], | ||
path: "Hamcrest"), | ||
|
||
|
||
.testTarget( | ||
name: "HamcrestTests", | ||
dependencies: [ | ||
"Hamcrest", | ||
], | ||
path: "HamcrestTests"), | ||
|
||
.macro( | ||
name: "HamcrestSwiftTestingMacros", | ||
dependencies: [ | ||
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"), | ||
.product(name: "SwiftCompilerPlugin", package: "swift-syntax") | ||
], | ||
path: "SwiftTesting", | ||
sources: [ | ||
"Source/Macros" | ||
] | ||
), | ||
|
||
.target( | ||
name: "HamcrestSwiftTesting", | ||
dependencies: [ | ||
"HamcrestSwiftTestingMacros", | ||
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"), | ||
.product(name: "SwiftCompilerPlugin", package: "swift-syntax") | ||
], | ||
path: "SwiftTesting", | ||
sources: [ | ||
"Source/Main" | ||
] | ||
), | ||
|
||
.testTarget( | ||
name: "HamcrestSwiftTestingTests", | ||
dependencies: [ | ||
"Hamcrest", | ||
"HamcrestSwiftTesting", | ||
"HamcrestSwiftTestingMacros", | ||
.product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax") | ||
], | ||
path: "SwiftTesting", | ||
sources: [ | ||
"Source/Test" | ||
] | ||
) | ||
] | ||
) |
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,43 @@ | ||
// | ||
// AssesrtThatMacro.swift | ||
// Hamcrest | ||
// | ||
// Created by René Pirringer on 13.09.24. | ||
// | ||
|
||
import Foundation | ||
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") | ||
} | ||
|
||
var iterator = arguments.makeIterator() | ||
guard let firstArgument = iterator.next()?.expression else { | ||
fatalError("the argument expression is missing") | ||
} | ||
guard let secondArgument = iterator.next()?.expression else { | ||
fatalError("the argument expression is missing") | ||
} | ||
return "checkMatcher(\(firstArgument), \(secondArgument), comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected()" | ||
} | ||
|
||
|
||
} | ||
|
||
@main | ||
struct HamcrestMacroPlugin: CompilerPlugin { | ||
public init() {} | ||
|
||
public let providingMacros: [Macro.Type] = [ | ||
AssertThatMacro.self | ||
] | ||
|
||
} |
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,44 @@ | ||
// | ||
// SwiftTesting.swift | ||
// Hamcrest | ||
// | ||
// Created by René Pirringer on 13.09.24. | ||
// | ||
import Foundation | ||
import Testing | ||
import Hamcrest | ||
import SwiftSyntax | ||
import SwiftSyntaxMacros | ||
|
||
|
||
@_disfavoredOverload public func checkMatcher<T>( | ||
_ value: @autoclosure () throws -> T, | ||
_ matcher: Matcher<T>, | ||
comments: @autoclosure () -> [Comment], | ||
isRequired: Bool, | ||
sourceLocation: Testing.SourceLocation | ||
) -> Result<Void, any Error> { | ||
|
||
|
||
if let message = applyMatcher(matcher, toValue: value) { | ||
let expression = Testing.__Expression.__fromSyntaxNode(message) | ||
return __checkValue( | ||
false, | ||
expression: expression, | ||
expressionWithCapturedRuntimeValues: expression, | ||
comments: comments(), | ||
isRequired: isRequired, | ||
sourceLocation: sourceLocation | ||
) | ||
} | ||
return .success(()) | ||
} | ||
|
||
@freestanding(expression) public macro assertThat<T>( | ||
_ value: @autoclosure () throws -> T, | ||
_ matcher: Matcher<T>, | ||
_ comment: @autoclosure () -> Comment? = nil, | ||
sourceLocation: Testing.SourceLocation = #_sourceLocation | ||
) = #externalMacro(module: "HamcrestSwiftTestingMacros", type: "AssertThatMacro") | ||
|
||
|
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,57 @@ | ||
// | ||
// AssertThatMacroTests.swift | ||
// HamcrestTests | ||
// | ||
// Created by René Pirringer on 16.09.24. | ||
// | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
import SwiftSyntaxMacrosTestSupport | ||
import HamcrestSwiftTestingMacros | ||
import XCTest | ||
|
||
#if canImport(HamcrestSwiftTestingMacros) | ||
import HamcrestSwiftTestingMacros | ||
|
||
let testMacros: [String: Macro.Type] = [ | ||
"assertThat": AssertThatMacro.self | ||
] | ||
|
||
#endif | ||
|
||
final class AssertThatMacroTests: XCTestCase { | ||
|
||
func test_macro_syntax_with_equal_int() throws { | ||
#if canImport(HamcrestSwiftTestingMacros) | ||
|
||
assertMacroExpansion( | ||
""" | ||
#assertThat(1, equalTo(1)) | ||
""", expandedSource: | ||
""" | ||
checkMatcher(1, equalTo(1), comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected() | ||
""", | ||
macros: testMacros) | ||
#else | ||
throw XCTSkip("macros are only supported when running tests for the host platform") | ||
#endif | ||
} | ||
|
||
|
||
func test_macro_syntax_with_equal_string() throws { | ||
#if canImport(HamcrestSwiftTestingMacros) | ||
|
||
assertMacroExpansion( | ||
""" | ||
#assertThat("1", equalTo("1")) | ||
""", expandedSource: | ||
""" | ||
checkMatcher("1", equalTo("1"), comments: [], isRequired: false, sourceLocation: Testing.SourceLocation.__here()).__expected() | ||
""", | ||
macros: testMacros) | ||
#else | ||
throw XCTSkip("macros are only supported when running tests for the host platform") | ||
#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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// SwiftTestIntegrationTests.swift | ||
// HamcrestTests | ||
// | ||
// Created by René Pirringer on 13.09.24. | ||
// | ||
|
||
import Testing | ||
import Hamcrest | ||
import HamcrestSwiftTesting | ||
|
||
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")) | ||
#assertThat("foo", not(equalTo("bar"))) | ||
#else | ||
throw XCTSkip("macros are only supported when running tests for the host platform") | ||
#endif | ||
} | ||
|
||
|
||
} | ||
|