Skip to content

Commit 02df514

Browse files
committed
NIT fixing and add some comments in the test methods
1 parent 8097d34 commit 02df514

File tree

2 files changed

+22
-54
lines changed

2 files changed

+22
-54
lines changed

test/stdlib/TestJSONEncoder.swift

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ class TestJSONEncoder : TestJSONEncoderSuper {
103103
_testRoundTrip(of: TopLevelWrapper(EnhancedBool.fileNotFound), expectedJSON: "{\"value\":null}".data(using: .utf8)!)
104104
}
105105

106-
// MARK: - Multiple Nested Keys with the same top-level key Encoding Tests
107106
func testEncodingMultipleNestedContainersWithTheSameTopLevelKey() {
108107
struct Model : Codable, Equatable {
109108
let first: String
@@ -160,59 +159,44 @@ class TestJSONEncoder : TestJSONEncoderSuper {
160159
}
161160
}
162161

163-
func testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey() {
164-
struct Model : Codable, Equatable {
162+
func testEncodingConflictedTypeNestedContainersWithTheSameTopLevelKey() {
163+
struct Model : Encodable, Equatable {
165164
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-
165+
178166
func encode(to encoder: Encoder) throws {
179167
var container = encoder.container(keyedBy: TopLevelCodingKeys.self)
180168

181169
var firstNestedContainer = container.nestedContainer(keyedBy: FirstNestedCodingKeys.self, forKey: .top)
182170
try firstNestedContainer.encode(self.first, forKey: .first)
183171

172+
// The following line would fail as it attempts to re-encode into already encoded container is invalid. This will always fail
184173
var secondNestedContainer = container.nestedUnkeyedContainer(forKey: .top)
185-
try secondNestedContainer.encode(self.second)
174+
try secondNestedContainer.encode("second")
186175
}
187176

188-
init(first: String, second: String) {
177+
init(first: String) {
189178
self.first = first
190-
self.second = second
191179
}
192180

193181
static var testValue: Model {
194-
return Model(first: "Johnny Appleseed",
195-
second: "[email protected]")
182+
return Model(first: "Johnny Appleseed")
196183
}
197184

198185
enum TopLevelCodingKeys : String, CodingKey {
199186
case top
200187
}
201-
202188
enum FirstNestedCodingKeys : String, CodingKey {
203189
case first
204190
}
205-
enum SecondNestedCodingKeys : String, CodingKey {
206-
case second
207-
}
208191
}
209192

210193
let model = Model.testValue
194+
// This following test would fail as it attempts to re-encode into already encoded container is invalid. This will always fail
211195
if #available(OSX 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *) {
212196
let expectedJSON = "{\"top\":{\"first\":\"Johnny Appleseed\",\"second\":\"[email protected]\"}}".data(using: .utf8)!
213-
_testRoundTrip(of: model, expectedJSON: expectedJSON, outputFormatting: [.sortedKeys])
197+
_testEncodeFailure(of: model)
214198
} else {
215-
_testRoundTrip(of: model)
199+
_testEncodeFailure(of: model)
216200
}
217201
}
218202

@@ -1729,10 +1713,10 @@ JSONEncoderTests.test("testEncodingTopLevelDeepStructuredType") { TestJSONEncode
17291713
JSONEncoderTests.test("testEncodingClassWhichSharesEncoderWithSuper") { TestJSONEncoder().testEncodingClassWhichSharesEncoderWithSuper() }
17301714
JSONEncoderTests.test("testEncodingTopLevelNullableType") { TestJSONEncoder().testEncodingTopLevelNullableType() }
17311715
JSONEncoderTests.test("testEncodingMultipleNestedContainersWithTheSameTopLevelKey") { TestJSONEncoder().testEncodingMultipleNestedContainersWithTheSameTopLevelKey() }
1732-
JSONEncoderTests.test("testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey")
1716+
JSONEncoderTests.test("testEncodingConflictedTypeNestedContainersWithTheSameTopLevelKey")
17331717
.xfail(.always("Attempt to re-encode into already encoded container is invalid. This will always fail"))
17341718
.code {
1735-
TestJSONEncoder().testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey()
1719+
TestJSONEncoder().testEncodingConflictedTypeNestedContainersWithTheSameTopLevelKey()
17361720
}
17371721
JSONEncoderTests.test("testEncodingOutputFormattingDefault") { TestJSONEncoder().testEncodingOutputFormattingDefault() }
17381722
JSONEncoderTests.test("testEncodingOutputFormattingPrettyPrinted") { TestJSONEncoder().testEncodingOutputFormattingPrettyPrinted() }

test/stdlib/TestPlistEncoder.swift

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ class TestPropertyListEncoder : TestPropertyListEncoderSuper {
128128
_testRoundTrip(of: TopLevelWrapper(EnhancedBool.fileNotFound), in: .xml)
129129
}
130130

131-
// MARK: - Multiple Nested Keys with the same top-level key Encoding Tests
132131
func testEncodingMultipleNestedContainersWithTheSameTopLevelKey() {
133132
struct Model : Codable, Equatable {
134133
let first: String
@@ -180,39 +179,27 @@ class TestPropertyListEncoder : TestPropertyListEncoderSuper {
180179
_testRoundTrip(of: model, in: .xml, expectedPlist: expectedXML)
181180
}
182181

183-
func testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey() {
184-
struct Model : Codable, Equatable {
182+
func testEncodingConflictedTypeNestedContainersWithTheSameTopLevelKey() {
183+
struct Model : Encodable, Equatable {
185184
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-
}
197185

198186
func encode(to encoder: Encoder) throws {
199187
var container = encoder.container(keyedBy: TopLevelCodingKeys.self)
200188

201189
var firstNestedContainer = container.nestedContainer(keyedBy: FirstNestedCodingKeys.self, forKey: .top)
202190
try firstNestedContainer.encode(self.first, forKey: .first)
203191

192+
// The following line would fail as it attempts to re-encode into already encoded container is invalid. This will always fail
204193
var secondNestedContainer = container.nestedUnkeyedContainer(forKey: .top)
205-
try secondNestedContainer.encode(self.second)
194+
try secondNestedContainer.encode("second")
206195
}
207196

208-
init(first: String, second: String) {
197+
init(first: String) {
209198
self.first = first
210-
self.second = second
211199
}
212200

213201
static var testValue: Model {
214-
return Model(first: "Johnny Appleseed",
215-
second: "[email protected]")
202+
return Model(first: "Johnny Appleseed")
216203
}
217204
enum TopLevelCodingKeys : String, CodingKey {
218205
case top
@@ -221,14 +208,11 @@ class TestPropertyListEncoder : TestPropertyListEncoderSuper {
221208
enum FirstNestedCodingKeys : String, CodingKey {
222209
case first
223210
}
224-
enum SecondNestedCodingKeys : String, CodingKey {
225-
case second
226-
}
227211
}
228212

229213
let model = Model.testValue
230-
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)!
231-
_testRoundTrip(of: model, in: .xml, expectedPlist: expectedXML)
214+
// This following test would fail as it attempts to re-encode into already encoded container is invalid. This will always fail
215+
_testEncodeFailure(of: model, in: .xml)
232216
}
233217

234218
// MARK: - Encoder Features
@@ -890,10 +874,10 @@ PropertyListEncoderTests.test("testEncodingTopLevelDeepStructuredType") { TestPr
890874
PropertyListEncoderTests.test("testEncodingClassWhichSharesEncoderWithSuper") { TestPropertyListEncoder().testEncodingClassWhichSharesEncoderWithSuper() }
891875
PropertyListEncoderTests.test("testEncodingTopLevelNullableType") { TestPropertyListEncoder().testEncodingTopLevelNullableType() }
892876
PropertyListEncoderTests.test("testEncodingMultipleNestedContainersWithTheSameTopLevelKey") { TestPropertyListEncoder().testEncodingMultipleNestedContainersWithTheSameTopLevelKey() }
893-
PropertyListEncoderTests.test("testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey")
877+
PropertyListEncoderTests.test("testEncodingConflictedTypeNestedContainersWithTheSameTopLevelKey")
894878
.xfail(.always("Attempt to re-encode into already encoded container is invalid. This will always fail"))
895879
.code {
896-
TestPropertyListEncoder().testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey()
880+
TestPropertyListEncoder().testEncodingConflictedTypeNestedContainersWithTheSameTopLevelKey()
897881
}
898882
PropertyListEncoderTests.test("testNestedContainerCodingPaths") { TestPropertyListEncoder().testNestedContainerCodingPaths() }
899883
PropertyListEncoderTests.test("testSuperEncoderCodingPaths") { TestPropertyListEncoder().testSuperEncoderCodingPaths() }

0 commit comments

Comments
 (0)