diff --git a/Tests/XMLCoderTests/NestedChoiceArrayTest.swift b/Tests/XMLCoderTests/NestedChoiceArrayTest.swift new file mode 100644 index 00000000..9ba53191 --- /dev/null +++ b/Tests/XMLCoderTests/NestedChoiceArrayTest.swift @@ -0,0 +1,96 @@ +// +// NestedChoiceArrayTest.swift +// XMLCoder +// +// Created by Benjamin Wetherfield on 8/22/19. +// + +import XCTest +@testable import XMLCoder + +private struct Book: Decodable { + let title: String + let chapters: Chapters + + enum CodingKeys: String, CodingKey { + case title + case chapters + } +} + +private struct Chapters { + let items: [Chapter] +} + +extension Chapters: Decodable, Equatable { + init(from decoder: Decoder) throws { + let container = try decoder.singleValueContainer() + items = try container.decode([Chapter].self) + } +} + +private enum Chapter { + struct Content { + let title: String + let content: String + } + + case intro(Content) + case body(Content) + case outro(Content) +} + +extension Chapter: Decodable { + enum CodingKeys: String, XMLChoiceCodingKey { + case intro, body = "chapter", outro + } + + init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + do { + self = .body(try container.decode(Content.self, forKey: .body)) + } catch { + do { + self = .intro(try container.decode(Content.self, forKey: .intro)) + } catch { + self = .outro(try container.decode(Content.self, forKey: .outro)) + } + } + } +} + +extension Chapter.Content: Decodable { + enum CodingKeys: String, CodingKey { + case title + case content = "" + } +} + +extension Book: Equatable {} +extension Chapter: Equatable {} +extension Chapter.Content: Equatable {} + +class NestedChoiceArrayTest: XCTestCase { + func testDecodingNestedChoiceArray() throws { + let xml = """ + + + + Content of first chapter + Content of chapter 1 + Content of chapter 2 + Content of last chapter + + + """ + let decoded = try XMLDecoder().decode(Book.self, from: xml.data(using: .utf8)!) + let expected = Book(title: "Example", + chapters: Chapters(items: [ + .intro(.init(title: "Intro", content: "Content of first chapter")), + .body(.init(title: "Chapter 1", content: "Content of chapter 1")), + .body(.init(title: "Chapter 2", content: "Content of chapter 2")), + .outro(.init(title: "Epilogue", content: "Content of last chapter")), + ])) + XCTAssertEqual(decoded, expected) + } +} diff --git a/XMLCoder.xcodeproj/project.pbxproj b/XMLCoder.xcodeproj/project.pbxproj index c043dead..287f49e6 100644 --- a/XMLCoder.xcodeproj/project.pbxproj +++ b/XMLCoder.xcodeproj/project.pbxproj @@ -21,6 +21,7 @@ /* End PBXAggregateTarget section */ /* Begin PBXBuildFile section */ + B5EA3BB6230F237800D8D69B /* NestedChoiceArrayTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5EA3BB4230F235C00D8D69B /* NestedChoiceArrayTest.swift */; }; B5F74472233F74E400BBDB15 /* RootLevelAttributeTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5F74471233F74E400BBDB15 /* RootLevelAttributeTest.swift */; }; OBJ_148 /* BoolBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_12 /* BoolBox.swift */; }; OBJ_149 /* Box.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_13 /* Box.swift */; }; @@ -153,6 +154,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + B5EA3BB4230F235C00D8D69B /* NestedChoiceArrayTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NestedChoiceArrayTest.swift; sourceTree = ""; }; B5F74471233F74E400BBDB15 /* RootLevelAttributeTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RootLevelAttributeTest.swift; sourceTree = ""; }; OBJ_100 /* DecimalTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DecimalTests.swift; sourceTree = ""; }; OBJ_101 /* EmptyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyTests.swift; sourceTree = ""; }; @@ -448,6 +450,7 @@ OBJ_125 /* SimpleChoiceTests.swift */, OBJ_126 /* SingleChildTests.swift */, OBJ_127 /* SpacePreserveTest.swift */, + B5EA3BB4230F235C00D8D69B /* NestedChoiceArrayTest.swift */, B5F74471233F74E400BBDB15 /* RootLevelAttributeTest.swift */, ); name = XMLCoderTests; @@ -721,6 +724,7 @@ OBJ_252 /* NullTests.swift in Sources */, OBJ_253 /* OptionalTests.swift in Sources */, OBJ_254 /* StringTests.swift in Sources */, + B5EA3BB6230F237800D8D69B /* NestedChoiceArrayTest.swift in Sources */, OBJ_255 /* UIntTests.swift in Sources */, OBJ_256 /* URLTests.swift in Sources */, OBJ_257 /* UnkeyedIntTests.swift in Sources */, diff --git a/XMLCoder.xcodeproj/xcshareddata/xcbaselines/XMLCoder::XMLCoderTests.xcbaseline/76E090BF-7AFE-4988-A06A-3C423396A4A4.plist b/XMLCoder.xcodeproj/xcshareddata/xcbaselines/XMLCoder::XMLCoderTests.xcbaseline/76E090BF-7AFE-4988-A06A-3C423396A4A4.plist index 606e6b36..0a5c0892 100644 --- a/XMLCoder.xcodeproj/xcshareddata/xcbaselines/XMLCoder::XMLCoderTests.xcbaseline/76E090BF-7AFE-4988-A06A-3C423396A4A4.plist +++ b/XMLCoder.xcodeproj/xcshareddata/xcbaselines/XMLCoder::XMLCoderTests.xcbaseline/76E090BF-7AFE-4988-A06A-3C423396A4A4.plist @@ -61,7 +61,7 @@ com.apple.XCTPerformanceMetric_WallClockTime baselineAverage - 0.08847 + 0.263 baselineIntegrationDisplayName Local Baseline