mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Added: Expected to failed test cases on requesting an already requested mismatched container type
This commit is contained in:
@@ -138,18 +138,19 @@ class TestJSONEncoder : TestJSONEncoderSuper {
|
||||
return Model(first: "Johnny Appleseed",
|
||||
second: "appleseed@apple.com")
|
||||
}
|
||||
|
||||
enum TopLevelCodingKeys : String, CodingKey {
|
||||
case top
|
||||
}
|
||||
|
||||
enum FirstNestedCodingKeys : String, CodingKey {
|
||||
case first
|
||||
}
|
||||
enum SecondNestedCodingKeys : String, CodingKey {
|
||||
case second
|
||||
}
|
||||
}
|
||||
|
||||
enum TopLevelCodingKeys : String, CodingKey {
|
||||
case top
|
||||
}
|
||||
|
||||
enum FirstNestedCodingKeys : String, CodingKey {
|
||||
case first
|
||||
}
|
||||
enum SecondNestedCodingKeys : String, CodingKey {
|
||||
case second
|
||||
}
|
||||
let model = Model.testValue
|
||||
if #available(OSX 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *) {
|
||||
let expectedJSON = "{\"top\":{\"first\":\"Johnny Appleseed\",\"second\":\"appleseed@apple.com\"}}".data(using: .utf8)!
|
||||
@@ -158,7 +159,63 @@ class TestJSONEncoder : TestJSONEncoderSuper {
|
||||
_testRoundTrip(of: model)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey() {
|
||||
struct Model : Codable, Equatable {
|
||||
let first: String
|
||||
let second: String
|
||||
|
||||
init(from coder: Decoder) throws {
|
||||
let container = try coder.container(keyedBy: TopLevelCodingKeys.self)
|
||||
|
||||
let firstNestedContainer = try container.nestedContainer(keyedBy: FirstNestedCodingKeys.self, forKey: .top)
|
||||
self.first = try firstNestedContainer.decode(String.self, forKey: .first)
|
||||
|
||||
let secondNestedContainer = try container.nestedContainer(keyedBy: SecondNestedCodingKeys.self, forKey: .top)
|
||||
self.second = try secondNestedContainer.decode(String.self, forKey: .second)
|
||||
}
|
||||
|
||||
func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: TopLevelCodingKeys.self)
|
||||
|
||||
var firstNestedContainer = container.nestedContainer(keyedBy: FirstNestedCodingKeys.self, forKey: .top)
|
||||
try firstNestedContainer.encode(self.first, forKey: .first)
|
||||
|
||||
var secondNestedContainer = container.nestedUnkeyedContainer(forKey: .top)
|
||||
try secondNestedContainer.encode(self.second)
|
||||
}
|
||||
|
||||
init(first: String, second: String) {
|
||||
self.first = first
|
||||
self.second = second
|
||||
}
|
||||
|
||||
static var testValue: Model {
|
||||
return Model(first: "Johnny Appleseed",
|
||||
second: "appleseed@apple.com")
|
||||
}
|
||||
|
||||
enum TopLevelCodingKeys : String, CodingKey {
|
||||
case top
|
||||
}
|
||||
|
||||
enum FirstNestedCodingKeys : String, CodingKey {
|
||||
case first
|
||||
}
|
||||
enum SecondNestedCodingKeys : String, CodingKey {
|
||||
case second
|
||||
}
|
||||
}
|
||||
|
||||
let model = Model.testValue
|
||||
if #available(OSX 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *) {
|
||||
let expectedJSON = "{\"top\":{\"first\":\"Johnny Appleseed\",\"second\":\"appleseed@apple.com\"}}".data(using: .utf8)!
|
||||
_testRoundTrip(of: model, expectedJSON: expectedJSON, outputFormatting: [.sortedKeys])
|
||||
} else {
|
||||
_testRoundTrip(of: model)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Output Formatting Tests
|
||||
func testEncodingOutputFormattingDefault() {
|
||||
let expectedJSON = "{\"name\":\"Johnny Appleseed\",\"email\":\"appleseed@apple.com\"}".data(using: .utf8)!
|
||||
@@ -1672,6 +1729,11 @@ JSONEncoderTests.test("testEncodingTopLevelDeepStructuredType") { TestJSONEncode
|
||||
JSONEncoderTests.test("testEncodingClassWhichSharesEncoderWithSuper") { TestJSONEncoder().testEncodingClassWhichSharesEncoderWithSuper() }
|
||||
JSONEncoderTests.test("testEncodingTopLevelNullableType") { TestJSONEncoder().testEncodingTopLevelNullableType() }
|
||||
JSONEncoderTests.test("testEncodingMultipleNestedContainersWithTheSameTopLevelKey") { TestJSONEncoder().testEncodingMultipleNestedContainersWithTheSameTopLevelKey() }
|
||||
JSONEncoderTests.test("testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey")
|
||||
.xfail(.always("Attempt to re-encode into already encoded container is invalid. This will always fail"))
|
||||
.code {
|
||||
TestJSONEncoder().testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey()
|
||||
}
|
||||
JSONEncoderTests.test("testEncodingOutputFormattingDefault") { TestJSONEncoder().testEncodingOutputFormattingDefault() }
|
||||
JSONEncoderTests.test("testEncodingOutputFormattingPrettyPrinted") { TestJSONEncoder().testEncodingOutputFormattingPrettyPrinted() }
|
||||
JSONEncoderTests.test("testEncodingOutputFormattingSortedKeys") { TestJSONEncoder().testEncodingOutputFormattingSortedKeys() }
|
||||
|
||||
@@ -163,23 +163,74 @@ class TestPropertyListEncoder : TestPropertyListEncoderSuper {
|
||||
return Model(first: "Johnny Appleseed",
|
||||
second: "appleseed@apple.com")
|
||||
}
|
||||
enum TopLevelCodingKeys : String, CodingKey {
|
||||
case top
|
||||
}
|
||||
|
||||
enum FirstNestedCodingKeys : String, CodingKey {
|
||||
case first
|
||||
}
|
||||
enum SecondNestedCodingKeys : String, CodingKey {
|
||||
case second
|
||||
}
|
||||
}
|
||||
|
||||
enum TopLevelCodingKeys : String, CodingKey {
|
||||
case top
|
||||
}
|
||||
|
||||
enum FirstNestedCodingKeys : String, CodingKey {
|
||||
case first
|
||||
}
|
||||
enum SecondNestedCodingKeys : String, CodingKey {
|
||||
case second
|
||||
}
|
||||
let model = Model.testValue
|
||||
let expectedXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n\t<key>top</key>\n\t<dict>\n\t\t<key>first</key>\n\t\t<string>Johnny Appleseed</string>\n\t\t<key>second</key>\n\t\t<string>appleseed@apple.com</string>\n\t</dict>\n</dict>\n</plist>\n".data(using: .utf8)!
|
||||
_testRoundTrip(of: model, in: .xml, expectedPlist: expectedXML)
|
||||
}
|
||||
|
||||
|
||||
func testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey() {
|
||||
struct Model : Codable, Equatable {
|
||||
let first: String
|
||||
let second: String
|
||||
|
||||
init(from coder: Decoder) throws {
|
||||
let container = try coder.container(keyedBy: TopLevelCodingKeys.self)
|
||||
|
||||
let firstNestedContainer = try container.nestedContainer(keyedBy: FirstNestedCodingKeys.self, forKey: .top)
|
||||
self.first = try firstNestedContainer.decode(String.self, forKey: .first)
|
||||
|
||||
let secondNestedContainer = try container.nestedContainer(keyedBy: SecondNestedCodingKeys.self, forKey: .top)
|
||||
self.second = try secondNestedContainer.decode(String.self, forKey: .second)
|
||||
}
|
||||
|
||||
func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: TopLevelCodingKeys.self)
|
||||
|
||||
var firstNestedContainer = container.nestedContainer(keyedBy: FirstNestedCodingKeys.self, forKey: .top)
|
||||
try firstNestedContainer.encode(self.first, forKey: .first)
|
||||
|
||||
var secondNestedContainer = container.nestedUnkeyedContainer(forKey: .top)
|
||||
try secondNestedContainer.encode(self.second)
|
||||
}
|
||||
|
||||
init(first: String, second: String) {
|
||||
self.first = first
|
||||
self.second = second
|
||||
}
|
||||
|
||||
static var testValue: Model {
|
||||
return Model(first: "Johnny Appleseed",
|
||||
second: "appleseed@apple.com")
|
||||
}
|
||||
enum TopLevelCodingKeys : String, CodingKey {
|
||||
case top
|
||||
}
|
||||
|
||||
enum FirstNestedCodingKeys : String, CodingKey {
|
||||
case first
|
||||
}
|
||||
enum SecondNestedCodingKeys : String, CodingKey {
|
||||
case second
|
||||
}
|
||||
}
|
||||
|
||||
let model = Model.testValue
|
||||
let expectedXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n\t<key>top</key>\n\t<dict>\n\t\t<key>first</key>\n\t\t<string>Johnny Appleseed</string>\n\t\t<key>second</key>\n\t\t<string>appleseed@apple.com</string>\n\t</dict>\n</dict>\n</plist>\n".data(using: .utf8)!
|
||||
_testRoundTrip(of: model, in: .xml, expectedPlist: expectedXML)
|
||||
}
|
||||
|
||||
// MARK: - Encoder Features
|
||||
func testNestedContainerCodingPaths() {
|
||||
let encoder = JSONEncoder()
|
||||
@@ -839,6 +890,11 @@ PropertyListEncoderTests.test("testEncodingTopLevelDeepStructuredType") { TestPr
|
||||
PropertyListEncoderTests.test("testEncodingClassWhichSharesEncoderWithSuper") { TestPropertyListEncoder().testEncodingClassWhichSharesEncoderWithSuper() }
|
||||
PropertyListEncoderTests.test("testEncodingTopLevelNullableType") { TestPropertyListEncoder().testEncodingTopLevelNullableType() }
|
||||
PropertyListEncoderTests.test("testEncodingMultipleNestedContainersWithTheSameTopLevelKey") { TestPropertyListEncoder().testEncodingMultipleNestedContainersWithTheSameTopLevelKey() }
|
||||
PropertyListEncoderTests.test("testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey")
|
||||
.xfail(.always("Attempt to re-encode into already encoded container is invalid. This will always fail"))
|
||||
.code {
|
||||
TestPropertyListEncoder().testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey()
|
||||
}
|
||||
PropertyListEncoderTests.test("testNestedContainerCodingPaths") { TestPropertyListEncoder().testNestedContainerCodingPaths() }
|
||||
PropertyListEncoderTests.test("testSuperEncoderCodingPaths") { TestPropertyListEncoder().testSuperEncoderCodingPaths() }
|
||||
PropertyListEncoderTests.test("testEncodingTopLevelData") { TestPropertyListEncoder().testEncodingTopLevelData() }
|
||||
|
||||
Reference in New Issue
Block a user