|
| 1 | +// |
| 2 | +// MixedChoiceAndNonChoiceTests.swift |
| 3 | +// XMLCoderTests |
| 4 | +// |
| 5 | +// Created by Benjamin Wetherfield on 11/24/19. |
| 6 | +// |
| 7 | + |
| 8 | +import XCTest |
| 9 | +import XMLCoder |
| 10 | + |
| 11 | +private struct MixedIntOrStringFirst: Equatable { |
| 12 | + let intOrString: IntOrString |
| 13 | + let otherValue: String |
| 14 | +} |
| 15 | + |
| 16 | +extension MixedIntOrStringFirst: Encodable { |
| 17 | + enum CodingKeys: String, CodingKey { |
| 18 | + case otherValue = "other-value" |
| 19 | + } |
| 20 | + |
| 21 | + func encode(to encoder: Encoder) throws { |
| 22 | + try intOrString.encode(to: encoder) |
| 23 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 24 | + try container.encode(otherValue, forKey: .otherValue) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +private struct MixedOtherFirst: Equatable { |
| 29 | + let intOrString: IntOrString |
| 30 | + let otherValue: String |
| 31 | +} |
| 32 | + |
| 33 | +extension MixedOtherFirst: Encodable { |
| 34 | + enum CodingKeys: String, CodingKey { |
| 35 | + case otherValue = "other-value" |
| 36 | + } |
| 37 | + |
| 38 | + func encode(to encoder: Encoder) throws { |
| 39 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 40 | + try container.encode(otherValue, forKey: .otherValue) |
| 41 | + try intOrString.encode(to: encoder) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +private struct MixedEitherSide { |
| 46 | + let leading: String |
| 47 | + let intOrString: IntOrString |
| 48 | + let trailing: String |
| 49 | +} |
| 50 | + |
| 51 | +extension MixedEitherSide: Encodable { |
| 52 | + enum CodingKeys: String, CodingKey { |
| 53 | + case leading |
| 54 | + case trailing |
| 55 | + } |
| 56 | + |
| 57 | + func encode(to encoder: Encoder) throws { |
| 58 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 59 | + try container.encode(leading, forKey: .leading) |
| 60 | + try intOrString.encode(to: encoder) |
| 61 | + try container.encode(trailing, forKey: .trailing) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +private struct TwoChoiceElements { |
| 66 | + let first: IntOrString |
| 67 | + let second: IntOrString |
| 68 | +} |
| 69 | + |
| 70 | +extension TwoChoiceElements: Encodable { |
| 71 | + func encode(to encoder: Encoder) throws { |
| 72 | + try first.encode(to: encoder) |
| 73 | + try second.encode(to: encoder) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +class MixedChoiceAndNonChoiceTests: XCTestCase { |
| 78 | + func testMixedChoiceFirstEncode() throws { |
| 79 | + let first = MixedIntOrStringFirst(intOrString: .int(4), otherValue: "other") |
| 80 | + let firstEncoded = try XMLEncoder().encode(first, withRootKey: "container") |
| 81 | + let firstExpectedXML = "<container><int>4</int><other-value>other</other-value></container>" |
| 82 | + XCTAssertEqual(String(data: firstEncoded, encoding: .utf8), firstExpectedXML) |
| 83 | + } |
| 84 | + |
| 85 | + func testMixedChoiceSecondEncode() throws { |
| 86 | + let second = MixedOtherFirst(intOrString: .int(4), otherValue: "other") |
| 87 | + let secondEncoded = try XMLEncoder().encode(second, withRootKey: "container") |
| 88 | + let secondExpectedXML = "<container><other-value>other</other-value><int>4</int></container>" |
| 89 | + XCTAssertEqual(String(data: secondEncoded, encoding: .utf8), secondExpectedXML) |
| 90 | + } |
| 91 | + |
| 92 | + func testMixedChoiceFlankedEncode() throws { |
| 93 | + let flanked = MixedEitherSide(leading: "first", intOrString: .string("then"), trailing: "second") |
| 94 | + let flankedEncoded = try XMLEncoder().encode(flanked, withRootKey: "container") |
| 95 | + let flankedExpectedXML = """ |
| 96 | + <container><leading>first</leading><string>then</string><trailing>second</trailing></container> |
| 97 | + """ |
| 98 | + XCTAssertEqual(String(data: flankedEncoded, encoding: .utf8), flankedExpectedXML) |
| 99 | + } |
| 100 | + |
| 101 | + func testTwoChoiceElementsEncode() throws { |
| 102 | + let twoChoiceElements = TwoChoiceElements(first: .int(1), second: .string("one")) |
| 103 | + let encoded = try XMLEncoder().encode(twoChoiceElements, withRootKey: "container") |
| 104 | + let expectedXML = "<container><int>1</int><string>one</string></container>" |
| 105 | + XCTAssertEqual(String(data: encoded, encoding: .utf8), expectedXML) |
| 106 | + } |
| 107 | +} |
0 commit comments