From fec4900d70373cf91c0f80fe405f7cb02ccf71ca Mon Sep 17 00:00:00 2001 From: Vincent Esche Date: Mon, 24 Dec 2018 19:07:21 +0100 Subject: [PATCH] Improve code coverage of auxiliary types (#43) * Add unit tests for `XMLKey` * Add unit tests for `XMLHeader` * Add unit tests for `XMLStackParser` * Add unit tests for `XMLElement` * Delete a bunch of dead code from `XMLElement` --- Sources/XMLCoder/Auxiliaries/XMLElement.swift | 12 +--- Sources/XMLCoder/Auxiliaries/XMLHeader.swift | 14 +++-- Sources/XMLCoder/Auxiliaries/XMLKey.swift | 13 ++-- .../Auxiliary/XMLElementTests.swift | 51 ++++++++++++++++ .../Auxiliary/XMLHeaderTests.swift | 61 +++++++++++++++++++ .../XMLCoderTests/Auxiliary/XMLKeyTests.swift | 29 +++++++++ .../Auxiliary/XMLStackParserTests.swift | 9 +++ XMLCoder.xcodeproj/project.pbxproj | 12 ++++ 8 files changed, 180 insertions(+), 21 deletions(-) create mode 100644 Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift create mode 100644 Tests/XMLCoderTests/Auxiliary/XMLHeaderTests.swift create mode 100644 Tests/XMLCoderTests/Auxiliary/XMLKeyTests.swift diff --git a/Sources/XMLCoder/Auxiliaries/XMLElement.swift b/Sources/XMLCoder/Auxiliaries/XMLElement.swift index 9280927c..86de9391 100644 --- a/Sources/XMLCoder/Auxiliaries/XMLElement.swift +++ b/Sources/XMLCoder/Auxiliaries/XMLElement.swift @@ -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: @@ -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) { @@ -182,8 +178,6 @@ struct _XMLElement { formatSortedXMLAttributes(&string) return } - formatUnsortedXMLAttributes(&string) - return } formatUnsortedXMLAttributes(&string) } @@ -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) } diff --git a/Sources/XMLCoder/Auxiliaries/XMLHeader.swift b/Sources/XMLCoder/Auxiliaries/XMLHeader.swift index 4cb56f8d..9209c14f 100644 --- a/Sources/XMLCoder/Auxiliaries/XMLHeader.swift +++ b/Sources/XMLCoder/Auxiliaries/XMLHeader.swift @@ -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? @@ -30,20 +32,22 @@ public struct XMLHeader { return nil } - var string = "\n" + string += "?>\n" + + return string } } diff --git a/Sources/XMLCoder/Auxiliaries/XMLKey.swift b/Sources/XMLCoder/Auxiliaries/XMLKey.swift index ec843042..7c8d9832 100644 --- a/Sources/XMLCoder/Auxiliaries/XMLKey.swift +++ b/Sources/XMLCoder/Auxiliaries/XMLKey.swift @@ -14,13 +14,11 @@ 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?) { @@ -28,9 +26,12 @@ struct _XMLKey: CodingKey { 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")! diff --git a/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift b/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift new file mode 100644 index 00000000..71f9e302 --- /dev/null +++ b/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, [:]) + } +} diff --git a/Tests/XMLCoderTests/Auxiliary/XMLHeaderTests.swift b/Tests/XMLCoderTests/Auxiliary/XMLHeaderTests.swift new file mode 100644 index 00000000..518ba06d --- /dev/null +++ b/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(), "\n") + + let versionEncoding = XMLHeader(version: 1.0, encoding: "UTF-8") + XCTAssertEqual(versionEncoding.toXML(), "\n") + + let versionEncodingStandalone = XMLHeader(version: 1.0, encoding: "UTF-8", standalone: "yes") + XCTAssertEqual(versionEncodingStandalone.toXML(), "\n") + } +} diff --git a/Tests/XMLCoderTests/Auxiliary/XMLKeyTests.swift b/Tests/XMLCoderTests/Auxiliary/XMLKeyTests.swift new file mode 100644 index 00000000..0719b2af --- /dev/null +++ b/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) + } +} diff --git a/Tests/XMLCoderTests/Auxiliary/XMLStackParserTests.swift b/Tests/XMLCoderTests/Auxiliary/XMLStackParserTests.swift index 1da9bd09..70c548ff 100644 --- a/Tests/XMLCoderTests/Auxiliary/XMLStackParserTests.swift +++ b/Tests/XMLCoderTests/Auxiliary/XMLStackParserTests.swift @@ -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)) + } } diff --git a/XMLCoder.xcodeproj/project.pbxproj b/XMLCoder.xcodeproj/project.pbxproj index c76b16d6..ec7967f8 100644 --- a/XMLCoder.xcodeproj/project.pbxproj +++ b/XMLCoder.xcodeproj/project.pbxproj @@ -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 */; }; @@ -115,6 +118,9 @@ BF63EF0721CD7AF8001D38C5 /* URLBoxTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLBoxTests.swift; sourceTree = ""; }; BF63EF0921CD7C1A001D38C5 /* URLTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLTests.swift; sourceTree = ""; }; BF63EF0B21CD7F28001D38C5 /* EmptyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyTests.swift; sourceTree = ""; }; + BF63EF6621D0F874001D38C5 /* XMLKeyTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XMLKeyTests.swift; sourceTree = ""; }; + BF63EF6821D0FDB5001D38C5 /* XMLHeaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XMLHeaderTests.swift; sourceTree = ""; }; + BF63EF6A21D10284001D38C5 /* XMLElementTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XMLElementTests.swift; sourceTree = ""; }; BF94579E21CBB497005ACFDE /* NullBox.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NullBox.swift; sourceTree = ""; }; BF94579F21CBB497005ACFDE /* KeyedBox.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyedBox.swift; sourceTree = ""; }; BF9457A021CBB497005ACFDE /* UnkeyedBox.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnkeyedBox.swift; sourceTree = ""; }; @@ -205,7 +211,10 @@ BF63EEFE21CCDEC1001D38C5 /* Auxiliary */ = { isa = PBXGroup; children = ( + BF63EF6621D0F874001D38C5 /* XMLKeyTests.swift */, BF63EEFF21CCDED2001D38C5 /* XMLStackParserTests.swift */, + BF63EF6821D0FDB5001D38C5 /* XMLHeaderTests.swift */, + BF63EF6A21D10284001D38C5 /* XMLElementTests.swift */, ); path = Auxiliary; sourceTree = ""; @@ -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 */, @@ -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 */,