From f8a4c677b960a0e0aad501b43b624dff773175bb Mon Sep 17 00:00:00 2001 From: Hodovaniuk Matvii Date: Wed, 9 Jan 2019 16:47:37 +0200 Subject: [PATCH 1/4] Add test for Decode .keyFormatted --- Tests/XMLCoderTests/Minimal/DataTests.swift | 51 +++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/Tests/XMLCoderTests/Minimal/DataTests.swift b/Tests/XMLCoderTests/Minimal/DataTests.swift index 695d3e3a..6ccac859 100644 --- a/Tests/XMLCoderTests/Minimal/DataTests.swift +++ b/Tests/XMLCoderTests/Minimal/DataTests.swift @@ -76,8 +76,59 @@ 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 (value, xmlString) in values { + print(value) + print(xmlString) + let xmlString = + """ + + \(xmlString) + + """ + let xmlData = xmlString.data(using: .utf8)! + + let decoded = try decoder.decode(Container.self, from: xmlData) + print(Data("value".utf8)) + print("") + print(decoded.value) + XCTAssertEqual(decoded.value, Data("value".utf8)) + } + } + static var allTests = [ ("testAttribute", testAttribute), ("testElement", testElement), + ("testKeyFormated", testKeyFormated), ] } From 39517dbd64d9ff3a7175e678db9d9b335361ce61 Mon Sep 17 00:00:00 2001 From: Hodovaniuk Matvii Date: Wed, 9 Jan 2019 17:37:30 +0200 Subject: [PATCH 2/4] Add test for Decode .keyFormatted errors --- Tests/XMLCoderTests/Minimal/DataTests.swift | 81 +++++++++++++++++++-- 1 file changed, 75 insertions(+), 6 deletions(-) diff --git a/Tests/XMLCoderTests/Minimal/DataTests.swift b/Tests/XMLCoderTests/Minimal/DataTests.swift index 6ccac859..e022dc73 100644 --- a/Tests/XMLCoderTests/Minimal/DataTests.swift +++ b/Tests/XMLCoderTests/Minimal/DataTests.swift @@ -107,9 +107,7 @@ class DataTests: XCTestCase { encoder.outputFormatting = [.prettyPrinted] - for (value, xmlString) in values { - print(value) - print(xmlString) + for (_, xmlString) in values { let xmlString = """ @@ -119,16 +117,87 @@ class DataTests: XCTestCase { let xmlData = xmlString.data(using: .utf8)! let decoded = try decoder.decode(Container.self, from: xmlData) - print(Data("value".utf8)) - print("") - print(decoded.value) + 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 = [ ("testAttribute", testAttribute), ("testElement", testElement), ("testKeyFormated", testKeyFormated), + ("testKeyFormatedError", testKeyFormatedError), + ("testKeyFormatedCouldNotDecodeError", testKeyFormatedCouldNotDecodeError), + ("testKeyFormatedNoPathError", testKeyFormatedNoPathError), ] } From 7a90c411958c314da6d78f83678db57e7a62dc6f Mon Sep 17 00:00:00 2001 From: Hodovaniuk Matvii Date: Wed, 9 Jan 2019 19:23:52 +0200 Subject: [PATCH 3/4] Add test for Decode Date .keyFormatted errors --- Tests/XMLCoderTests/Minimal/DateTests.swift | 79 +++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/Tests/XMLCoderTests/Minimal/DateTests.swift b/Tests/XMLCoderTests/Minimal/DateTests.swift index ec73ab7e..d39bffab 100644 --- a/Tests/XMLCoderTests/Minimal/DateTests.swift +++ b/Tests/XMLCoderTests/Minimal/DateTests.swift @@ -78,8 +78,87 @@ 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 = [ ("testAttribute", testAttribute), ("testElement", testElement), + ("testKeyFormatedError", testKeyFormatedError), + ("testKeyFormatedCouldNotDecodeError", testKeyFormatedCouldNotDecodeError), + ("testKeyFormatedNoPathError", testKeyFormatedNoPathError), ] } From 071901a0b5dfe25172bbdf6568d464d7dada8b10 Mon Sep 17 00:00:00 2001 From: Hodovaniuk Matvii Date: Thu, 10 Jan 2019 10:13:35 +0200 Subject: [PATCH 4/4] Add to allTests array testMissing function --- Tests/XMLCoderTests/Minimal/BoolTests.swift | 1 + Tests/XMLCoderTests/Minimal/DataTests.swift | 1 + Tests/XMLCoderTests/Minimal/DateTests.swift | 1 + Tests/XMLCoderTests/Minimal/DecimalTests.swift | 1 + Tests/XMLCoderTests/Minimal/FloatTests.swift | 1 + Tests/XMLCoderTests/Minimal/IntTests.swift | 1 + Tests/XMLCoderTests/Minimal/StringTests.swift | 1 + Tests/XMLCoderTests/Minimal/UIntTests.swift | 1 + Tests/XMLCoderTests/Minimal/URLTests.swift | 1 + 9 files changed, 9 insertions(+) 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 e022dc73..54934cf0 100644 --- a/Tests/XMLCoderTests/Minimal/DataTests.swift +++ b/Tests/XMLCoderTests/Minimal/DataTests.swift @@ -193,6 +193,7 @@ class DataTests: XCTestCase { } static var allTests = [ + ("testMissing", testMissing), ("testAttribute", testAttribute), ("testElement", testElement), ("testKeyFormated", testKeyFormated), diff --git a/Tests/XMLCoderTests/Minimal/DateTests.swift b/Tests/XMLCoderTests/Minimal/DateTests.swift index d39bffab..6df21095 100644 --- a/Tests/XMLCoderTests/Minimal/DateTests.swift +++ b/Tests/XMLCoderTests/Minimal/DateTests.swift @@ -155,6 +155,7 @@ class DateTests: XCTestCase { } static var allTests = [ + ("testMissing", testMissing), ("testAttribute", testAttribute), ("testElement", testElement), ("testKeyFormatedError", testKeyFormatedError), 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), ]