Skip to content

Commit ba46351

Browse files
authored
chore: add unit tests for IDL-2.0 enum shapes code generation (#435)
This test complements the pull request that added logic to support enum shapes from IDL-2.0.
1 parent 0d3a3e6 commit ba46351

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

smithy-swift-codegen/src/test/kotlin/EnumGeneratorTests.kt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55

66
import io.kotest.matchers.string.shouldContain
7+
import org.junit.jupiter.api.Assertions
78
import org.junit.jupiter.api.Test
9+
import software.amazon.smithy.build.MockManifest
810
import software.amazon.smithy.codegen.core.SymbolProvider
911
import software.amazon.smithy.model.shapes.StringShape
1012
import software.amazon.smithy.model.traits.DocumentationTrait
@@ -138,6 +140,60 @@ class EnumGeneratorTests {
138140
contents.shouldContain(expectedGeneratedEnum)
139141
}
140142

143+
@Test
144+
fun `generates enums from IDL-2 enum shape`() {
145+
val model = javaClass.getResource("enum-shape-test.smithy").asSmithy()
146+
val manifest = MockManifest()
147+
val context = buildMockPluginContext(model, manifest, "smithy.example#Example")
148+
SwiftCodegenPlugin().execute(context)
149+
val suitEnumShape = manifest
150+
.getFileString("example/models/Suit.swift").get()
151+
Assertions.assertNotNull(suitEnumShape)
152+
153+
var expectedGeneratedEnum =
154+
"""
155+
extension ExampleClientTypes {
156+
public enum Suit: Swift.Equatable, Swift.RawRepresentable, Swift.CaseIterable, Swift.Codable, Swift.Hashable {
157+
case club
158+
case diamond
159+
case heart
160+
case spade
161+
case sdkUnknown(Swift.String)
162+
163+
public static var allCases: [Suit] {
164+
return [
165+
.club,
166+
.diamond,
167+
.heart,
168+
.spade,
169+
.sdkUnknown("")
170+
]
171+
}
172+
public init?(rawValue: Swift.String) {
173+
let value = Self.allCases.first(where: { ${'$'}0.rawValue == rawValue })
174+
self = value ?? Self.sdkUnknown(rawValue)
175+
}
176+
public var rawValue: Swift.String {
177+
switch self {
178+
case .club: return "CLUB"
179+
case .diamond: return "DIAMOND"
180+
case .heart: return "HEART"
181+
case .spade: return "SPADE"
182+
case let .sdkUnknown(s): return s
183+
}
184+
}
185+
public init(from decoder: Swift.Decoder) throws {
186+
let container = try decoder.singleValueContainer()
187+
let rawValue = try container.decode(RawValue.self)
188+
self = Suit(rawValue: rawValue) ?? Suit.sdkUnknown(rawValue)
189+
}
190+
}
191+
}
192+
""".trimIndent()
193+
194+
suitEnumShape.shouldContain(expectedGeneratedEnum)
195+
}
196+
141197
private fun createStringWithEnumTrait(vararg enumDefinitions: EnumDefinition): StringShape {
142198
val enumTraitBuilder = EnumTrait.builder()
143199
for (enumDefinition in enumDefinitions) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
$version: "2.0"
2+
3+
namespace smithy.example
4+
5+
service Example {
6+
version: "1.0.0"
7+
operations: [
8+
ChangeCard
9+
]
10+
}
11+
12+
operation ChangeCard {
13+
input: Card
14+
output: Card
15+
}
16+
17+
structure Card {
18+
suit: Suit
19+
}
20+
21+
enum Suit {
22+
DIAMOND
23+
CLUB
24+
HEART
25+
SPADE
26+
}

0 commit comments

Comments
 (0)