Skip to content

Commit

Permalink
Add Linux support (CoreOffice#117)
Browse files Browse the repository at this point in the history
Only a few minor changes needed to be made for this to work properly on Linux.

- Upgraded to Swift 5.0.1
- There was a logical error in XMLStackParser causing a crash on forced unwrapping
- Element names returned from the XMLParser on linux have the namespace coming after the name. I put a hack in to reverse them back.
- The allTests definitions are no longer relevant. Instead, I used `swift test --generate-linuxmain`
  • Loading branch information
drewag authored and Arjun Gupta committed Jun 26, 2020
1 parent f0d17d5 commit 951c611
Show file tree
Hide file tree
Showing 42 changed files with 911 additions and 229 deletions.
5 changes: 4 additions & 1 deletion .swiftlint.yml
Expand Up @@ -15,4 +15,7 @@ function_body_length:
- 50

type_name:
min_length: 1
min_length: 1

excluded:
- Tests/XMLCoderTests/XCTestManifests.swift
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -4,7 +4,7 @@ Encoder & Decoder for XML using Swift's `Codable` protocols.
[![Build Status](https://dev.azure.com/max0484/max/_apis/build/status/MaxDesiatov.XMLCoder?branchName=master)](https://dev.azure.com/max0484/max/_build/latest?definitionId=4&branchName=master)
[![Version](https://img.shields.io/cocoapods/v/XMLCoder.svg?style=flat)](https://cocoapods.org/pods/XMLCoder)
[![License](https://img.shields.io/cocoapods/l/XMLCoder.svg?style=flat)](https://cocoapods.org/pods/XMLCoder)
[![Platform](https://img.shields.io/cocoapods/p/XMLCoder.svg?style=flat)](https://cocoapods.org/pods/XMLCoder)
[![Platform](https://img.shields.io/badge/platform-watchos%20%7C%20ios%20%7C%20tvos%20%7C%20macos%20%7C%20linux-lightgrey.svg?style=flat)](https://cocoapods.org/pods/XMLCoder)
[![Coverage](https://img.shields.io/codecov/c/github/MaxDesiatov/XMLCoder/master.svg?style=flat)](https://codecov.io/gh/maxdesiatov/XMLCoder)

This package is a fork of the original
Expand Down Expand Up @@ -211,10 +211,15 @@ you can now set a property `trimValueWhitespaces` to `false` (the default value

### Requirements

**Apple Platforms**
- Xcode 10.0 or later
- Swift 4.2 or later
- iOS 9.0 / watchOS 2.0 / tvOS 9.0 / macOS 10.10 or later deployment targets

**Linux**
- Ubuntu 14.04 or Later
- Swift 5.0.1 or later

### Swift Package Manager

[Swift Package Manager](https://swift.org/package-manager/) is a tool for
Expand Down
7 changes: 6 additions & 1 deletion Sources/XMLCoder/Auxiliaries/XMLStackParser.swift
Expand Up @@ -44,7 +44,7 @@ class XMLStackParser: NSObject {
xmlParser.shouldProcessNamespaces = shouldProcessNamespaces
xmlParser.delegate = self

guard !xmlParser.parse(), root == nil else {
guard !xmlParser.parse() || root == nil else {
return root!
}

Expand Down Expand Up @@ -126,6 +126,11 @@ extension XMLStackParser: XMLParserDelegate {
namespaceURI: String?,
qualifiedName: String?,
attributes attributeDict: [String: String] = [:]) {
#if os(Linux)
// For some reason, element names on linux are coming out with the namespace after the name
// https://bugs.swift.org/browse/SR-11191
let elementName = elementName.components(separatedBy: ":").reversed().joined(separator: ":")
#endif
let attributes = attributeDict.map { key, value in
Attribute(key: key, value: value)
}
Expand Down
15 changes: 6 additions & 9 deletions Tests/LinuxMain.swift
@@ -1,11 +1,8 @@
import XCTest
@testable import XMLCoderTests

XCTMain([
testCase(RelationshipsTest.allTests),
testCase(BreakfastTest.allTests),
testCase(NodeEncodingStrategyTests.allTests),
testCase(BooksTest.allTests),
testCase(NoteTest.allTests),
testCase(PlantTest.allTests),
])
import XMLCoderTests

var tests = [XCTestCaseEntry]()
tests += XMLCoderTests.__allTests()

XCTMain(tests)
5 changes: 0 additions & 5 deletions Tests/XMLCoderTests/AttributedEnumIntrinsicTest.swift
Expand Up @@ -118,9 +118,4 @@ final class AttributedEnumIntrinsicTest: XCTestCase {
// XCTAssertEqual(foo.number[0].type, FooEnum.string("ABC"))
// XCTAssertEqual(foo.number[1].type, FooEnum.int(123))
// }

static var allTests = [
("testEncode", testEncode),
// ("testDecode", testDecode),
]
}
6 changes: 0 additions & 6 deletions Tests/XMLCoderTests/AttributedIntrinsicTest.swift
Expand Up @@ -300,10 +300,4 @@ final class AttributedIntrinsicTest: XCTestCase {
XCTAssertEqual(foo.valueElement, "blah")
XCTAssertNil(foo.value)
}

static var allTests = [
("testEncode", testEncode),
("testDecode", testDecode),
("testDecodePreview", testDecodePreview),
]
}
19 changes: 0 additions & 19 deletions Tests/XMLCoderTests/BenchmarkTests.swift
Expand Up @@ -221,23 +221,4 @@ class BenchmarkTests: XCTestCase {
self.measure { closure() }
}
}

static var allTests = [
("testEncodeNulls", testEncodeNulls),
("testDecodeNulls", testDecodeNulls),
("testEncodeBools", testEncodeBools),
("testDecodeBools", testDecodeBools),
("testEncodeInts", testEncodeInts),
("testDecodeInts", testDecodeInts),
("testEncodeUInts", testEncodeUInts),
("testDecodeUInts", testDecodeUInts),
("testEncodeFloats", testEncodeFloats),
("testDecodeFloats", testDecodeFloats),
("testEncodeDecimals", testEncodeDecimals),
("testDecodeDecimals", testDecodeDecimals),
("testEncodeArrays", testEncodeArrays),
("testDecodeArrays", testDecodeArrays),
("testEncodeDictionaries", testEncodeDictionaries),
("testDecodeDictionaries", testDecodeDictionaries),
]
}
4 changes: 0 additions & 4 deletions Tests/XMLCoderTests/BooksTest.swift
Expand Up @@ -242,8 +242,4 @@ final class BooksTest: XCTestCase {
let catalog2 = try decoder.decode(Catalog.self, from: data)
XCTAssertEqual(catalog1, catalog2)
}

static var allTests = [
("testBookXML", testBookXML),
]
}
4 changes: 0 additions & 4 deletions Tests/XMLCoderTests/BreakfastTest.swift
Expand Up @@ -70,8 +70,4 @@ final class BreakfastTest: XCTestCase {
let menu2 = try decoder.decode(Menu.self, from: data)
XCTAssertEqual(menu1, menu2)
}

static var allTests = [
("testXML", testXML),
]
}
4 changes: 0 additions & 4 deletions Tests/XMLCoderTests/CDTest.swift
Expand Up @@ -60,8 +60,4 @@ final class CDTest: XCTestCase {

XCTAssertEqual(cdCatalog1, cdCatalog2)
}

static var allTests = [
("testXML", testXML),
]
}
4 changes: 0 additions & 4 deletions Tests/XMLCoderTests/ClassTests.swift
Expand Up @@ -108,8 +108,4 @@ class ClassTests: XCTestCase {

XCTAssertEqual(encoded, xmlData)
}

static var allTests = [
("testEmpty", testEmpty),
]
}
6 changes: 0 additions & 6 deletions Tests/XMLCoderTests/DynamicNodeDecodingTest.swift
Expand Up @@ -217,10 +217,4 @@ final class DynamicNodeDecodingTest: XCTestCase {
let test = try decoder.decode(TestStruct.self, from: overlappingKeys)
XCTAssertEqual(test, TestStruct(attribute: 123, element: "StringValue"))
}

static var allTests = [
("testDecode", testDecode),
("testStrategyPriority", testStrategyPriority),
("testOverlappingKeys", testOverlappingKeys),
]
}
6 changes: 0 additions & 6 deletions Tests/XMLCoderTests/DynamicNodeEncodingTest.swift
Expand Up @@ -277,10 +277,4 @@ final class DynamicNodeEncodingTest: XCTestCase {
encoding: "UTF-8"))
XCTAssertEqual(String(data: data, encoding: .utf8)!, libraryXMLYNStrategy)
}

static var allTests = [
("testEncode", testEncode),
("testDecode", testDecode),
("testEncodeDecode", testEncodeDecode),
]
}
25 changes: 21 additions & 4 deletions Tests/XMLCoderTests/ErrorContextTest.swift
Expand Up @@ -29,11 +29,21 @@ final class ErrorContextTest: XCTestCase {
return
}

#if os(Linux)
// XML Parser returns a different column on Linux
// https://bugs.swift.org/browse/SR-11192
XCTAssertEqual(ctx.debugDescription, """
\(underlying.localizedDescription) \
at line 1, column 7:
`ah //>`
""")
#else
XCTAssertEqual(ctx.debugDescription, """
\(underlying.localizedDescription) \
at line 1, column 2:
`<blah `
""")
#endif
}
}

Expand All @@ -60,12 +70,23 @@ final class ErrorContextTest: XCTestCase {
return
}

#if os(Linux)
// XML Parser returns a different column on Linux
// https://bugs.swift.org/browse/SR-11192
XCTAssertEqual(ctx.debugDescription, """
\(underlying.localizedDescription) \
at line 4, column 1:
`blah>
<c`
""")
#else
XCTAssertEqual(ctx.debugDescription, """
\(underlying.localizedDescription) \
at line 3, column 8:
`blah>
<c`
""")
#endif
}
}

Expand Down Expand Up @@ -99,8 +120,4 @@ final class ErrorContextTest: XCTestCase {
""")
}
}

static var allTests = [
("testErrorContext", testErrorContext),
]
}
6 changes: 0 additions & 6 deletions Tests/XMLCoderTests/Minimal/BoolTests.swift
Expand Up @@ -74,10 +74,4 @@ class BoolTests: XCTestCase {
XCTAssertEqual(String(data: encoded, encoding: .utf8)!, xmlString)
}
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
]
}
11 changes: 1 addition & 10 deletions Tests/XMLCoderTests/Minimal/BoxTreeTests.swift
Expand Up @@ -30,16 +30,7 @@ class BoxTreeTests: XCTestCase {
)

let boxTree = root.transformToBoxTree()

guard let foo = boxTree.elements["foo"] as? UnkeyedBox else {
XCTAssert(
false,
"""
flattened.elements["foo"] is not an UnkeyedBox
"""
)
return
}
let foo = boxTree.elements["foo"]

XCTAssertEqual(foo.count, 2)
}
Expand Down
10 changes: 0 additions & 10 deletions Tests/XMLCoderTests/Minimal/DataTests.swift
Expand Up @@ -191,14 +191,4 @@ class DataTests: XCTestCase {
XCTAssertThrowsError(try decoder.decode(Container.self, from: xmlData))
}
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
("testKeyFormated", testKeyFormated),
("testKeyFormatedError", testKeyFormatedError),
("testKeyFormatedCouldNotDecodeError", testKeyFormatedCouldNotDecodeError),
("testKeyFormatedNoPathError", testKeyFormatedNoPathError),
]
}
9 changes: 0 additions & 9 deletions Tests/XMLCoderTests/Minimal/DateTests.swift
Expand Up @@ -153,13 +153,4 @@ class DateTests: XCTestCase {
XCTAssertThrowsError(try decoder.decode(Container.self, from: xmlData))
}
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
("testKeyFormatedError", testKeyFormatedError),
("testKeyFormatedCouldNotDecodeError", testKeyFormatedCouldNotDecodeError),
("testKeyFormatedNoPathError", testKeyFormatedNoPathError),
]
}
6 changes: 0 additions & 6 deletions Tests/XMLCoderTests/Minimal/DecimalTests.swift
Expand Up @@ -75,10 +75,4 @@ class DecimalTests: XCTestCase {
XCTAssertEqual(String(data: encoded, encoding: .utf8)!, xmlString)
}
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
]
}
5 changes: 0 additions & 5 deletions Tests/XMLCoderTests/Minimal/EmptyTests.swift
Expand Up @@ -42,9 +42,4 @@ class EmptyTests: XCTestCase {
let encoded = try encoder.encode(Container(), withRootKey: "container")
XCTAssertEqual(String(data: encoded, encoding: .utf8)!, "<container />")
}

static var allTests = [
("testAttribute", testAttribute),
("testElement", testElement),
]
}
6 changes: 0 additions & 6 deletions Tests/XMLCoderTests/Minimal/FloatTests.swift
Expand Up @@ -75,10 +75,4 @@ class FloatTests: XCTestCase {
XCTAssertEqual(String(data: encoded, encoding: .utf8)!, xmlString)
}
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
]
}
6 changes: 0 additions & 6 deletions Tests/XMLCoderTests/Minimal/IntTests.swift
Expand Up @@ -74,10 +74,4 @@ class IntTests: XCTestCase {
XCTAssertEqual(String(data: encoded, encoding: .utf8)!, xmlString)
}
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
]
}
6 changes: 0 additions & 6 deletions Tests/XMLCoderTests/Minimal/KeyedIntTests.swift
Expand Up @@ -193,10 +193,4 @@ class KeyedIntTests: XCTestCase {
try testElement(ContainerUInt32.self)
try testElement(ContainerUInt64.self)
}

static var allTests = [
("testIntegerTypeMissing", testIntegerTypeMissing),
("testIntegerTypeAttribute", testIntegerTypeAttribute),
("testIntegerTypeElement", testIntegerTypeElement),
]
}
8 changes: 0 additions & 8 deletions Tests/XMLCoderTests/Minimal/KeyedTests.swift
Expand Up @@ -149,12 +149,4 @@ class KeyedTests: XCTestCase {

XCTAssertEqual(decoded.valUe, ["foo": 12])
}

static var allTests = [
("testEmpty", testEmpty),
("testSingleElement", testSingleElement),
("testMultiElement", testMultiElement),
("testAttribute", testAttribute),
("testConvertFromSnakeCase", testConvertFromSnakeCase),
]
}
5 changes: 0 additions & 5 deletions Tests/XMLCoderTests/Minimal/NullTests.swift
Expand Up @@ -52,9 +52,4 @@ class NullTests: XCTestCase {
let encoded = try encoder.encode(decoded, withRootKey: "container")
XCTAssertEqual(String(data: encoded, encoding: .utf8)!, xmlString)
}

static var allTests = [
("testAttribute", testAttribute),
("testElement", testElement),
]
}
4 changes: 0 additions & 4 deletions Tests/XMLCoderTests/Minimal/OptionalTests.swift
Expand Up @@ -73,8 +73,4 @@ class OptionalTests: XCTestCase {
let decoded2 = try decoder.decode(DecodeIfPresent.self, from: xml)
XCTAssertEqual(decoded2, DecodeIfPresent())
}

static var allTests = [
("testMissing", testMissing),
]
}
6 changes: 0 additions & 6 deletions Tests/XMLCoderTests/Minimal/StringTests.swift
Expand Up @@ -79,10 +79,4 @@ class StringTests: XCTestCase {
XCTAssertEqual(String(data: encoded, encoding: .utf8)!, xmlString)
}
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
]
}
6 changes: 0 additions & 6 deletions Tests/XMLCoderTests/Minimal/UIntTests.swift
Expand Up @@ -74,10 +74,4 @@ class UIntTests: XCTestCase {
XCTAssertEqual(String(data: encoded, encoding: .utf8)!, xmlString)
}
}

static var allTests = [
("testMissing", testMissing),
("testAttribute", testAttribute),
("testElement", testElement),
]
}

0 comments on commit 951c611

Please sign in to comment.