Skip to content

Commit ec55cd5

Browse files
committed
test imports directly
1 parent a968d3c commit ec55cd5

File tree

7 files changed

+212
-164
lines changed

7 files changed

+212
-164
lines changed

Sources/GRPCCodeGen/Internal/Translator/IDLToStructuredSwiftTranslator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ struct IDLToStructuredSwiftTranslator: Translator {
9797
return StructuredSwiftRepresentation(file: file)
9898
}
9999

100-
private func makeImports(
100+
internal func makeImports(
101101
dependencies: [Dependency],
102102
accessLevel: SourceGenerator.Config.AccessLevel,
103103
accessLevelOnImports: Bool

Tests/GRPCCodeGenTests/Internal/StructuredSwift+ClientTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Testing
1818

1919
@testable import GRPCCodeGen
2020

21-
extension StructuedSwiftTests {
21+
extension StructuredSwiftTests {
2222
@Suite("Client")
2323
struct Client {
2424
@Test(
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*
2+
* Copyright 2024, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import Testing
18+
19+
@testable import GRPCCodeGen
20+
21+
extension StructuredSwiftTests {
22+
@Suite("Import")
23+
struct Import {
24+
static let translator = IDLToStructuredSwiftTranslator()
25+
26+
static let allAccessLevels: [SourceGenerator.Config.AccessLevel] = [
27+
.internal, .public, .package,
28+
]
29+
30+
@Test(
31+
"import rendering",
32+
arguments: allAccessLevels
33+
)
34+
func imports(accessLevel: SourceGenerator.Config.AccessLevel) throws {
35+
var dependencies = [Dependency]()
36+
dependencies.append(Dependency(module: "Foo", accessLevel: .public))
37+
dependencies.append(
38+
Dependency(
39+
item: .init(kind: .typealias, name: "Bar"),
40+
module: "Foo",
41+
accessLevel: .internal
42+
)
43+
)
44+
dependencies.append(
45+
Dependency(
46+
item: .init(kind: .struct, name: "Baz"),
47+
module: "Foo",
48+
accessLevel: .package
49+
)
50+
)
51+
dependencies.append(
52+
Dependency(
53+
item: .init(kind: .class, name: "Bac"),
54+
module: "Foo",
55+
accessLevel: .package
56+
)
57+
)
58+
dependencies.append(
59+
Dependency(
60+
item: .init(kind: .enum, name: "Bap"),
61+
module: "Foo",
62+
accessLevel: .package
63+
)
64+
)
65+
dependencies.append(
66+
Dependency(
67+
item: .init(kind: .protocol, name: "Bat"),
68+
module: "Foo",
69+
accessLevel: .package
70+
)
71+
)
72+
dependencies.append(
73+
Dependency(
74+
item: .init(kind: .let, name: "Baq"),
75+
module: "Foo",
76+
accessLevel: .package
77+
)
78+
)
79+
dependencies.append(
80+
Dependency(
81+
item: .init(kind: .var, name: "Bag"),
82+
module: "Foo",
83+
accessLevel: .package
84+
)
85+
)
86+
dependencies.append(
87+
Dependency(
88+
item: .init(kind: .func, name: "Bak"),
89+
module: "Foo",
90+
accessLevel: .package
91+
)
92+
)
93+
94+
let expected =
95+
"""
96+
\(accessLevel.level) import GRPCCore
97+
public import Foo
98+
internal import typealias Foo.Bar
99+
package import struct Foo.Baz
100+
package import class Foo.Bac
101+
package import enum Foo.Bap
102+
package import protocol Foo.Bat
103+
package import let Foo.Baq
104+
package import var Foo.Bag
105+
package import func Foo.Bak
106+
"""
107+
108+
let imports = try StructuredSwiftTests.Import.translator.makeImports(
109+
dependencies: dependencies,
110+
accessLevel: accessLevel,
111+
accessLevelOnImports: true
112+
)
113+
114+
#expect(render(imports) == expected)
115+
}
116+
117+
@Test(
118+
"preconcurrency import rendering"
119+
)
120+
func preconcurrencyImports() throws {
121+
var dependencies = [Dependency]()
122+
dependencies.append(
123+
Dependency(
124+
module: "Foo",
125+
preconcurrency: .required,
126+
accessLevel: .internal
127+
)
128+
)
129+
dependencies.append(
130+
Dependency(
131+
item: .init(kind: .enum, name: "Bar"),
132+
module: "Foo",
133+
preconcurrency: .required,
134+
accessLevel: .internal
135+
)
136+
)
137+
dependencies.append(
138+
Dependency(
139+
module: "Baz",
140+
preconcurrency: .requiredOnOS(["Deq", "Der"]),
141+
accessLevel: .internal
142+
)
143+
)
144+
145+
let expected =
146+
"""
147+
public import GRPCCore
148+
@preconcurrency internal import Foo
149+
@preconcurrency internal import enum Foo.Bar
150+
#if os(Deq) || os(Der)
151+
@preconcurrency internal import Baz
152+
#else
153+
internal import Baz
154+
#endif
155+
"""
156+
157+
let imports = try StructuredSwiftTests.Import.translator.makeImports(
158+
dependencies: dependencies,
159+
accessLevel: .public,
160+
accessLevelOnImports: true
161+
)
162+
163+
#expect(render(imports) == expected)
164+
}
165+
166+
@Test(
167+
"SPI import rendering"
168+
)
169+
func spiImports() throws {
170+
var dependencies = [Dependency]()
171+
dependencies.append(
172+
Dependency(module: "Foo", spi: "Secret", accessLevel: .internal)
173+
)
174+
dependencies.append(
175+
Dependency(
176+
item: .init(kind: .enum, name: "Bar"),
177+
module: "Foo",
178+
spi: "Secret",
179+
accessLevel: .internal
180+
)
181+
)
182+
183+
let expected =
184+
"""
185+
public import GRPCCore
186+
@_spi(Secret) internal import Foo
187+
@_spi(Secret) internal import enum Foo.Bar
188+
"""
189+
190+
let imports = try StructuredSwiftTests.Import.translator.makeImports(
191+
dependencies: dependencies,
192+
accessLevel: .public,
193+
accessLevelOnImports: true
194+
)
195+
196+
#expect(render(imports) == expected)
197+
}
198+
199+
}
200+
}

Tests/GRPCCodeGenTests/Internal/StructuredSwift+MetadataTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Testing
1818

1919
@testable import GRPCCodeGen
2020

21-
extension StructuedSwiftTests {
21+
extension StructuredSwiftTests {
2222
@Suite("Metadata")
2323
struct Metadata {
2424
@Test("typealias Input = <Name>", arguments: AccessModifier.allCases)

Tests/GRPCCodeGenTests/Internal/StructuredSwift+ServerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Testing
1818

1919
@testable import GRPCCodeGen
2020

21-
extension StructuedSwiftTests {
21+
extension StructuredSwiftTests {
2222
@Suite("Server")
2323
struct Server {
2424
@Test(

Tests/GRPCCodeGenTests/Internal/StructuredSwiftTestHelpers.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import Testing
1919
@testable import GRPCCodeGen
2020

2121
// Used as a namespace for organising other structured swift tests.
22-
@Suite("Structued Swift")
23-
struct StructuedSwiftTests {}
22+
@Suite("Structured Swift")
23+
struct StructuredSwiftTests {}
2424

2525
func render(_ declaration: Declaration) -> String {
2626
let renderer = TextBasedRenderer(indentation: 2)
@@ -40,6 +40,12 @@ func render(_ blocks: [CodeBlock]) -> String {
4040
return renderer.renderedContents()
4141
}
4242

43+
func render(_ imports: [ImportDescription]) -> String {
44+
let renderer = TextBasedRenderer(indentation: 2)
45+
renderer.renderImports(imports)
46+
return renderer.renderedContents()
47+
}
48+
4349
enum RPCKind: Hashable, Sendable, CaseIterable {
4450
case unary
4551
case clientStreaming

0 commit comments

Comments
 (0)