-
Notifications
You must be signed in to change notification settings - Fork 104
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
[WIP] Enum usage #112
Changes from 9 commits
84ca446
02b6622
4bf611f
6fe722b
7fd7cd3
e65b6c0
82d93d6
8a7d80f
09be924
cf097cb
f449ec9
44ad73a
d136496
d0da962
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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] | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case element = "" | ||
} | ||
} | ||
|
||
private struct IntOrStringExplicitArray: Equatable, Codable { | ||
let intOrString: [IntOrString] | ||
} | ||
|
||
private struct IntOrStringContaining: Equatable, Codable { | ||
let element: IntOrString | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
)) | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)]) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = [] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
]) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
]) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
] | ||
} |
There was a problem hiding this comment.
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)