Skip to content

Commit 8097d34

Browse files
committed
Added: Expected to failed test cases on requesting an already requested mismatched container type
1 parent a75d98f commit 8097d34

File tree

2 files changed

+136
-18
lines changed

2 files changed

+136
-18
lines changed

test/stdlib/TestJSONEncoder.swift

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,75 @@ class TestJSONEncoder : TestJSONEncoderSuper {
138138
return Model(first: "Johnny Appleseed",
139139
second: "[email protected]")
140140
}
141+
142+
enum TopLevelCodingKeys : String, CodingKey {
143+
case top
144+
}
145+
146+
enum FirstNestedCodingKeys : String, CodingKey {
147+
case first
148+
}
149+
enum SecondNestedCodingKeys : String, CodingKey {
150+
case second
151+
}
141152
}
142153

143-
enum TopLevelCodingKeys : String, CodingKey {
144-
case top
145-
}
146-
147-
enum FirstNestedCodingKeys : String, CodingKey {
148-
case first
154+
let model = Model.testValue
155+
if #available(OSX 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *) {
156+
let expectedJSON = "{\"top\":{\"first\":\"Johnny Appleseed\",\"second\":\"[email protected]\"}}".data(using: .utf8)!
157+
_testRoundTrip(of: model, expectedJSON: expectedJSON, outputFormatting: [.sortedKeys])
158+
} else {
159+
_testRoundTrip(of: model)
149160
}
150-
enum SecondNestedCodingKeys : String, CodingKey {
151-
case second
161+
}
162+
163+
func testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey() {
164+
struct Model : Codable, Equatable {
165+
let first: String
166+
let second: String
167+
168+
init(from coder: Decoder) throws {
169+
let container = try coder.container(keyedBy: TopLevelCodingKeys.self)
170+
171+
let firstNestedContainer = try container.nestedContainer(keyedBy: FirstNestedCodingKeys.self, forKey: .top)
172+
self.first = try firstNestedContainer.decode(String.self, forKey: .first)
173+
174+
let secondNestedContainer = try container.nestedContainer(keyedBy: SecondNestedCodingKeys.self, forKey: .top)
175+
self.second = try secondNestedContainer.decode(String.self, forKey: .second)
176+
}
177+
178+
func encode(to encoder: Encoder) throws {
179+
var container = encoder.container(keyedBy: TopLevelCodingKeys.self)
180+
181+
var firstNestedContainer = container.nestedContainer(keyedBy: FirstNestedCodingKeys.self, forKey: .top)
182+
try firstNestedContainer.encode(self.first, forKey: .first)
183+
184+
var secondNestedContainer = container.nestedUnkeyedContainer(forKey: .top)
185+
try secondNestedContainer.encode(self.second)
186+
}
187+
188+
init(first: String, second: String) {
189+
self.first = first
190+
self.second = second
191+
}
192+
193+
static var testValue: Model {
194+
return Model(first: "Johnny Appleseed",
195+
second: "[email protected]")
196+
}
197+
198+
enum TopLevelCodingKeys : String, CodingKey {
199+
case top
200+
}
201+
202+
enum FirstNestedCodingKeys : String, CodingKey {
203+
case first
204+
}
205+
enum SecondNestedCodingKeys : String, CodingKey {
206+
case second
207+
}
152208
}
209+
153210
let model = Model.testValue
154211
if #available(OSX 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *) {
155212
let expectedJSON = "{\"top\":{\"first\":\"Johnny Appleseed\",\"second\":\"[email protected]\"}}".data(using: .utf8)!
@@ -158,7 +215,7 @@ class TestJSONEncoder : TestJSONEncoderSuper {
158215
_testRoundTrip(of: model)
159216
}
160217
}
161-
218+
162219
// MARK: - Output Formatting Tests
163220
func testEncodingOutputFormattingDefault() {
164221
let expectedJSON = "{\"name\":\"Johnny Appleseed\",\"email\":\"[email protected]\"}".data(using: .utf8)!
@@ -1672,6 +1729,11 @@ JSONEncoderTests.test("testEncodingTopLevelDeepStructuredType") { TestJSONEncode
16721729
JSONEncoderTests.test("testEncodingClassWhichSharesEncoderWithSuper") { TestJSONEncoder().testEncodingClassWhichSharesEncoderWithSuper() }
16731730
JSONEncoderTests.test("testEncodingTopLevelNullableType") { TestJSONEncoder().testEncodingTopLevelNullableType() }
16741731
JSONEncoderTests.test("testEncodingMultipleNestedContainersWithTheSameTopLevelKey") { TestJSONEncoder().testEncodingMultipleNestedContainersWithTheSameTopLevelKey() }
1732+
JSONEncoderTests.test("testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey")
1733+
.xfail(.always("Attempt to re-encode into already encoded container is invalid. This will always fail"))
1734+
.code {
1735+
TestJSONEncoder().testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey()
1736+
}
16751737
JSONEncoderTests.test("testEncodingOutputFormattingDefault") { TestJSONEncoder().testEncodingOutputFormattingDefault() }
16761738
JSONEncoderTests.test("testEncodingOutputFormattingPrettyPrinted") { TestJSONEncoder().testEncodingOutputFormattingPrettyPrinted() }
16771739
JSONEncoderTests.test("testEncodingOutputFormattingSortedKeys") { TestJSONEncoder().testEncodingOutputFormattingSortedKeys() }

test/stdlib/TestPlistEncoder.swift

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,23 +163,74 @@ class TestPropertyListEncoder : TestPropertyListEncoderSuper {
163163
return Model(first: "Johnny Appleseed",
164164
second: "[email protected]")
165165
}
166+
enum TopLevelCodingKeys : String, CodingKey {
167+
case top
168+
}
169+
170+
enum FirstNestedCodingKeys : String, CodingKey {
171+
case first
172+
}
173+
enum SecondNestedCodingKeys : String, CodingKey {
174+
case second
175+
}
166176
}
167177

168-
enum TopLevelCodingKeys : String, CodingKey {
169-
case top
178+
let model = Model.testValue
179+
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>[email protected]</string>\n\t</dict>\n</dict>\n</plist>\n".data(using: .utf8)!
180+
_testRoundTrip(of: model, in: .xml, expectedPlist: expectedXML)
181+
}
182+
183+
func testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey() {
184+
struct Model : Codable, Equatable {
185+
let first: String
186+
let second: String
187+
188+
init(from coder: Decoder) throws {
189+
let container = try coder.container(keyedBy: TopLevelCodingKeys.self)
190+
191+
let firstNestedContainer = try container.nestedContainer(keyedBy: FirstNestedCodingKeys.self, forKey: .top)
192+
self.first = try firstNestedContainer.decode(String.self, forKey: .first)
193+
194+
let secondNestedContainer = try container.nestedContainer(keyedBy: SecondNestedCodingKeys.self, forKey: .top)
195+
self.second = try secondNestedContainer.decode(String.self, forKey: .second)
196+
}
197+
198+
func encode(to encoder: Encoder) throws {
199+
var container = encoder.container(keyedBy: TopLevelCodingKeys.self)
200+
201+
var firstNestedContainer = container.nestedContainer(keyedBy: FirstNestedCodingKeys.self, forKey: .top)
202+
try firstNestedContainer.encode(self.first, forKey: .first)
203+
204+
var secondNestedContainer = container.nestedUnkeyedContainer(forKey: .top)
205+
try secondNestedContainer.encode(self.second)
206+
}
207+
208+
init(first: String, second: String) {
209+
self.first = first
210+
self.second = second
211+
}
212+
213+
static var testValue: Model {
214+
return Model(first: "Johnny Appleseed",
215+
second: "[email protected]")
216+
}
217+
enum TopLevelCodingKeys : String, CodingKey {
218+
case top
219+
}
220+
221+
enum FirstNestedCodingKeys : String, CodingKey {
222+
case first
223+
}
224+
enum SecondNestedCodingKeys : String, CodingKey {
225+
case second
226+
}
170227
}
171228

172-
enum FirstNestedCodingKeys : String, CodingKey {
173-
case first
174-
}
175-
enum SecondNestedCodingKeys : String, CodingKey {
176-
case second
177-
}
178229
let model = Model.testValue
179230
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>[email protected]</string>\n\t</dict>\n</dict>\n</plist>\n".data(using: .utf8)!
180231
_testRoundTrip(of: model, in: .xml, expectedPlist: expectedXML)
181232
}
182-
233+
183234
// MARK: - Encoder Features
184235
func testNestedContainerCodingPaths() {
185236
let encoder = JSONEncoder()
@@ -839,6 +890,11 @@ PropertyListEncoderTests.test("testEncodingTopLevelDeepStructuredType") { TestPr
839890
PropertyListEncoderTests.test("testEncodingClassWhichSharesEncoderWithSuper") { TestPropertyListEncoder().testEncodingClassWhichSharesEncoderWithSuper() }
840891
PropertyListEncoderTests.test("testEncodingTopLevelNullableType") { TestPropertyListEncoder().testEncodingTopLevelNullableType() }
841892
PropertyListEncoderTests.test("testEncodingMultipleNestedContainersWithTheSameTopLevelKey") { TestPropertyListEncoder().testEncodingMultipleNestedContainersWithTheSameTopLevelKey() }
893+
PropertyListEncoderTests.test("testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey")
894+
.xfail(.always("Attempt to re-encode into already encoded container is invalid. This will always fail"))
895+
.code {
896+
TestPropertyListEncoder().testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey()
897+
}
842898
PropertyListEncoderTests.test("testNestedContainerCodingPaths") { TestPropertyListEncoder().testNestedContainerCodingPaths() }
843899
PropertyListEncoderTests.test("testSuperEncoderCodingPaths") { TestPropertyListEncoder().testSuperEncoderCodingPaths() }
844900
PropertyListEncoderTests.test("testEncodingTopLevelData") { TestPropertyListEncoder().testEncodingTopLevelData() }

0 commit comments

Comments
 (0)