Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for nested keyed/unkeyed collections #38

Merged
merged 17 commits into from May 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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],
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved
]

let unkeyedWithinKeyed: [String: [Int]] = [
"first": [1, 2, 3],
"second": [1, 2, 3],
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved
]

let keyedWithinUnkeyed: [[String: Int]] = [
["first": 1],
["second": 2],
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved
]

let keyedWithinKeyed: [String: [String: Int]] = [
"first": ["a": 1, "b": 2],
"second": ["c": 3, "d": 4],
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved
]

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),
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved
]
}
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