Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 0 additions & 134 deletions Tests/MacroTestingTests/AddAsyncCompletionHandlerTests.swift

This file was deleted.

109 changes: 109 additions & 0 deletions Tests/MacroTestingTests/AddAsyncTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import MacroTesting
import XCTest

final class AddAsyncMacroTests: BaseTestCase {
override func invokeTest() {
withMacroTesting(macros: [AddAsyncMacro.self]) {
super.invokeTest()
}
}

func testExpansionTransformsFunctionWithResultCompletionToAsyncThrows() {
assertMacro {
#"""
@AddAsync
func c(a: Int, for b: String, _ value: Double, completionBlock: @escaping (Result<String, Error>) -> Void) -> Void {
completionBlock(.success("a: \(a), b: \(b), value: \(value)"))
}
"""#
} expansion: {
#"""
func c(a: Int, for b: String, _ value: Double, completionBlock: @escaping (Result<String, Error>) -> Void) -> Void {
completionBlock(.success("a: \(a), b: \(b), value: \(value)"))
}

func c(a: Int, for b: String, _ value: Double) async throws -> String {
try await withCheckedThrowingContinuation { continuation in
c(a: a, for: b, value) { returnValue in

switch returnValue {
case .success(let value):
continuation.resume(returning: value)
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
"""#
}
}

func testExpansionTransformsFunctionWithBoolCompletionToAsync() {
assertMacro {
"""
@AddAsync
func d(a: Int, for b: String, _ value: Double, completionBlock: @escaping (Bool) -> Void) -> Void {
completionBlock(true)
}
"""
} expansion: {
"""
func d(a: Int, for b: String, _ value: Double, completionBlock: @escaping (Bool) -> Void) -> Void {
completionBlock(true)
}

func d(a: Int, for b: String, _ value: Double) async -> Bool {
await withCheckedContinuation { continuation in
d(a: a, for: b, value) { returnValue in

continuation.resume(returning: returnValue)
}
}
}
"""
}
}

func testExpansionOnStoredPropertyEmitsError() {
assertMacro {
"""
struct Test {
@AddAsync
var name: String
}
"""
} diagnostics: {
"""
struct Test {
@AddAsync
┬────────
╰─ 🛑 @addAsync only works on functions
var name: String
}
"""
}
}

func testExpansionOnAsyncFunctionEmitsError() {
assertMacro {
"""
struct Test {
@AddAsync
async func sayHello() {
}
}
"""
} diagnostics: {
"""
struct Test {
@AddAsync
┬────────
╰─ 🛑 @addAsync requires an function that returns void
async func sayHello() {
}
}
"""
}
}
}
31 changes: 4 additions & 27 deletions Tests/MacroTestingTests/AddBlockerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,37 @@ final class AddBlockerTests: BaseTestCase {
}
}

func testAddBlocker() {
func testExpansionTransformsAdditionToSubtractionAndEmitsWarning() {
assertMacro {
"""
let x = 1
let y = 2
let z = 3
#addBlocker(x * y + z)
"""
} diagnostics: {
"""
let x = 1
let y = 2
let z = 3
#addBlocker(x * y + z)
───── ┬ ─
╰─ ⚠️ blocked an add; did you mean to subtract?
✏️ use '-'
"""
} fixes: {
"""
let x = 1
let y = 2
let z = 3
#addBlocker(x * y - z)
"""
} expansion: {
"""
let x = 1
let y = 2
let z = 3
x * y - z
"""
}
}

func testAddBlocker_Inline() {
func testExpansionPreservesSubtraction() {
assertMacro {
"""
#addBlocker(1 * 2 + 3)
"""
} diagnostics: {
"""
#addBlocker(1 * 2 + 3)
───── ┬ ─
╰─ ⚠️ blocked an add; did you mean to subtract?
✏️ use '-'
"""
} fixes: {
"""
#addBlocker(1 * 2 - 3)
#addBlocker(x * y - z)
"""
} expansion: {
"""
1 * 2 - 3
x * y - z
"""
}
}
Expand Down
Loading