forked from CoreOffice/XMLCoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
605c532
commit c33e0c7
Showing
10 changed files
with
2,009 additions
and
1,581 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// CompositeChoiceTests.swift | ||
// XMLCoderTests | ||
// | ||
// Created by James Bean on 7/15/19. | ||
// | ||
|
||
import XCTest | ||
import XMLCoder | ||
|
||
private struct IntWrapper: Codable, Equatable { | ||
let wrapped: Int | ||
} | ||
|
||
private struct StringWrapper: Codable, Equatable { | ||
let wrapped: String | ||
} | ||
|
||
private enum IntOrStringWrapper: Equatable { | ||
case int(IntWrapper) | ||
case string(StringWrapper) | ||
} | ||
|
||
extension IntOrStringWrapper: Codable { | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case int | ||
case string | ||
} | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
do { | ||
self = .int(try container.decode(IntWrapper.self, forKey: .int)) | ||
} catch { | ||
self = .string(try container.decode(StringWrapper.self, forKey: .string)) | ||
} | ||
} | ||
|
||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
switch self { | ||
case let .int(value): | ||
try container.encode(value, forKey: .int) | ||
case let .string(value): | ||
try container.encode(value, forKey: .string) | ||
} | ||
} | ||
} | ||
|
||
class CompositeChoiceTests: XCTestCase { | ||
|
||
func testIntOrStringWrapper() throws { | ||
let xml = """ | ||
<container> | ||
<string> | ||
<wrapped>A Word About Woke Times</wrapped> | ||
</string> | ||
</container> | ||
""" | ||
let result = try XMLDecoder().decode(IntOrStringWrapper.self, from: xml.data(using: .utf8)!) | ||
let expected = IntOrStringWrapper.string(StringWrapper(wrapped: "A Word About Woke Times")) | ||
XCTAssertEqual(result, expected) | ||
} | ||
|
||
func testArrayOfIntOrStringWrappers() throws { | ||
let xml = """ | ||
<container> | ||
<string> | ||
<wrapped>A Word About Woke Times</wrapped> | ||
</string> | ||
<int> | ||
<wrapped>9000</wrapped> | ||
</int> | ||
<string> | ||
<wrapped>A Word About Woke Tomes</wrapped> | ||
</string> | ||
</container> | ||
""" | ||
let result = try XMLDecoder().decode([IntOrStringWrapper].self, from: xml.data(using: .utf8)!) | ||
let expected: [IntOrStringWrapper] = [ | ||
.string(StringWrapper(wrapped: "A Word About Woke Times")), | ||
.int(IntWrapper(wrapped: 9000)), | ||
.string(StringWrapper(wrapped: "A Word About Woke Tomes")), | ||
] | ||
XCTAssertEqual(result, expected) | ||
} | ||
|
||
#warning("TODO: Add encoding and round-trip tests") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// | ||
// SimpleChoiceTests.swift | ||
// XMLCoderTests | ||
// | ||
// Created by James Bean on 7/15/19. | ||
// | ||
|
||
import XCTest | ||
import XMLCoder | ||
|
||
private enum IntOrString: Equatable { | ||
case int(Int) | ||
case string(String) | ||
} | ||
|
||
extension IntOrString: Codable { | ||
enum CodingKeys: String, CodingKey { | ||
case int | ||
case string | ||
} | ||
|
||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
switch self { | ||
case let .int(value): | ||
try container.encode(value, forKey: .int) | ||
case let .string(value): | ||
try container.encode(value, forKey: .string) | ||
} | ||
} | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
do { | ||
self = .int(try container.decode(Int.self, forKey: .int)) | ||
} catch { | ||
self = .string(try container.decode(String.self, forKey: .string)) | ||
} | ||
} | ||
} | ||
|
||
class SimpleChoiceTests: XCTestCase { | ||
|
||
func testIntOrStringIntDecoding() throws { | ||
let xml = "<int>42</int>" | ||
let result = try XMLDecoder().decode(IntOrString.self, from: xml.data(using: .utf8)!) | ||
let expected = IntOrString.int(42) | ||
XCTAssertEqual(result, expected) | ||
} | ||
|
||
func testIntOrStringStringDecoding() throws { | ||
let xml = "<string>forty-two</string>" | ||
let result = try XMLDecoder().decode(IntOrString.self, from: xml.data(using: .utf8)!) | ||
let expected = IntOrString.string("forty-two") | ||
XCTAssertEqual(result, expected) | ||
} | ||
|
||
func testIntOrStringArrayDecoding() throws { | ||
let xml = """ | ||
<container> | ||
<int>1</int> | ||
<string>two</string> | ||
<string>three</string> | ||
<int>4</int> | ||
<int>5</int> | ||
</container> | ||
""" | ||
let result = try XMLDecoder().decode([IntOrString].self, from: xml.data(using: .utf8)!) | ||
let expected: [IntOrString] = [ | ||
.int(1), | ||
.string("two"), | ||
.string("three"), | ||
.int(4), | ||
.int(5), | ||
] | ||
XCTAssertEqual(result, expected) | ||
} | ||
|
||
func testIntOrStringRoundTrip() throws { | ||
let original = IntOrString.int(5) | ||
let encoded = try XMLEncoder().encode(original, withRootKey: "container") | ||
let decoded = try XMLDecoder().decode(IntOrString.self, from: encoded) | ||
XCTAssertEqual(original, decoded) | ||
} | ||
|
||
func testIntOrStringArrayRoundTrip() throws { | ||
let original: [IntOrString] = [ | ||
.int(1), | ||
.string("two"), | ||
.string("three"), | ||
.int(4), | ||
.int(5), | ||
] | ||
let encoded = try XMLEncoder().encode(original, withRootKey: "container") | ||
let decoded = try XMLDecoder().decode([IntOrString].self, from: encoded) | ||
XCTAssertEqual(original, decoded) | ||
} | ||
} |
Oops, something went wrong.