Skip to content

Commit

Permalink
Add EncoderTests.testNodeTypeMismatch()
Browse files Browse the repository at this point in the history
  • Loading branch information
norio-nomura committed Feb 17, 2018
1 parent 96f485f commit b7435be
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion Tests/YamsTests/EncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,59 @@ class EncoderTests: XCTestCase {
XCTAssertEqual(decodedYaml, ["200": "ok"])
}

func testNodeTypeMismatch() throws {
// https://github.com/jpsim/Yams/pull/95
struct Sample: Decodable { // swiftlint:disable:this nesting
let values: [String]
}

let validYaml = """
values:
- hello
"""
XCTAssertNoThrow(try YAMLDecoder().decode(Sample.self, from: validYaml))

let invalidYamls = [
// expecting scalar,
// but mapping instead
"""
values:
- hello:
""",
// but sequence instead
"""
values:
- [hello1, hello2]
""",
// expecting mapping,
// but scalar instead
"""
hello
""",
// but sequence instead
"""
- hello
""",
// expecting sequence,
// but scalar instead
"""
values: hello
""",
// but mapping instead
"""
values:
hello:
"""
]
for invalidYaml in invalidYamls {
XCTAssertThrowsError(try YAMLDecoder().decode(Sample.self, from: invalidYaml)) { error in
if case DecodingError.typeMismatch = error {} else {
XCTFail("unexpected error: \(error)")
}
}
}
}

// MARK: - Helper Functions
private func _testEncodeFailure<T: Encodable>(of value: T) {
do {
Expand Down Expand Up @@ -1007,7 +1060,8 @@ extension EncoderTests {
("testValuesInSingleValueContainer", testValuesInSingleValueContainer),
("testValuesInKeyedContainer", testValuesInKeyedContainer),
("testValuesInUnkeyedContainer", testValuesInUnkeyedContainer),
("testDictionary", testDictionary)
("testDictionary", testDictionary),
("testNodeTypeMismatch", testNodeTypeMismatch)
]
}
}
Expand Down

0 comments on commit b7435be

Please sign in to comment.