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

Improve code coverage of auxiliary types #43

Merged
merged 5 commits into from Dec 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 2 additions & 10 deletions Sources/XMLCoder/Auxiliaries/XMLElement.swift
Expand Up @@ -41,7 +41,7 @@ struct _XMLElement {
return (key, value)
})

let elementsByKey: [(String, [_XMLElement])] = box.elements.map { key, box in
elements = Dictionary(uniqueKeysWithValues: box.elements.map { key, box in
switch box {
case let unkeyedBox as UnkeyedBox:
// This basically injects the unkeyed children directly into self:
Expand All @@ -56,11 +56,7 @@ struct _XMLElement {
case let box:
preconditionFailure("Unclassified box: \(type(of: box))")
}
}

elements = Dictionary(elementsByKey) { existingElements, newElements in
existingElements + newElements
}
})
}

init(key: String, box: SimpleBox) {
Expand Down Expand Up @@ -182,8 +178,6 @@ struct _XMLElement {
formatSortedXMLAttributes(&string)
return
}
formatUnsortedXMLAttributes(&string)
return
}
formatUnsortedXMLAttributes(&string)
}
Expand All @@ -194,8 +188,6 @@ struct _XMLElement {
formatSortedXMLElements(&string, level, cdata, formatting, prettyPrinted)
return
}
formatUnsortedXMLElements(&string, level, cdata, formatting, prettyPrinted)
return
}
formatUnsortedXMLElements(&string, level, cdata, formatting, prettyPrinted)
}
Expand Down
14 changes: 9 additions & 5 deletions Sources/XMLCoder/Auxiliaries/XMLHeader.swift
Expand Up @@ -10,8 +10,10 @@ import Foundation
public struct XMLHeader {
/// the XML standard that the produced document conforms to.
public let version: Double?

/// the encoding standard used to represent the characters in the produced document.
public let encoding: String?

/// indicates whether a document relies on information from an external source.
public let standalone: String?

Expand All @@ -30,20 +32,22 @@ public struct XMLHeader {
return nil
}

var string = "<?xml "
var string = "<?xml"

if let version = version {
string += "version=\"\(version)\" "
string += " version=\"\(version)\""
}

if let encoding = encoding {
string += "encoding=\"\(encoding)\" "
string += " encoding=\"\(encoding)\""
}

if let standalone = standalone {
string += "standalone=\"\(standalone)\""
string += " standalone=\"\(standalone)\""
}

return string.trimmingCharacters(in: .whitespaces) + "?>\n"
string += "?>\n"

return string
}
}
13 changes: 7 additions & 6 deletions Sources/XMLCoder/Auxiliaries/XMLKey.swift
Expand Up @@ -14,23 +14,24 @@ struct _XMLKey: CodingKey {
public let intValue: Int?

public init?(stringValue: String) {
self.stringValue = stringValue
intValue = nil
self.init(key: stringValue)
}

public init?(intValue: Int) {
stringValue = "\(intValue)"
self.intValue = intValue
self.init(index: intValue)
}

public init(stringValue: String, intValue: Int?) {
self.stringValue = stringValue
self.intValue = intValue
}

init(key: String) {
self.init(stringValue: key, intValue: nil)
}

init(index: Int) {
stringValue = "Index \(index)"
intValue = index
self.init(stringValue: "\(index)", intValue: index)
}

static let `super` = _XMLKey(stringValue: "super")!
Expand Down
51 changes: 51 additions & 0 deletions Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift
@@ -0,0 +1,51 @@
//
// XMLElementTests.swift
// XMLCoderTests
//
// Created by Vincent Esche on 12/24/18.
//

import XCTest
@testable import XMLCoder

class XMLElementTests: XCTestCase {
func testInitNull() {
let null = _XMLElement(key: "foo")

XCTAssertEqual(null.key, "foo")
XCTAssertNil(null.value)
XCTAssertEqual(null.elements, [:])
XCTAssertEqual(null.attributes, [:])
}

func testInitUnkeyed() {
let keyed = _XMLElement(key: "foo", box: UnkeyedBox())

XCTAssertEqual(keyed.key, "foo")
XCTAssertNil(keyed.value)
debugPrint(keyed.elements)
XCTAssertEqual(keyed.elements, ["foo": []])
XCTAssertEqual(keyed.attributes, [:])
}

func testInitKeyed() {
let keyed = _XMLElement(key: "foo", box: KeyedBox(
elements: [:],
attributes: ["baz": NullBox(), "blee": IntBox(42)]
))

XCTAssertEqual(keyed.key, "foo")
XCTAssertNil(keyed.value)
XCTAssertEqual(keyed.elements, [:])
XCTAssertEqual(keyed.attributes, ["blee": "42"])
}

func testInitSimple() {
let keyed = _XMLElement(key: "foo", box: StringBox("bar"))

XCTAssertEqual(keyed.key, "foo")
XCTAssertEqual(keyed.value, "bar")
XCTAssertEqual(keyed.elements, [:])
XCTAssertEqual(keyed.attributes, [:])
}
}
61 changes: 61 additions & 0 deletions Tests/XMLCoderTests/Auxiliary/XMLHeaderTests.swift
@@ -0,0 +1,61 @@
//
// XMLHeaderTests.swift
// XMLCoderTests
//
// Created by Vincent Esche on 12/24/18.
//

import XCTest
@testable import XMLCoder

class XMLHeaderTests: XCTestCase {
func testInitVersionEncodingStandalone() {
let header = XMLHeader(version: 1.0, encoding: "UTF-8", standalone: "yes")
XCTAssertEqual(header.version, 1.0)
XCTAssertEqual(header.encoding, "UTF-8")
XCTAssertEqual(header.standalone, "yes")
}

func testInitVersionEncoding() {
let header = XMLHeader(version: 1.0, encoding: "UTF-8")
XCTAssertEqual(header.version, 1.0)
XCTAssertEqual(header.encoding, "UTF-8")
XCTAssertNil(header.standalone)
}

func testInitVersion() {
let header = XMLHeader(version: 1.0)
XCTAssertEqual(header.version, 1.0)
XCTAssertNil(header.encoding)
XCTAssertNil(header.standalone)
}

func testInit() {
let header = XMLHeader()
XCTAssertNil(header.version)
XCTAssertNil(header.encoding)
XCTAssertNil(header.standalone)
}

func testIsEmpty() {
let empty = XMLHeader()
XCTAssertTrue(empty.isEmpty())

let nonEmpty = XMLHeader(version: 1.0)
XCTAssertFalse(nonEmpty.isEmpty())
}

func testToXML() {
let empty = XMLHeader()
XCTAssertNil(empty.toXML())

let version = XMLHeader(version: 1.0)
XCTAssertEqual(version.toXML(), "<?xml version=\"1.0\"?>\n")

let versionEncoding = XMLHeader(version: 1.0, encoding: "UTF-8")
XCTAssertEqual(versionEncoding.toXML(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")

let versionEncodingStandalone = XMLHeader(version: 1.0, encoding: "UTF-8", standalone: "yes")
XCTAssertEqual(versionEncodingStandalone.toXML(), "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n")
}
}
29 changes: 29 additions & 0 deletions Tests/XMLCoderTests/Auxiliary/XMLKeyTests.swift
@@ -0,0 +1,29 @@
//
// XMLKeyTests.swift
// XMLCoderTests
//
// Created by Vincent Esche on 12/24/18.
//

import XCTest
@testable import XMLCoder

class XMLKeyTests: XCTestCase {
func testInitStringValue() {
let key = _XMLKey(stringValue: "foo")
XCTAssertEqual(key?.stringValue, "foo")
XCTAssertEqual(key?.intValue, nil)
}

func testInitIntValue() {
let key = _XMLKey(intValue: 42)
XCTAssertEqual(key?.stringValue, "42")
XCTAssertEqual(key?.intValue, 42)
}

func testInitStringValueIntValue() {
let key = _XMLKey(stringValue: "foo", intValue: 42)
XCTAssertEqual(key.stringValue, "foo")
XCTAssertEqual(key.intValue, 42)
}
}
9 changes: 9 additions & 0 deletions Tests/XMLCoderTests/Auxiliary/XMLStackParserTests.swift
Expand Up @@ -30,4 +30,13 @@ class XMLStackParserTests: XCTestCase {
)
XCTAssertEqual(root, expected)
}

func testParseWithThrow() throws {
let parser = _XMLStackParser()

let xmlString = "lorem ipsum"
let xmlData = xmlString.data(using: .utf8)!

XCTAssertThrowsError(try parser.parse(with: xmlData))
}
}
12 changes: 12 additions & 0 deletions XMLCoder.xcodeproj/project.pbxproj
Expand Up @@ -26,6 +26,9 @@
BF63EF0821CD7AF8001D38C5 /* URLBoxTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF63EF0721CD7AF8001D38C5 /* URLBoxTests.swift */; };
BF63EF0A21CD7C1A001D38C5 /* URLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF63EF0921CD7C1A001D38C5 /* URLTests.swift */; };
BF63EF0C21CD7F28001D38C5 /* EmptyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF63EF0B21CD7F28001D38C5 /* EmptyTests.swift */; };
BF63EF6721D0F874001D38C5 /* XMLKeyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF63EF6621D0F874001D38C5 /* XMLKeyTests.swift */; };
BF63EF6921D0FDB5001D38C5 /* XMLHeaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF63EF6821D0FDB5001D38C5 /* XMLHeaderTests.swift */; };
BF63EF6B21D10284001D38C5 /* XMLElementTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF63EF6A21D10284001D38C5 /* XMLElementTests.swift */; };
BF9457A821CBB498005ACFDE /* NullBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF94579E21CBB497005ACFDE /* NullBox.swift */; };
BF9457A921CBB498005ACFDE /* KeyedBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF94579F21CBB497005ACFDE /* KeyedBox.swift */; };
BF9457AA21CBB498005ACFDE /* UnkeyedBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF9457A021CBB497005ACFDE /* UnkeyedBox.swift */; };
Expand Down Expand Up @@ -115,6 +118,9 @@
BF63EF0721CD7AF8001D38C5 /* URLBoxTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLBoxTests.swift; sourceTree = "<group>"; };
BF63EF0921CD7C1A001D38C5 /* URLTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLTests.swift; sourceTree = "<group>"; };
BF63EF0B21CD7F28001D38C5 /* EmptyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyTests.swift; sourceTree = "<group>"; };
BF63EF6621D0F874001D38C5 /* XMLKeyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XMLKeyTests.swift; sourceTree = "<group>"; };
BF63EF6821D0FDB5001D38C5 /* XMLHeaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XMLHeaderTests.swift; sourceTree = "<group>"; };
BF63EF6A21D10284001D38C5 /* XMLElementTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XMLElementTests.swift; sourceTree = "<group>"; };
BF94579E21CBB497005ACFDE /* NullBox.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NullBox.swift; sourceTree = "<group>"; };
BF94579F21CBB497005ACFDE /* KeyedBox.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyedBox.swift; sourceTree = "<group>"; };
BF9457A021CBB497005ACFDE /* UnkeyedBox.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnkeyedBox.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -205,7 +211,10 @@
BF63EEFE21CCDEC1001D38C5 /* Auxiliary */ = {
isa = PBXGroup;
children = (
BF63EF6621D0F874001D38C5 /* XMLKeyTests.swift */,
BF63EEFF21CCDED2001D38C5 /* XMLStackParserTests.swift */,
BF63EF6821D0FDB5001D38C5 /* XMLHeaderTests.swift */,
BF63EF6A21D10284001D38C5 /* XMLElementTests.swift */,
);
path = Auxiliary;
sourceTree = "<group>";
Expand Down Expand Up @@ -511,7 +520,9 @@
BF9457F221CBB6BC005ACFDE /* UnkeyedTests.swift in Sources */,
BF9457F321CBB6BC005ACFDE /* DateTests.swift in Sources */,
BF9457D121CBB516005ACFDE /* UnkeyedBoxTests.swift in Sources */,
BF63EF6921D0FDB5001D38C5 /* XMLHeaderTests.swift in Sources */,
BF9457F021CBB6BC005ACFDE /* StringTests.swift in Sources */,
BF63EF6B21D10284001D38C5 /* XMLElementTests.swift in Sources */,
BF9457ED21CBB6BC005ACFDE /* BoolTests.swift in Sources */,
D1FC040521C7EF8200065B43 /* RJISample.swift in Sources */,
BF63EF0A21CD7C1A001D38C5 /* URLTests.swift in Sources */,
Expand All @@ -526,6 +537,7 @@
BF9457E021CBB68A005ACFDE /* DataBoxTests.swift in Sources */,
BF9457F521CBB6BC005ACFDE /* DecimalTests.swift in Sources */,
OBJ_83 /* CDTest.swift in Sources */,
BF63EF6721D0F874001D38C5 /* XMLKeyTests.swift in Sources */,
OBJ_85 /* NodeEncodingStrategyTests.swift in Sources */,
OBJ_86 /* NoteTest.swift in Sources */,
BF63EF0C21CD7F28001D38C5 /* EmptyTests.swift in Sources */,
Expand Down