|
4 | 4 | */ |
5 | 5 |
|
6 | 6 | import io.kotest.matchers.string.shouldContain |
| 7 | +import org.junit.jupiter.api.Assertions |
7 | 8 | import org.junit.jupiter.api.Test |
| 9 | +import software.amazon.smithy.build.MockManifest |
8 | 10 | import software.amazon.smithy.codegen.core.SymbolProvider |
9 | 11 | import software.amazon.smithy.model.shapes.StringShape |
10 | 12 | import software.amazon.smithy.model.traits.DocumentationTrait |
@@ -138,6 +140,60 @@ class EnumGeneratorTests { |
138 | 140 | contents.shouldContain(expectedGeneratedEnum) |
139 | 141 | } |
140 | 142 |
|
| 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 | + |
141 | 197 | private fun createStringWithEnumTrait(vararg enumDefinitions: EnumDefinition): StringShape { |
142 | 198 | val enumTraitBuilder = EnumTrait.builder() |
143 | 199 | for (enumDefinition in enumDefinitions) { |
|
0 commit comments