Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Enum usage #112

Closed
wants to merge 14 commits into from
4 changes: 3 additions & 1 deletion Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ struct XMLCoderElement: Equatable {
string += "</\(key)>"
}
} else if !elements.isEmpty {
string += prettyPrinted ? ">\n" : ">"
if !key.isEmpty {
string += prettyPrinted ? ">\n" : ">"
}
formatXMLElements(formatting, &string, level, cdata, prettyPrinted)

string += indentation
Expand Down
173 changes: 173 additions & 0 deletions Tests/XMLCoderTests/IntOrStringTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
//
// IntOrStringTest.swift
// XMLCoderTests
//
// Created by Benjamin Wetherfield on 7/4/19.
//

import XCTest
@testable import XMLCoder

let stringXML = """
<?xml version="1.0" encoding="UTF-8"?>
<container><string>forty-two</string></container>
""".data(using: .utf8)!
let intXML = """
<?xml version="1.0" encoding="UTF-8"?>
<container><int>42</int></container>
""".data(using: .utf8)!

let explicitXML = """
<?xml version="1.0" encoding="UTF-8"?>
<container><intOrString><int>42</int></intOrString><intOrString><string>forty-two</string></intOrString></container>
""".data(using: .utf8)!

private struct IntOrStringArray: Equatable, Codable {
let element: [IntOrString]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

enum CodingKeys: String, CodingKey {
case element = ""
}
}

private struct IntOrStringExplicitArray: Equatable, Codable {
let intOrString: [IntOrString]
}

private struct IntOrStringContaining: Equatable, Codable {
let element: IntOrString

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

public func encode(to encoder: Encoder) throws {
try element.encode(to: encoder)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

public init(from decoder: Decoder) throws {
element = try IntOrString(from: decoder)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

public init(element: IntOrString) {
self.element = element
}
}

private enum IntOrString: Equatable, Codable {
private enum CodingKeys: String, CodingKey {
case string
case int
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
if let value = try values.decodeIfPresent(String.self, forKey: .string) {
self = .string(value)
return
} else if let value = try values.decodeIfPresent(Int.self, forKey: .int) {
self = .int(value)
return
} else {
throw DecodingError.dataCorrupted(DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "No coded value for string or int"
))
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case let .string(value):
try container.encode(value, forKey: .string)
case let .int(value):
try container.encode(value, forKey: .int)
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

case string(String)
case int(Int)
}

final class IntOrStringTest: XCTestCase {

private let string = IntOrStringContaining(element: IntOrString.string("forty-two"))
private let int = IntOrStringContaining(element: IntOrString.int(42))
private let array = IntOrStringArray(element: [.string("forty-two"), .int(42)])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

func testEncode() throws {
let encoder = XMLEncoder()
encoder.outputFormatting = []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

let header = XMLHeader(version: 1.0, encoding: "UTF-8")
let encodedString = try encoder.encode(string, withRootKey: "container", header: header)
let encodedInt = try encoder.encode(int, withRootKey: "container", header: header)
let stringXMLString = String(data: encodedString, encoding: .utf8)
let intXMLString = String(data: encodedInt, encoding: .utf8)

// Test string equivalency
let encodedStringXML = stringXMLString!.trimmingCharacters(in: .whitespacesAndNewlines)
let encodedIntXML = intXMLString!.trimmingCharacters(in: .whitespacesAndNewlines)
let originalStringXML = String(data: stringXML, encoding: .utf8)!.trimmingCharacters(in: .whitespacesAndNewlines)
let originalIntXML = String(data: intXML, encoding: .utf8)!.trimmingCharacters(in: .whitespacesAndNewlines)
XCTAssertEqual(encodedStringXML, originalStringXML)
XCTAssertEqual(encodedIntXML, originalIntXML)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

func testDecode() throws {
let decoder = XMLDecoder()
let decodedString = try decoder.decode(IntOrStringContaining.self, from: stringXML)
let decodedInt = try decoder.decode(IntOrStringContaining.self, from: intXML)
XCTAssertEqual(string, decodedString)
XCTAssertEqual(int, decodedInt)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

func testEncodeExplicitArray() throws {
let encoder = XMLEncoder()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

let explicitArray = IntOrStringExplicitArray(intOrString: [
IntOrString.int(42),
IntOrString.string("forty-two")
])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

let header = XMLHeader(version: 1.0, encoding: "UTF-8")
let encodedArray = try encoder.encode(explicitArray, withRootKey: "container", header: header)

let arrayXMLString = String(data: encodedArray, encoding: .utf8)!.trimmingCharacters(in: .whitespacesAndNewlines)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

XCTAssertEqual(arrayXMLString, String(data: explicitXML, encoding: .utf8)!.trimmingCharacters(in: .whitespacesAndNewlines))
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

func testDecodeExplicitArray() throws {
let decoder = XMLDecoder()
let decodedExplicit = try decoder.decode(IntOrStringExplicitArray.self, from: explicitXML)
print(decodedExplicit)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

let correct = IntOrStringExplicitArray(intOrString: [
IntOrString.int(42),
IntOrString.string("forty-two")
])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

XCTAssertEqual(correct, decodedExplicit)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

func testNonExplicitArray() throws {
let encoder = XMLEncoder()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

let header = XMLHeader(version: 1.0, encoding: "UTF-8")
let encodedArray = try encoder.encode(array, withRootKey: "container", header: header)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

let arrayXMLString = String(data: encodedArray, encoding: .utf8)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

print(arrayXMLString)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

let decoder = XMLDecoder()
let decodedStringArray = try decoder.decode(IntOrStringArray.self, from: stringXML)
print(decodedStringArray)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

static var allTests = [
("testEncode", testEncode),
("testDecode", testDecode),
("testEncodeExplicitArray", testEncodeExplicitArray),
("testDecodeExplicitArray", testDecodeExplicitArray),
("testNonExplicitArray", testNonExplicitArray)
]
}