Skip to content

Commit

Permalink
Add tests for nested keyed/unkeyed collections (CoreOffice#38)
Browse files Browse the repository at this point in the history
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
regexident authored and Arjun Gupta committed Jun 26, 2020
1 parent e7c58af commit 5f2aed3
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
143 changes: 143 additions & 0 deletions Tests/XMLCoderTests/NestingTests.swift
@@ -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),
]
}
4 changes: 4 additions & 0 deletions XMLCoder.xcodeproj/project.pbxproj
Expand Up @@ -94,6 +94,7 @@
D1CAD3622293FAB80077901D /* MixedContainerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1CAD3612293FAB80077901D /* MixedContainerTest.swift */; };
D1CB1EF521EA9599009CAF02 /* RJITest.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_37 /* RJITest.swift */; };
D1CFC8242226B13F00B03222 /* NamespaceTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1CFC8222226AFB400B03222 /* NamespaceTest.swift */; };
D1D50D3122942EAE007098FC /* NestingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1D50D3022942EAE007098FC /* NestingTests.swift */; };
D1E0C85321D8E65E0042A261 /* ErrorContextTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1E0C85121D8E6540042A261 /* ErrorContextTest.swift */; };
D1E0C85521D91EBF0042A261 /* Metatypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1E0C85421D91EBF0042A261 /* Metatypes.swift */; };
D1EC3E62225A32F500C610E3 /* BoxTreeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1EC3E61225A32F500C610E3 /* BoxTreeTests.swift */; };
Expand Down Expand Up @@ -216,6 +217,7 @@
D1AC9464224E3E1F004AB49B /* AttributedEnumIntrinsicTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributedEnumIntrinsicTest.swift; sourceTree = "<group>"; };
D1CAD3612293FAB80077901D /* MixedContainerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MixedContainerTest.swift; sourceTree = "<group>"; };
D1CFC8222226AFB400B03222 /* NamespaceTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NamespaceTest.swift; sourceTree = "<group>"; };
D1D50D3022942EAE007098FC /* NestingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NestingTests.swift; sourceTree = "<group>"; };
D1E0C85121D8E6540042A261 /* ErrorContextTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorContextTest.swift; sourceTree = "<group>"; };
D1E0C85421D91EBF0042A261 /* Metatypes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Metatypes.swift; sourceTree = "<group>"; };
D1EC3E61225A32F500C610E3 /* BoxTreeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoxTreeTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -403,6 +405,7 @@
B3BE1D602202C1F600259831 /* DynamicNodeEncodingTest.swift */,
D1E0C85121D8E6540042A261 /* ErrorContextTest.swift */,
D1CFC8222226AFB400B03222 /* NamespaceTest.swift */,
D1D50D3022942EAE007098FC /* NestingTests.swift */,
OBJ_33 /* NodeEncodingStrategyTests.swift */,
OBJ_34 /* NoteTest.swift */,
OBJ_35 /* PlantCatalog.swift */,
Expand Down Expand Up @@ -649,6 +652,7 @@
BF9457F221CBB6BC005ACFDE /* UnkeyedTests.swift in Sources */,
A61FE03C21E4EAB10015D993 /* KeyedIntTests.swift in Sources */,
BF9457F321CBB6BC005ACFDE /* DateTests.swift in Sources */,
D1D50D3122942EAE007098FC /* NestingTests.swift in Sources */,
BF9457D121CBB516005ACFDE /* UnkeyedBoxTests.swift in Sources */,
BF63EF6921D0FDB5001D38C5 /* XMLHeaderTests.swift in Sources */,
BF9457F021CBB6BC005ACFDE /* StringTests.swift in Sources */,
Expand Down

0 comments on commit 5f2aed3

Please sign in to comment.