-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParameterExtractorTests.swift
More file actions
113 lines (96 loc) · 3.86 KB
/
ParameterExtractorTests.swift
File metadata and controls
113 lines (96 loc) · 3.86 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
//
// ParameterExtractorTests.swift
// PrincipleMacros
//
// Created by Kamil Strzelecki on 05/04/2025.
// Copyright © 2025 Kamil Strzelecki. All rights reserved.
//
@testable import PrincipleMacros
import Testing
internal struct ParameterExtractorTests {
private func makeExtractor(from expr: ExprSyntax) throws -> ParameterExtractor {
let macro = try #require(MacroExpansionExprSyntax(expr))
return ParameterExtractor(from: macro)
}
@Test
func testExpressionExtraction() throws {
let extractor = try makeExtractor(from: "#MyMacro(value: Type.make())")
let extracted = extractor.expression(withLabel: "value")
let expected: ExprSyntax = "Type.make()"
#expect(extracted?.description == expected.description)
}
@Test
func testUnnamedExpressionExtraction() throws {
let extractor = try makeExtractor(from: "#MyMacro(value: Type.make(), 123)")
let extracted = extractor.expression(withLabel: nil)
let expected: ExprSyntax = "123"
#expect(extracted?.description == expected.description)
}
@Test
func testMissingExpressionExtraction() throws {
let extractor = try makeExtractor(from: #"#MyMacro(arg: Type.make())"#)
let extracted = extractor.expression(withLabel: "value")
#expect(extracted == nil)
}
}
extension ParameterExtractorTests {
@Test
func testTrailingClosureExtraction() throws {
let extractor = try makeExtractor(from: "#MyMacro { _ in }")
let extracted = try extractor.trailingClosure(withLabel: "operation")
#expect(extracted?.description == "{ _ in }")
}
@Test
func testTrailingClosureReferenceExtraction() throws {
let extractor = try makeExtractor(from: "#MyMacro(operation: perform)")
let extracted = try extractor.trailingClosure(withLabel: "operation")
#expect(extracted?.description == "perform")
}
}
extension ParameterExtractorTests {
@Test
func testRawStringExtraction() throws {
let extractor = try makeExtractor(from: #"#MyMacro(string: "arg")"#)
let extracted = try extractor.rawString(withLabel: "string")
#expect(extracted == "arg")
}
@Test
func testUnexpectedSyntaxWhenPerformingRawStringExtraction() throws {
let extractor = try makeExtractor(from: #"#MyMacro(string: reference.arg)"#)
#expect(throws: ParameterExtractionError.unexpectedSyntaxType) {
try extractor.rawString(withLabel: "string")
}
}
}
extension ParameterExtractorTests {
@Test
func testMissingPreferredGlobalActorExtraction() throws {
let extractor = try makeExtractor(from: "#MyMacro()")
let extracted = try extractor.preferredGlobalActorIsolation(withLabel: "isolation")
#expect(extracted == nil)
}
@Test
func testNonisolatedPreferredGlobalActorExtraction() throws {
let extractor = try makeExtractor(from: "#MyMacro(isolation: nil)")
let extracted = try extractor.preferredGlobalActorIsolation(withLabel: "isolation")
#expect(extracted == .nonisolated)
}
@Test(
arguments: [
"MainActor",
"SomeType.SomeActor"
]
)
func testIsolatedPreferredGlobalActorExtraction(isolation: String) throws {
let extractor = try makeExtractor(from: "#MyMacro(isolation: \(raw: isolation).self)")
let extracted = try extractor.preferredGlobalActorIsolation(withLabel: "isolation")
#expect(extracted?.attribute?.description == "@\(isolation)")
}
@Test
func testUnexpectedSyntaxWhenPerformingPreferredGlobalActorExtraction() throws {
let extractor = try makeExtractor(from: #"#MyMacro(isolation: MainActor.Type)"#)
#expect(throws: ParameterExtractionError.unexpectedSyntaxType) {
try extractor.preferredGlobalActorIsolation(withLabel: "isolation")
}
}
}