-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathExpression.swift
More file actions
145 lines (138 loc) · 5.06 KB
/
Copy pathExpression.swift
File metadata and controls
145 lines (138 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import SwiftSyntax
import SwiftSyntaxMacros
/// Dummy implementation of swift-testing `require` macro.
struct RequireOptional: ExpressionMacro {
/// Dummy implementation of swift-testing `require` macro.
///
/// - Parameters:
/// - node: The expression describing this macro.
/// - context: The context in which to perform the macro expansion.
///
/// - Returns: Equivalent `XCTUnwrap` expression.
static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> ExprSyntax {
guard !node.arguments.isEmpty else {
let message = TestingMacroMessage(
message: "Expecting atleast one argument",
severity: .error
)
context.diagnose(.init(node: node, message: message))
return ""
}
return ExprSyntax(
FunctionCallExprSyntax(callee: "XCTUnwrap" as ExprSyntax) {
LabeledExprSyntax(expression: node.arguments.first!.expression)
}
)
}
}
/// Dummy implementation of swift-testing `expect` macro.
struct Expect: ExpressionMacro {
/// Dummy implementation of swift-testing `expect` macro.
///
/// This returns equivalent:
/// * `XCTAssertEqual` expression if condition is an equality check.
/// * `XCTAssertTrue` expression otherwise.
///
/// - Parameters:
/// - node: The expression describing this macro.
/// - context: The context in which to perform the macro expansion.
///
/// - Returns: Equivalent `XCTest` expression.
static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> ExprSyntax {
guard
node.arguments.count == 1,
let expr = node.arguments.first?.expression,
let compExpr = expr.as(InfixOperatorExprSyntax.self),
let oExpr = compExpr.operator.as(BinaryOperatorExprSyntax.self),
oExpr.operator.tokenKind == .binaryOperator("==")
else {
guard !node.arguments.isEmpty else {
let message = TestingMacroMessage(
message: "Expecting atleast one argument",
severity: .error
)
context.diagnose(.init(node: node, message: message))
return ""
}
return ExprSyntax(
FunctionCallExprSyntax(callee: "XCTAssertTrue" as ExprSyntax) {
LabeledExprSyntax(
expression: node.arguments.first!.expression
)
}
)
}
return ExprSyntax(
FunctionCallExprSyntax(callee: "XCTAssertEqual" as ExprSyntax) {
LabeledExprSyntax(expression: compExpr.leftOperand)
LabeledExprSyntax(expression: compExpr.rightOperand)
}
)
}
}
/// Dummy implementation of swift-testing `expect(throws:)` macro.
struct ExpectThrows: ExpressionMacro {
/// Dummy implementation of swift-testing `expect(throws:)` macro.
///
/// This returns to `XCTAssertThrowsError` expression that uses
/// `XCTAssertTrue` for validating error type in `errorHandler`
/// closure expression.
///
/// - Parameters:
/// - node: The expression describing this macro.
/// - context: The context in which to perform the macro expansion.
///
/// - Returns: Equivalent `XCTest` expression.
static func expansion(
of node: some FreestandingMacroExpansionSyntax,
in context: some MacroExpansionContext
) throws -> ExprSyntax {
let trailingClosure: ClosureExprSyntax
if let closure = node.trailingClosure {
trailingClosure = closure
} else if node.arguments.count > 1,
case let args = node.arguments,
case let index = args.index(args.startIndex, offsetBy: 1),
let closure = args[index].expression.as(ClosureExprSyntax.self)
{
trailingClosure = closure
} else {
let message = TestingMacroMessage(
message: "Expecting closure",
severity: .error
)
context.diagnose(.init(node: node, message: message))
return ""
}
let errorHandler = ClosureExprSyntax {
"XCTAssertTrue(type(of: $0) == Decodable.self)"
}
return ExprSyntax(
TryExprSyntax(
expression: FunctionCallExprSyntax(
callee: "XCTAssertThrowsError" as ExprSyntax,
trailingClosure: errorHandler
) {
LabeledExprSyntax(
expression: FunctionCallExprSyntax(
callee: trailingClosure
)
)
}
)
)
}
}
#if !canImport(SwiftSyntax510)
extension FreestandingMacroExpansionSyntax {
var arguments: LabeledExprListSyntax {
return self.argumentList
}
}
#endif