diff --git a/Tests/XMLCoderTests/Minimal/BoolTests.swift b/Tests/XMLCoderTests/Minimal/BoolTests.swift index 1d9803aa..ce41aaca 100644 --- a/Tests/XMLCoderTests/Minimal/BoolTests.swift +++ b/Tests/XMLCoderTests/Minimal/BoolTests.swift @@ -76,6 +76,7 @@ class BoolTests: XCTestCase { } static var allTests = [ + ("testMissing", testMissing), ("testAttribute", testAttribute), ("testElement", testElement), ] diff --git a/Tests/XMLCoderTests/Minimal/DataTests.swift b/Tests/XMLCoderTests/Minimal/DataTests.swift index 695d3e3a..54934cf0 100644 --- a/Tests/XMLCoderTests/Minimal/DataTests.swift +++ b/Tests/XMLCoderTests/Minimal/DataTests.swift @@ -76,8 +76,129 @@ class DataTests: XCTestCase { } } + func testStrategy() throws { + let decoder = XMLDecoder() + let encoder = XMLEncoder() + + encoder.outputFormatting = [.prettyPrinted] + + for (value, xmlString) in values { + let xmlString = + """ + + \(xmlString) + + """ + let xmlData = xmlString.data(using: .utf8)! + + let decoded = try decoder.decode(Container.self, from: xmlData) + XCTAssertEqual(decoded.value, value) + + let encoded = try encoder.encode(decoded, withRootKey: "container") + XCTAssertEqual(String(data: encoded, encoding: .utf8)!, xmlString) + } + } + + func testKeyFormated() throws { + let decoder = XMLDecoder() + let encoder = XMLEncoder() + + decoder.dataDecodingStrategy = .keyFormatted { $0.stringValue.data(using: .utf8) } + + encoder.outputFormatting = [.prettyPrinted] + + for (_, xmlString) in values { + let xmlString = + """ + + \(xmlString) + + """ + let xmlData = xmlString.data(using: .utf8)! + + let decoded = try decoder.decode(Container.self, from: xmlData) + + XCTAssertEqual(decoded.value, Data("value".utf8)) + } + } + + func testKeyFormatedError() throws { + let decoder = XMLDecoder() + let encoder = XMLEncoder() + + decoder.dataDecodingStrategy = .keyFormatted { codingKey in + return Data(base64Encoded: codingKey.stringValue) + } + + encoder.outputFormatting = [.prettyPrinted] + + for (_, xmlString) in values { + let xmlString = + """ + + \(xmlString) + + """ + let xmlData = xmlString.data(using: .utf8)! + + XCTAssertThrowsError(try decoder.decode(Container.self, from: xmlData)) + } + } + + func testKeyFormatedCouldNotDecodeError() throws { + let decoder = XMLDecoder() + let encoder = XMLEncoder() + + decoder.dataDecodingStrategy = .keyFormatted { codingKey in + return Data(base64Encoded: codingKey.stringValue) + } + + encoder.outputFormatting = [.prettyPrinted] + + for (_, xmlString) in values { + let xmlString = + """ + + \(xmlString)0 + \(xmlString)0 + + """ + let xmlData = xmlString.data(using: .utf8)! + + XCTAssertThrowsError(try decoder.decode(Container.self, from: xmlData)) + } + } + + func testKeyFormatedNoPathError() throws { + let decoder = XMLDecoder() + let encoder = XMLEncoder() + + decoder.dataDecodingStrategy = .keyFormatted { codingKey in + return Data(base64Encoded: codingKey.stringValue) + } + + encoder.outputFormatting = [.prettyPrinted] + + for (_, _) in values { + let xmlString = + """ + + 12 + + """ + let xmlData = xmlString.data(using: .utf8)! + + 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), ] } diff --git a/Tests/XMLCoderTests/Minimal/DateTests.swift b/Tests/XMLCoderTests/Minimal/DateTests.swift index ec73ab7e..6df21095 100644 --- a/Tests/XMLCoderTests/Minimal/DateTests.swift +++ b/Tests/XMLCoderTests/Minimal/DateTests.swift @@ -78,8 +78,88 @@ class DateTests: XCTestCase { } } + func testKeyFormatedError() throws { + let decoder = XMLDecoder() + let encoder = XMLEncoder() + + decoder.dateDecodingStrategy = .keyFormatted { _ in + let formatter = DateFormatter() + formatter.dateFormat = "value" + return formatter + } + + encoder.outputFormatting = [.prettyPrinted] + + for (_, xmlString) in values { + let xmlString = + """ + + \(xmlString) + + """ + let xmlData = xmlString.data(using: .utf8)! + + XCTAssertThrowsError(try decoder.decode(Container.self, from: xmlData)) + } + } + + func testKeyFormatedCouldNotDecodeError() throws { + let decoder = XMLDecoder() + let encoder = XMLEncoder() + + decoder.dateDecodingStrategy = .keyFormatted { _ in + let formatter = DateFormatter() + formatter.dateFormat = "value" + return formatter + } + + encoder.outputFormatting = [.prettyPrinted] + + for (_, xmlString) in values { + let xmlString = + """ + + \(xmlString) + \(xmlString) + + """ + let xmlData = xmlString.data(using: .utf8)! + + XCTAssertThrowsError(try decoder.decode(Container.self, from: xmlData)) + } + } + + func testKeyFormatedNoPathError() throws { + let decoder = XMLDecoder() + let encoder = XMLEncoder() + + decoder.dateDecodingStrategy = .keyFormatted { _ in + let formatter = DateFormatter() + formatter.dateFormat = "value" + return formatter + } + + encoder.outputFormatting = [.prettyPrinted] + + for (_, _) in values { + let xmlString = + """ + + 12 + + """ + let xmlData = xmlString.data(using: .utf8)! + + XCTAssertThrowsError(try decoder.decode(Container.self, from: xmlData)) + } + } + static var allTests = [ + ("testMissing", testMissing), ("testAttribute", testAttribute), ("testElement", testElement), + ("testKeyFormatedError", testKeyFormatedError), + ("testKeyFormatedCouldNotDecodeError", testKeyFormatedCouldNotDecodeError), + ("testKeyFormatedNoPathError", testKeyFormatedNoPathError), ] } diff --git a/Tests/XMLCoderTests/Minimal/DecimalTests.swift b/Tests/XMLCoderTests/Minimal/DecimalTests.swift index 6cdcc5a2..79e951a1 100644 --- a/Tests/XMLCoderTests/Minimal/DecimalTests.swift +++ b/Tests/XMLCoderTests/Minimal/DecimalTests.swift @@ -77,6 +77,7 @@ class DecimalTests: XCTestCase { } static var allTests = [ + ("testMissing", testMissing), ("testAttribute", testAttribute), ("testElement", testElement), ] diff --git a/Tests/XMLCoderTests/Minimal/FloatTests.swift b/Tests/XMLCoderTests/Minimal/FloatTests.swift index 5455e268..6e00ac5c 100644 --- a/Tests/XMLCoderTests/Minimal/FloatTests.swift +++ b/Tests/XMLCoderTests/Minimal/FloatTests.swift @@ -77,6 +77,7 @@ class FloatTests: XCTestCase { } static var allTests = [ + ("testMissing", testMissing), ("testAttribute", testAttribute), ("testElement", testElement), ] diff --git a/Tests/XMLCoderTests/Minimal/IntTests.swift b/Tests/XMLCoderTests/Minimal/IntTests.swift index 7710b103..41a84fc6 100644 --- a/Tests/XMLCoderTests/Minimal/IntTests.swift +++ b/Tests/XMLCoderTests/Minimal/IntTests.swift @@ -76,6 +76,7 @@ class IntTests: XCTestCase { } static var allTests = [ + ("testMissing", testMissing), ("testAttribute", testAttribute), ("testElement", testElement), ] diff --git a/Tests/XMLCoderTests/Minimal/StringTests.swift b/Tests/XMLCoderTests/Minimal/StringTests.swift index 9c9766f7..caedfe10 100644 --- a/Tests/XMLCoderTests/Minimal/StringTests.swift +++ b/Tests/XMLCoderTests/Minimal/StringTests.swift @@ -81,6 +81,7 @@ class StringTests: XCTestCase { } static var allTests = [ + ("testMissing", testMissing), ("testAttribute", testAttribute), ("testElement", testElement), ] diff --git a/Tests/XMLCoderTests/Minimal/UIntTests.swift b/Tests/XMLCoderTests/Minimal/UIntTests.swift index 3476cc0d..e7e3a77e 100644 --- a/Tests/XMLCoderTests/Minimal/UIntTests.swift +++ b/Tests/XMLCoderTests/Minimal/UIntTests.swift @@ -76,6 +76,7 @@ class UIntTests: XCTestCase { } static var allTests = [ + ("testMissing", testMissing), ("testAttribute", testAttribute), ("testElement", testElement), ] diff --git a/Tests/XMLCoderTests/Minimal/URLTests.swift b/Tests/XMLCoderTests/Minimal/URLTests.swift index f62fb3b1..3c4b57b2 100644 --- a/Tests/XMLCoderTests/Minimal/URLTests.swift +++ b/Tests/XMLCoderTests/Minimal/URLTests.swift @@ -76,6 +76,7 @@ class URLTests: XCTestCase { } static var allTests = [ + ("testMissing", testMissing), ("testAttribute", testAttribute), ("testElement", testElement), ]