Skip to content

Commit

Permalink
Pull in tests (CoreOffice#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwetherfield committed Jul 26, 2019
1 parent 605c532 commit c33e0c7
Show file tree
Hide file tree
Showing 10 changed files with 2,009 additions and 1,581 deletions.
90 changes: 90 additions & 0 deletions Tests/XMLCoderTests/CompositeChoiceTests.swift
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")
}
98 changes: 98 additions & 0 deletions Tests/XMLCoderTests/SimpleChoiceTests.swift
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)
}
}
Loading

0 comments on commit c33e0c7

Please sign in to comment.