From ca836d00b7fa7a31eb7f0e8cc335112a736c63e8 Mon Sep 17 00:00:00 2001 From: John Woo Date: Mon, 2 Aug 2021 16:06:52 -0700 Subject: [PATCH 1/5] fix: encode element with empty key, empty element, and attributes --- .../XMLCoder/Auxiliaries/XMLCoderElement.swift | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift index c3a96673..f9872560 100644 --- a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift +++ b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift @@ -217,9 +217,16 @@ struct XMLCoderElement: Equatable { _ string: inout String, _ charactersEscapedInAttributes: [(String, String)] ) { + let actuallyAttributes = self.elements.filter { + $0.key.isEmpty && $0.elements.isEmpty && !$0.attributes.isEmpty + }.flatMap { + $0.attributes + } + let allAttributes = self.attributes + actuallyAttributes + let attributes = formatting.contains(.sortedKeys) ? - self.attributes.sorted(by: { $0.key < $1.key }) : - self.attributes + allAttributes.sorted(by: { $0.key < $1.key }) : + allAttributes formatXMLAttributes( from: attributes, into: &string, @@ -266,10 +273,9 @@ struct XMLCoderElement: Equatable { if !key.isEmpty { string += "<\(key)" + formatXMLAttributes(formatting, &string, escapedCharacters.attributes) } - formatXMLAttributes(formatting, &string, escapedCharacters.attributes) - if !elements.isEmpty { let prettyPrintElements = prettyPrinted && !containsTextNodes if !key.isEmpty { @@ -282,7 +288,9 @@ struct XMLCoderElement: Equatable { string += "" } } else { - string += " />" + if !key.isEmpty { + string += " />" + } } return string From bebb7126a1c4d180623bf8ce78848760075bff0e Mon Sep 17 00:00:00 2001 From: John Woo Date: Thu, 5 Aug 2021 11:57:15 -0700 Subject: [PATCH 2/5] Adding unit tests --- .../Auxiliaries/XMLCoderElement.swift | 6 +- .../Auxiliary/XMLElementTests.swift | 64 +++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift index f9872560..55434147 100644 --- a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift +++ b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift @@ -217,12 +217,12 @@ struct XMLCoderElement: Equatable { _ string: inout String, _ charactersEscapedInAttributes: [(String, String)] ) { - let actuallyAttributes = self.elements.filter { - $0.key.isEmpty && $0.elements.isEmpty && !$0.attributes.isEmpty + let attributesBelongingToContainer = self.elements.filter { + $0.key.isEmpty && !$0.attributes.isEmpty }.flatMap { $0.attributes } - let allAttributes = self.attributes + actuallyAttributes + let allAttributes = self.attributes + attributesBelongingToContainer let attributes = formatting.contains(.sortedKeys) ? allAttributes.sorted(by: { $0.key < $1.key }) : diff --git a/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift b/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift index fb828246..4b7394c6 100644 --- a/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift +++ b/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift @@ -64,4 +64,68 @@ class XMLElementTests: XCTestCase { XCTAssert(whitespaceElement2.isWhitespaceWithNoElements()) XCTAssert(whitespaceElement3.isWhitespaceWithNoElements()) } + + func testNestedElementWith_Namespace_Attribute() { + typealias Attribute = XMLCoderElement.Attribute + typealias Element = XMLCoderElement + let nested = Element(key: "Nested", + elements: [ + Element(key: "", + elements: [], + attributes: [ + Attribute(key: "xsi:someName", value: "nestedAttrValue") + ] + )], + attributes: [ + Attribute(key: "xmlns:xsi", value: "https://example.com") + ]) + let simpleScalarPropertiesInputNamespace = Attribute(key: "xmlns", value: "https://example.com") + let simpleScalarPropertiesInput = Element(key: "SimpleScalarPropertiesInput", + elements: [nested], + attributes: [simpleScalarPropertiesInputNamespace]) + + let result = simpleScalarPropertiesInput.toXMLString( + escapedCharacters: (elements: XMLEncoder().charactersEscapedInElements, attributes: XMLEncoder().charactersEscapedInAttributes), + formatting: [], + indentation: .spaces(4) + ) + + XCTAssertEqual(result, """ + + """) + } + + func testNestedElementWith_Namespace_Attribute_Element() { + typealias Attribute = XMLCoderElement.Attribute + typealias Element = XMLCoderElement + let nested = Element(key: "Nested", + elements: [ + Element(key: "", + elements: [ + Element(key: "nonAttrField", + elements: [Element(key: "", stringValue: "hello")], + attributes: []) + ], + attributes: [ + Attribute(key: "xsi:someName", value: "nestedAttrValue") + ] + )], + attributes: [ + Attribute(key: "xmlns:xsi", value: "https://example.com") + ]) + let simpleScalarPropertiesInputNamespace = Attribute(key: "xmlns", value: "https://example.com") + let simpleScalarPropertiesInput = Element(key: "SimpleScalarPropertiesInput", + elements: [nested], + attributes: [simpleScalarPropertiesInputNamespace]) + + let result = simpleScalarPropertiesInput.toXMLString( + escapedCharacters: (elements: XMLEncoder().charactersEscapedInElements, attributes: XMLEncoder().charactersEscapedInAttributes), + formatting: [], + indentation: .spaces(4) + ) + + XCTAssertEqual(result, """ + hello + """) + } } From 6df283e1d8e6cf4d04b8cd01e2108b435120ce19 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 5 Aug 2021 20:03:22 +0100 Subject: [PATCH 3/5] Silence line_length linter warning --- Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift b/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift index 4b7394c6..f5408265 100644 --- a/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift +++ b/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift @@ -89,7 +89,7 @@ class XMLElementTests: XCTestCase { formatting: [], indentation: .spaces(4) ) - + // swiftlint:disable line_length XCTAssertEqual(result, """ """) From 7a9982d6d7ccd9eb9ba14b5ddc1715c73d63f5bd Mon Sep 17 00:00:00 2001 From: John Woo Date: Thu, 5 Aug 2021 12:04:08 -0700 Subject: [PATCH 4/5] fix lint --- .../Auxiliary/XMLElementTests.swift | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift b/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift index 4b7394c6..83bc7f62 100644 --- a/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift +++ b/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift @@ -79,22 +79,23 @@ class XMLElementTests: XCTestCase { attributes: [ Attribute(key: "xmlns:xsi", value: "https://example.com") ]) - let simpleScalarPropertiesInputNamespace = Attribute(key: "xmlns", value: "https://example.com") - let simpleScalarPropertiesInput = Element(key: "SimpleScalarPropertiesInput", - elements: [nested], - attributes: [simpleScalarPropertiesInputNamespace]) - - let result = simpleScalarPropertiesInput.toXMLString( - escapedCharacters: (elements: XMLEncoder().charactersEscapedInElements, attributes: XMLEncoder().charactersEscapedInAttributes), + let inputNamespace = Attribute(key: "xmlns", value: "https://example.com") + let input = Element(key: "Input", + elements: [nested], + attributes: [inputNamespace]) + + let result = input.toXMLString( + escapedCharacters: (elements: XMLEncoder().charactersEscapedInElements, + attributes: XMLEncoder().charactersEscapedInAttributes), formatting: [], indentation: .spaces(4) ) XCTAssertEqual(result, """ - + """) } - + func testNestedElementWith_Namespace_Attribute_Element() { typealias Attribute = XMLCoderElement.Attribute typealias Element = XMLCoderElement @@ -113,19 +114,20 @@ class XMLElementTests: XCTestCase { attributes: [ Attribute(key: "xmlns:xsi", value: "https://example.com") ]) - let simpleScalarPropertiesInputNamespace = Attribute(key: "xmlns", value: "https://example.com") - let simpleScalarPropertiesInput = Element(key: "SimpleScalarPropertiesInput", - elements: [nested], - attributes: [simpleScalarPropertiesInputNamespace]) - - let result = simpleScalarPropertiesInput.toXMLString( - escapedCharacters: (elements: XMLEncoder().charactersEscapedInElements, attributes: XMLEncoder().charactersEscapedInAttributes), + let inputNamespace = Attribute(key: "xmlns", value: "https://example.com") + let input = Element(key: "Input", + elements: [nested], + attributes: [inputNamespace]) + + let result = input.toXMLString( + escapedCharacters: (elements: XMLEncoder().charactersEscapedInElements, + attributes: XMLEncoder().charactersEscapedInAttributes), formatting: [], indentation: .spaces(4) ) XCTAssertEqual(result, """ - hello + hello """) } } From bc89de7a9123cc3074a89e332516afe03d6f4dfc Mon Sep 17 00:00:00 2001 From: John Woo Date: Thu, 5 Aug 2021 12:05:20 -0700 Subject: [PATCH 5/5] reducing line size --- Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift b/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift index 693af4f5..83bc7f62 100644 --- a/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift +++ b/Tests/XMLCoderTests/Auxiliary/XMLElementTests.swift @@ -90,7 +90,7 @@ class XMLElementTests: XCTestCase { formatting: [], indentation: .spaces(4) ) - // swiftlint:disable line_length + XCTAssertEqual(result, """ """)