forked from CoreOffice/XMLCoder
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for nested keyed/unkeyed collections (CoreOffice#38)
CoreOffice#34 encounters a crasher when trying to decode nested arrays. This PR tries to add some more tests for nested keyed/unkeyed collections: - `[String: [Int]]` (dictionary of arrays) - `[String: [String: Int]]` (dictionary of dictionaries) - `[[Int]]` (array of arrays) - `[[String: Int]]` (array of dictionaries) * Added tests for nested complex (keyed/unkeyed) boxes * Fix trailing whitespace and indentation * Remove whitespace between doc comment and code * Avoid conflict in XMLDecoder.swift
- Loading branch information
1 parent
e7c58af
commit 5f2aed3
Showing
2 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// | ||
// NestingTests.swift | ||
// XMLCoderTests | ||
// | ||
// Created by Vincent Esche on 12/22/18. | ||
// | ||
|
||
import XCTest | ||
@testable import XMLCoder | ||
|
||
final class NestingTests: XCTestCase { | ||
var encoder: XMLEncoder { | ||
let encoder = XMLEncoder() | ||
encoder.outputFormatting = [.prettyPrinted] | ||
return encoder | ||
} | ||
|
||
var decoder: XMLDecoder { | ||
let decoder = XMLDecoder() | ||
return decoder | ||
} | ||
|
||
let unkeyedWithinUnkeyed: [[Int]] = [ | ||
[1, 2, 3], | ||
[1, 2, 3], | ||
] | ||
|
||
let unkeyedWithinKeyed: [String: [Int]] = [ | ||
"first": [1, 2, 3], | ||
"second": [1, 2, 3], | ||
] | ||
|
||
let keyedWithinUnkeyed: [[String: Int]] = [ | ||
["first": 1], | ||
["second": 2], | ||
] | ||
|
||
let keyedWithinKeyed: [String: [String: Int]] = [ | ||
"first": ["a": 1, "b": 2], | ||
"second": ["c": 3, "d": 4], | ||
] | ||
|
||
func testEncodeUnkeyedWithinUnkeyed() throws { | ||
XCTAssertNoThrow(try encoder.encode(unkeyedWithinUnkeyed, withRootKey: "element")) | ||
} | ||
|
||
func testEncodeUnkeyedWithinKeyed() throws { | ||
XCTAssertNoThrow(try encoder.encode(unkeyedWithinKeyed, withRootKey: "element")) | ||
} | ||
|
||
func testEncodeKeyedWithinUnkeyed() throws { | ||
XCTAssertNoThrow(try encoder.encode(keyedWithinUnkeyed, withRootKey: "element")) | ||
} | ||
|
||
func testEncodeKeyedWithinKeyed() throws { | ||
XCTAssertNoThrow(try encoder.encode(keyedWithinKeyed, withRootKey: "element")) | ||
} | ||
|
||
func testDecodeUnkeyedWithinUnkeyed() throws { | ||
let xml = | ||
""" | ||
<element> | ||
<element> | ||
<element>1</element> | ||
<element>2</element> | ||
<element>3</element> | ||
</element> | ||
<element> | ||
<element>1</element> | ||
<element>2</element> | ||
<element>3</element> | ||
</element> | ||
</element> | ||
""" | ||
let encoded = xml.data(using: .utf8)! | ||
|
||
XCTAssertNoThrow(try decoder.decode(type(of: unkeyedWithinUnkeyed), from: encoded)) | ||
} | ||
|
||
func testDecodeUnkeyedWithinKeyed() throws { | ||
let xml = | ||
""" | ||
<element> | ||
<first>1</first> | ||
<first>2</first> | ||
<first>3</first> | ||
<second>1</second> | ||
<second>2</second> | ||
<second>3</second> | ||
</element> | ||
""" | ||
let encoded = xml.data(using: .utf8)! | ||
|
||
XCTAssertNoThrow(try decoder.decode(type(of: unkeyedWithinKeyed), from: encoded)) | ||
} | ||
|
||
func testDecodeKeyedWithinUnkeyed() throws { | ||
let xml = | ||
""" | ||
<element> | ||
<element> | ||
<first>1</first> | ||
</element> | ||
<element> | ||
<second>2</second> | ||
</element> | ||
</element> | ||
""" | ||
let encoded = xml.data(using: .utf8)! | ||
|
||
XCTAssertNoThrow(try decoder.decode(type(of: keyedWithinUnkeyed), from: encoded)) | ||
} | ||
|
||
func testDecodeKeyedWithinKeyed() throws { | ||
let xml = | ||
""" | ||
<element> | ||
<first> | ||
<b>2</b> | ||
<a>1</a> | ||
</first> | ||
<second> | ||
<c>3</c> | ||
<d>4</d> | ||
</second> | ||
</element> | ||
""" | ||
let encoded = xml.data(using: .utf8)! | ||
|
||
XCTAssertNoThrow(try decoder.decode(type(of: keyedWithinKeyed), from: encoded)) | ||
} | ||
|
||
static var allTests = [ | ||
("testEncodeUnkeyedWithinUnkeyed", testEncodeUnkeyedWithinUnkeyed), | ||
("testEncodeUnkeyedWithinKeyed", testEncodeUnkeyedWithinKeyed), | ||
("testEncodeKeyedWithinUnkeyed", testEncodeKeyedWithinUnkeyed), | ||
("testEncodeKeyedWithinKeyed", testEncodeKeyedWithinKeyed), | ||
("testDecodeUnkeyedWithinUnkeyed", testDecodeUnkeyedWithinUnkeyed), | ||
("testDecodeUnkeyedWithinKeyed", testDecodeUnkeyedWithinKeyed), | ||
("testDecodeKeyedWithinUnkeyed", testDecodeKeyedWithinUnkeyed), | ||
("testDecodeKeyedWithinKeyed", testDecodeKeyedWithinKeyed), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters